import { Loader2, Search } from 'lucide-react' import { ComponentPropsWithoutRef, forwardRef, useMemo, useState } from 'react' import { Button, Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, Popover, PopoverContent, PopoverTrigger, ScrollArea, } from 'ui' import { useInfiniteTablesQuery } from '@/data/tables/tables-query' import { useDebouncedValue } from '@/hooks/misc/useDebouncedValue' import type { SafePostgresTable } from '@/lib/postgres-types' type FindTableSelectorProps = Omit, 'onSelect'> & { projectRef?: string connectionString?: string | null schema?: string disabled?: boolean size?: 'tiny' | 'small' open: boolean onOpenChange: (open: boolean) => void onSelect: (table: SafePostgresTable) => void } export const FindTableSelector = forwardRef( ( { className, projectRef, connectionString, schema, disabled = false, size = 'tiny', open, onOpenChange, onSelect, ...rest }, ref ) => { const [search, setSearch] = useState('') const debouncedSearch = useDebouncedValue(search, 300) const nameFilter = debouncedSearch.trim() || undefined const { data, isFetching, hasNextPage, isFetchingNextPage, fetchNextPage } = useInfiniteTablesQuery( { projectRef, connectionString, schema, includeColumns: false, pageSize: 50, nameFilter, }, { enabled: open } ) const tables = useMemo(() => data?.pages.flat() ?? [], [data]) const handleOpenChange = (next: boolean) => { if (!next) setSearch('') onOpenChange(next) } return (
{isFetching && tables.length === 0 ? (
Loading tables
) : ( <> No tables found 7 ? 'h-[210px]' : ''}> {tables.map((table) => ( { onSelect(table) handleOpenChange(false) }} > {table.name} ))} {hasNextPage && (
)}
)}
) } ) FindTableSelector.displayName = 'FindTableSelector'