import { PermissionAction } from '@supabase/shared-types/out/constants' import { Check, ChevronsUpDown, Plus } from 'lucide-react' import { ComponentPropsWithoutRef, forwardRef, useState } from 'react' import { Alert, AlertDescription, AlertTitle, Button, Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandSeparator, Popover, PopoverContent, PopoverTrigger, ScrollArea, Skeleton, } from 'ui' import { useSchemasQuery } from '@/data/database/schemas-query' import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions' import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' type SchemaSelectorProps = Omit, 'onSelect'> & { disabled?: boolean size?: 'tiny' | 'small' showError?: boolean selectedSchemaName?: string placeholderLabel?: string supportSelectAll?: boolean excludedSchemas?: string[] stopScrollPropagation?: boolean onSelectSchema: (name: string) => void onSelectCreateSchema?: () => void align?: 'start' | 'end' open?: boolean onOpenChange?: (open: boolean) => void } export const SchemaSelector = forwardRef( ( { className, disabled = false, size = 'tiny', showError = true, selectedSchemaName, placeholderLabel = 'Choose a schema...', supportSelectAll = false, excludedSchemas = [], stopScrollPropagation = false, onSelectSchema, onSelectCreateSchema, align = 'start', open: openProp, onOpenChange, ...rest }, ref ) => { const [internalOpen, setInternalOpen] = useState(false) const isControlled = openProp !== undefined const open = isControlled ? openProp : internalOpen const setOpen = (next: boolean) => { if (!isControlled) setInternalOpen(next) onOpenChange?.(next) } const { can: canCreateSchemas } = useAsyncCheckPermissions( PermissionAction.TENANT_SQL_ADMIN_WRITE, 'schemas' ) const { data: project } = useSelectedProjectQuery() const { data, isPending: isSchemasLoading, isSuccess: isSchemasSuccess, isError: isSchemasError, error: schemasError, refetch: refetchSchemas, } = useSchemasQuery({ projectRef: project?.ref, connectionString: project?.connectionString, }) const schemas = (data || []) .filter((schema) => !excludedSchemas.includes(schema.name)) .sort((a, b) => a.name.localeCompare(b.name)) return (
{isSchemasLoading && ( )} {showError && isSchemasError && ( Failed to load schemas Error: {(schemasError as any)?.message} )} {isSchemasSuccess && ( event.stopPropagation() : undefined} > No schemas found 7 ? 'h-[210px]' : ''}> {supportSelectAll && ( { onSelectSchema('*') setOpen(false) }} onClick={() => { onSelectSchema('*') setOpen(false) }} > All schemas {selectedSchemaName === '*' && ( )} )} {schemas.map((schema) => ( { onSelectSchema(schema.name) setOpen(false) }} onClick={() => { onSelectSchema(schema.name) setOpen(false) }} > {schema.name} {selectedSchemaName === schema.name && ( )} ))} {onSelectCreateSchema !== undefined && canCreateSchemas && ( <> { onSelectCreateSchema() setOpen(false) }} onClick={() => { onSelectCreateSchema() setOpen(false) }} > Create a new schema )} )}
) } ) SchemaSelector.displayName = 'SchemaSelector' export default SchemaSelector