import { SimplePagination } from '@components/common/SimplePagination.js'; import { Button } from '@components/common/ui/Button.js'; import { Input } from '@components/common/ui/Input.js'; import { Skeleton } from '@components/common/ui/Skeleton.js'; import { Check } from 'lucide-react'; import React from 'react'; import { useQuery } from 'urql'; import { AtLeastOne } from '../../types/atLeastOne.js'; const SearchQuery = ` query Query ($filters: [FilterInput!]) { collections(filters: $filters) { items { collectionId uuid name } total } } `; interface CollectionIdentifier { collectionId?: string | number; uuid?: string; } const CollectionListSkeleton: React.FC = () => { const skeletonItems = Array(5).fill(0); return (
{skeletonItems.map((_, index) => (
))}
); }; const isCollectionSelected = ( collection: CollectionIdentifier, selectedCollections: AtLeastOne[] ): boolean => { return selectedCollections.some( (selected) => (selected?.collectionId && selected.collectionId === collection.collectionId) || (selected?.uuid && selected.uuid === collection.uuid) ); }; const CollectionSelector: React.FC<{ onSelect: (id: string | number, uuid: string, name: string) => void; onUnSelect: (id: string | number, uuid: string, name: string) => void; selectedCollections: AtLeastOne[]; }> = ({ onSelect, onUnSelect, selectedCollections }) => { const [internalSelectedCollections, setInternalSelectedCollections] = React.useState[]>( selectedCollections || [] ); const [loading, setLoading] = React.useState(false); const limit = 10; const [inputValue, setInputValue] = React.useState(''); const [page, setPage] = React.useState(1); const [result, reexecuteQuery] = useQuery({ query: SearchQuery, variables: { filters: inputValue ? [ { key: 'name', operation: 'like', value: inputValue }, { key: 'page', operation: 'eq', value: page.toString() }, { key: 'limit', operation: 'eq', value: limit.toString() } ] : [ { key: 'limit', operation: 'eq', value: limit.toString() }, { key: 'page', operation: 'eq', value: page.toString() } ] }, pause: true }); React.useEffect(() => { reexecuteQuery({ requestPolicy: 'network-only' }); }, [page]); React.useEffect(() => { const timer = setTimeout(() => { setLoading(false); if (inputValue !== '') { reexecuteQuery({ requestPolicy: 'network-only' }); } }, 1500); return () => clearTimeout(timer); }, [inputValue]); const { data, fetching, error } = result as { data: { collections: { items: Array<{ collectionId: string | number; uuid: string; name: string; }>; total: number; }; }; fetching: boolean; error: Error | undefined; }; if (error) { return (

There was an error fetching collections. {error.message}

); } return (
) => { setInputValue(e.target.value); setLoading(true); }} />
{(fetching || loading) && } {!fetching && data && (
{data.collections.items.length === 0 && (
{inputValue ? (

No collections found for query "{inputValue}”

) : (

You have no collections to display

)}
)} {data.collections.items.map((c) => (

{c.name}

{!isCollectionSelected(c, internalSelectedCollections) && ( )} {isCollectionSelected(c, internalSelectedCollections) && ( )}
))}
)}
); }; export { CollectionSelector };