'use client' import { useParams } from 'common' import { Database, Loader2 } from 'lucide-react' import { useMemo } from 'react' import { EmptyState, ResultsList, SkeletonResults, type SearchResult, } from './ContextSearchResults.shared' import { useTablesQuery } from '@/data/tables/tables-query' import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' interface TableSearchResultsProps { query: string } export function TableSearchResults({ query }: TableSearchResultsProps) { const { ref: projectRef } = useParams() const { data: project } = useSelectedProjectQuery() const trimmedQuery = query.trim() const { data: tables, isLoading: isLoadingTables, isError: isErrorTables, } = useTablesQuery( { projectRef: project?.ref, connectionString: project?.connectionString, includeColumns: false, sortByProperty: 'name', }, { enabled: !!project?.ref, } ) const tableResults: SearchResult[] = useMemo(() => { if (!tables) return [] const filtered = trimmedQuery ? tables.filter((table) => { const searchLower = trimmedQuery.toLowerCase() const tableName = table.name?.toLowerCase() || '' const schemaName = table.schema?.toLowerCase() || '' const fullName = `${schemaName}.${tableName}` return ( tableName.includes(searchLower) || schemaName.includes(searchLower) || fullName.includes(searchLower) ) }) : tables // Limit results for performance return filtered.slice(0, 20).map((table) => { const displayName = table.schema && table.schema !== 'public' ? `${table.schema}.${table.name}` : table.name || 'Untitled Table' const description = table.comment ? table.comment.length > 50 ? `${table.comment.slice(0, 50)}...` : table.comment : undefined return { id: String(table.id), name: displayName, description, } }) }, [tables, trimmedQuery]) const totalTables = tables?.length ?? 0 const renderFooter = () => (
Failed to load tables