|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { Disclosure, DisclosureButton, DisclosurePanel, Input } from '@headlessui/react'; |
| 4 | +import { ChevronDownIcon, PlusIcon } from '@heroicons/react/20/solid'; |
| 5 | +import { useQuery } from '@tanstack/react-query'; |
| 6 | +import { Array as EffectArray, Order, pipe } from 'effect'; |
| 7 | +import { useState } from 'react'; |
| 8 | + |
| 9 | +import type { SchemaBrowserTypesQuery } from '../../../generated/graphql'; |
| 10 | +import { schemaBrowserQueryOptions } from '../../../hooks/useSchemaBrowserQuery'; |
| 11 | +import { Loading } from '../../Loading'; |
| 12 | + |
| 13 | +export type SchemaBrowserType = NonNullable<SchemaBrowserTypesQuery['space']>['types'][number]; |
| 14 | +type ExtendedSchemaBrowserType = SchemaBrowserType & { slug: string }; |
| 15 | + |
| 16 | +const SchemaTypeOrder = Order.mapInput(Order.string, (type: SchemaBrowserType) => type.name || type.id); |
| 17 | + |
| 18 | +export type SchemaBrowserProps = Readonly<{ |
| 19 | + typeSelected(type: SchemaBrowserType): void; |
| 20 | +}>; |
| 21 | +export function SchemaBrowser(props: SchemaBrowserProps) { |
| 22 | + const [typeSearch, setTypeSearch] = useState(''); |
| 23 | + |
| 24 | + const { data: types, isLoading } = useQuery({ |
| 25 | + ...schemaBrowserQueryOptions, |
| 26 | + select(data) { |
| 27 | + const types = data.space?.types ?? []; |
| 28 | + const mappedAndSorted = pipe( |
| 29 | + types, |
| 30 | + EffectArray.map((type) => { |
| 31 | + const slugifiedProps = EffectArray.reduce(type.properties, '', (slug, curr) => `${slug}${curr.name || ''}`); |
| 32 | + const slug = `${type.name || ''}${slugifiedProps}`.toLowerCase(); |
| 33 | + return { |
| 34 | + ...type, |
| 35 | + slug, |
| 36 | + } as const satisfies ExtendedSchemaBrowserType; |
| 37 | + }), |
| 38 | + EffectArray.sort(SchemaTypeOrder), |
| 39 | + ); |
| 40 | + if (!typeSearch) { |
| 41 | + return mappedAndSorted; |
| 42 | + } |
| 43 | + return pipe( |
| 44 | + mappedAndSorted, |
| 45 | + EffectArray.filter((type) => type.slug.includes(typeSearch.toLowerCase())), |
| 46 | + ); |
| 47 | + }, |
| 48 | + }); |
| 49 | + |
| 50 | + return ( |
| 51 | + <div className="flex flex-col gap-y-6"> |
| 52 | + <h3 className="text-sm/6 font-semibold text-gray-900 dark:text-white flex items-center gap-x-3"> |
| 53 | + Schema Browser |
| 54 | + {isLoading ? <Loading /> : null} |
| 55 | + </h3> |
| 56 | + <div className="bg-gray-100 dark:bg-slate-900 rounded-lg flex flex-col gap-y-3 pt-2"> |
| 57 | + <div className="px-3 mt-2"> |
| 58 | + <Input |
| 59 | + id="SchemaBrowserSearch" |
| 60 | + name="SchemaBrowserSearch" |
| 61 | + value={typeSearch} |
| 62 | + onChange={(e) => setTypeSearch(e.target.value || '')} |
| 63 | + type="search" |
| 64 | + placeholder="Search types..." |
| 65 | + className="block min-w-0 grow py-1.5 pl-2 pr-3 rounded-md bg-white dark:bg-slate-700 data-[state=invalid]:pr-10 text-base text-gray-900 dark:text-white placeholder:text-gray-400 dark:placeholder:text-gray-500 focus:outline sm:text-sm/6 focus-visible:outline-none w-full" |
| 66 | + /> |
| 67 | + </div> |
| 68 | + <ul className="px-4 divide-y divide-gray-100 dark:divide-gray-700 overflow-y-auto h-fit max-h-[500px] 2xl:max-h-[750px]"> |
| 69 | + {(types ?? []).map((_type) => ( |
| 70 | + <Disclosure as="li" key={_type.id} className="py-3 flex flex-col gap-y-2"> |
| 71 | + <div className="flex items-center justify-between gap-x-6"> |
| 72 | + <DisclosureButton |
| 73 | + as="div" |
| 74 | + disabled={_type.properties.length === 0} |
| 75 | + data-interactive={_type.properties.length > 0 ? 'clickable' : undefined} |
| 76 | + className="min-w-0 data-[interactive=clickable]:cursor-pointer" |
| 77 | + > |
| 78 | + <div className="flex items-center gap-x-2"> |
| 79 | + {_type.properties.length > 0 ? <ChevronDownIcon className="size-4" aria-hidden="true" /> : null} |
| 80 | + <p className="text-sm/6 font-semibold text-gray-900 dark:text-white">{_type.name || _type.id}</p> |
| 81 | + <p className="text-xs font-light text-gray-600 dark:text-gray-300 bg-gray-50 dark:bg-slate-600 ring-gray-500/10 rounded-md px-1.5 py-0.5 whitespace-nowrap ring-1 ring-inset"> |
| 82 | + {_type.id} |
| 83 | + </p> |
| 84 | + </div> |
| 85 | + </DisclosureButton> |
| 86 | + <div className="flex flex-none items-center justify-end"> |
| 87 | + <button |
| 88 | + type="button" |
| 89 | + className="rounded-md bg-white dark:bg-black p-2 cursor-pointer text-sm font-semibold text-gray-900 dark:text-white shadow-xs ring-1 ring-gray-300 dark:ring-black/15 ring-inset hover:bg-gray-50 dark:hover:bg-slate-950 inline-flex flex-col items-center justify-center" |
| 90 | + onClick={() => props.typeSelected(_type)} |
| 91 | + > |
| 92 | + <PlusIcon className="size-4" aria-hidden="true" /> |
| 93 | + </button> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + <DisclosurePanel |
| 97 | + as="div" |
| 98 | + transition |
| 99 | + className="w-full transition duration-200 ease-in-out py-1.5 flex flex-col gap-y-1" |
| 100 | + > |
| 101 | + <ul className="w-full pl-6 pr-3 divide-y divide-gray-300 dark:divide-gray-800"> |
| 102 | + {_type.properties.map((prop) => ( |
| 103 | + <li key={prop.id} className="w-full text-xs py-1.5 flex items-center gap-x-2 list-disc"> |
| 104 | + {prop.name || prop.id} |
| 105 | + {prop.valueType?.name != null ? ( |
| 106 | + <p className="text-xs font-light text-gray-600 dark:text-gray-300 bg-gray-50 dark:bg-slate-700 ring-gray-500/10 dark:ring-gray-700/10 rounded-md px-1.5 py-0.5 whitespace-nowrap ring-1 ring-inset"> |
| 107 | + {prop.valueType.name} |
| 108 | + </p> |
| 109 | + ) : null} |
| 110 | + </li> |
| 111 | + ))} |
| 112 | + </ul> |
| 113 | + </DisclosurePanel> |
| 114 | + </Disclosure> |
| 115 | + ))} |
| 116 | + </ul> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + ); |
| 120 | +} |
0 commit comments