import { useParams } from 'common' import { uniqBy } from 'lodash' import { Check, Plus } from 'lucide-react' import { useState } from 'react' import { Alert, AlertDescription, AlertTitle, Button, ComboboxTrigger, Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandSeparator, Popover, PopoverContent, PopoverTrigger, ScrollArea, } from 'ui' import { CommandItemLink } from '@/components/ui/CommandItemLink' import { DatabaseFunctionsData, useDatabaseFunctionsQuery, } from '@/data/database-functions/database-functions-query' import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' type DatabaseFunction = DatabaseFunctionsData[number] interface FunctionSelectorProps { className?: string size?: 'tiny' | 'small' showError?: boolean schema?: string value: string onChange: (value: string) => void disabled?: boolean // used to filter the functions by a criteria filterFunction?: (func: DatabaseFunction) => boolean noResultsLabel?: React.ReactNode } const FunctionSelector = ({ className, size = 'tiny', showError = true, disabled = false, schema, value, onChange, filterFunction = () => true, noResultsLabel = No functions found in this schema., }: FunctionSelectorProps) => { const { ref } = useParams() const { data: project } = useSelectedProjectQuery() const [open, setOpen] = useState(false) const { data, error, isPending: isLoading, isError, isSuccess, refetch, } = useDatabaseFunctionsQuery({ projectRef: project?.ref, connectionString: project?.connectionString, }) const filteredFunctions = (data ?? []) .filter((func) => schema && func.schema === schema) .filter(filterFunction) const functions = uniqBy(filteredFunctions, (func) => func.name) return (
{isLoading && ( )} {showError && isError && ( Failed to load functions Error: {error.message} )} {isSuccess && ( {value ? ( function: {value} ) : ( Select a function )} No functions found 7 ? 'h-[210px]' : ''}> {!functions.length && ( {noResultsLabel} )} {functions.map((func) => ( { onChange(func.name) setOpen(false) }} onClick={() => { onChange(func.name) setOpen(false) }} > {func.name} {value === func.name && ( )} ))} setOpen(false)} >

New function

)}
) } export default FunctionSelector