import { useState } from 'react' import { AlertDescription_Shadcn_, AlertTitle_Shadcn_, Alert_Shadcn_, Button, CommandEmpty_Shadcn_, CommandGroup_Shadcn_, CommandInput_Shadcn_, CommandItem_Shadcn_, CommandList_Shadcn_, Command_Shadcn_, PopoverContent_Shadcn_, PopoverTrigger_Shadcn_, Popover_Shadcn_, ScrollArea, } from 'ui' import { convertArgumentTypes } from 'components/interfaces/Database/Functions/Functions.utils' import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext' import { useDatabaseFunctionsQuery } from 'data/database-functions/database-functions-query' import { Check, Code } from 'lucide-react' interface FunctionSelectorProps { className?: string size?: 'tiny' | 'small' showError?: boolean schema?: string value: string onChange: (value: string) => void disabled?: boolean } const FunctionSelector = ({ className, size = 'tiny', showError = true, disabled = false, schema, value, onChange, }: FunctionSelectorProps) => { const { project } = useProjectContext() const [open, setOpen] = useState(false) const { data, error, isLoading, isError, isSuccess, refetch } = useDatabaseFunctionsQuery({ projectRef: project?.ref, connectionString: project?.connectionString, }) const functions = (data ?? []) .filter((func) => schema && func.schema === schema) .filter((func) => func.return_type === 'json' || func.return_type === 'jsonb') .filter((func) => { const { value } = convertArgumentTypes(func.argument_types) if (value.length !== 1) return false return value[0].type === 'json' || value[0].type === 'jsonb' }) return (
{isLoading && ( )} {showError && isError && ( Failed to load functions Error: {error.message} )} {isSuccess && ( No functions found 7 ? 'h-[210px]' : ''}> {!(functions || []).length && ( No function with a single JSON/B argument
and JSON/B return type found in this schema.
)} {functions?.map((func) => ( { onChange(func.name) setOpen(false) }} onClick={() => { onChange(func.name) setOpen(false) }} > {func.name} {value === func.name && ( )} ))}
)}
) } export default FunctionSelector