import { debounce } from 'lodash'
import { Check, Loader2, Search } from 'lucide-react'
import { useCallback, useState } from 'react'
import { useEntityTypesQuery } from 'data/entity-types/entity-types-infinite-query'
import { useSelectedProject } from 'hooks/misc/useSelectedProject'
import {
Command_Shadcn_,
CommandEmpty_Shadcn_,
CommandGroup_Shadcn_,
CommandItem_Shadcn_,
CommandList_Shadcn_,
} from 'ui'
import { Admonition } from 'ui-patterns'
import { Input } from 'ui-patterns/DataInputs/Input'
import { DropdownScrollArea } from './DropdownScrollArea'
interface EntitiesDropdownMenuProps {
selectedSchemas: string[]
selectedEntities: readonly { schema: string; name: string }[]
onToggleEntity: (value: { schema: string; name: string }) => void
}
export const EntitiesDropdownMenu = ({
selectedSchemas,
selectedEntities,
onToggleEntity,
}: EntitiesDropdownMenuProps) => {
const project = useSelectedProject()
const [search, setSearch] = useState('')
// [Joshen] Not gonna do infinite loading here, but rather just try to load sufficient tables
// I reckon users will opt to just search if there's many tables here given the small real estate
const {
data: entitiesData,
error,
isLoading,
isFetching,
isError,
} = useEntityTypesQuery({
projectRef: project?.ref,
connectionString: project?.connectionString,
schemas: selectedSchemas,
limit: 100,
search: search.length === 0 ? undefined : search,
})
const entities = (
entitiesData?.pages[0].data.entities ? [...entitiesData?.pages[0].data.entities] : []
).sort((a, b) => (a.name > b.name ? 0 : -1))
// eslint-disable-next-line react-hooks/exhaustive-deps
const searchInputHandler = useCallback(debounce(setSearch, 1000), [])
if (isError) {
return (
)
}
return (
{/* Separate input for async searching */}
}
className="text-xs outline-none rounded-b-none border-0 border-b pl-9 pr-7"
onChange={(e) => searchInputHandler(e.target.value)}
actions={
(isLoading || isFetching) && (
)
}
/>
No tables found
7 ? 'h-[210px]' : ''}
>
{entities?.map((entity) => (
onToggleEntity({ schema: entity.schema, name: entity.name })}
onClick={() => onToggleEntity({ schema: entity.schema, name: entity.name })}
>
{entity.schema}
{entity.name}
{selectedEntities.find(
(x) => x.name === entity.name && x.schema === entity.schema
) && }
))}
)
}