mirror of
https://github.com/supabase/supabase.git
synced 2026-10-08 10:55:06 +03:00
* Set up react queries for enumerated types * Add delete enumerated type RQ * Implement types UI + deleting types * Implement create enumerated type * Implement update enumerated type * Tidy enumb dropdown in table editor * Fix default value dropdown for enum in column editor * Remove unused code * Add create enum type cta in column editor * Support updating enum description * Add learn more URL RE inability to delete a single enum value * Add sorting when creating enums * Support updating existing values in an enum type * Support updating order when editing existing enum * Address comments part 1 * Address comments part 2 * Use lucide icons * Update icons * Address comments part 3 * Refactor enum type forms to use react hook form * Update studio/components/interfaces/Database/EnumeratedTypes/EditEnumeratedTypeSidePanel.tsx * Update studio/components/interfaces/Database/EnumeratedTypes/EditEnumeratedTypeSidePanel.tsx * Copy * Small style fix to API panel * Use new input components and add alert when creating enum type * Address comments * Address comments --------- Co-authored-by: Terry Sutton <saltcod@gmail.com>
163 lines
5.3 KiB
TypeScript
163 lines
5.3 KiB
TypeScript
import { useState } from 'react'
|
|
import {
|
|
AlertDescription_Shadcn_,
|
|
AlertTitle_Shadcn_,
|
|
Alert_Shadcn_,
|
|
Button,
|
|
CommandEmpty_Shadcn_,
|
|
CommandGroup_Shadcn_,
|
|
CommandInput_Shadcn_,
|
|
CommandItem_Shadcn_,
|
|
CommandList_Shadcn_,
|
|
Command_Shadcn_,
|
|
IconCheck,
|
|
IconCode,
|
|
IconLoader,
|
|
IconPlus,
|
|
PopoverContent_Shadcn_,
|
|
PopoverTrigger_Shadcn_,
|
|
Popover_Shadcn_,
|
|
ScrollArea,
|
|
} from 'ui'
|
|
|
|
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
|
|
import { useSchemasQuery } from 'data/database/schemas-query'
|
|
|
|
interface SchemaSelectorProps {
|
|
className?: string
|
|
size?: 'tiny' | 'small'
|
|
showError?: boolean
|
|
selectedSchemaName: string
|
|
onSelectSchema: (name: string) => void
|
|
onSelectCreateSchema?: () => void
|
|
}
|
|
|
|
const SchemaSelector = ({
|
|
className,
|
|
size = 'tiny',
|
|
showError = true,
|
|
selectedSchemaName,
|
|
onSelectSchema,
|
|
onSelectCreateSchema,
|
|
}: SchemaSelectorProps) => {
|
|
const [open, setOpen] = useState(false)
|
|
|
|
const { project } = useProjectContext()
|
|
const {
|
|
data,
|
|
isLoading: isSchemasLoading,
|
|
isSuccess: isSchemasSuccess,
|
|
isError: isSchemasError,
|
|
error: schemasError,
|
|
refetch: refetchSchemas,
|
|
} = useSchemasQuery({
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
})
|
|
|
|
const schemas = (data ?? []).sort((a, b) => (a.name > b.name ? 0 : -1))
|
|
|
|
return (
|
|
<div className={className}>
|
|
{isSchemasLoading && (
|
|
<Button
|
|
type="outline"
|
|
className="w-full [&>span]:w-full"
|
|
icon={<IconLoader className="animate-spin" size={12} />}
|
|
>
|
|
<div>
|
|
<div className="w-full flex space-x-3 py-0.5">
|
|
<p className="text-xs text-light">Loading schemas...</p>
|
|
</div>
|
|
</div>
|
|
</Button>
|
|
)}
|
|
|
|
{showError && isSchemasError && (
|
|
<Alert_Shadcn_ variant="warning" className="!px-3 !py-3">
|
|
<AlertTitle_Shadcn_ className="text-xs text-amber-900">
|
|
Failed to load schemas
|
|
</AlertTitle_Shadcn_>
|
|
<AlertDescription_Shadcn_ className="text-xs mb-2">
|
|
Error: {schemasError?.message}
|
|
</AlertDescription_Shadcn_>
|
|
<Button type="default" size="tiny" onClick={() => refetchSchemas()}>
|
|
Reload schemas
|
|
</Button>
|
|
</Alert_Shadcn_>
|
|
)}
|
|
|
|
{isSchemasSuccess && (
|
|
<Popover_Shadcn_ open={open} onOpenChange={setOpen} modal={false}>
|
|
<PopoverTrigger_Shadcn_ asChild>
|
|
<Button
|
|
size={size}
|
|
type="outline"
|
|
className={`w-full [&>span]:w-full ${size === 'small' ? 'py-1.5' : ''}`}
|
|
iconRight={
|
|
<IconCode className="text-foreground-light rotate-90" strokeWidth={2} size={12} />
|
|
}
|
|
>
|
|
<div className="w-full flex space-x-3 py-0.5">
|
|
<p className="text-xs text-light">schema</p>
|
|
<p className="text-xs">{selectedSchemaName}</p>
|
|
</div>
|
|
</Button>
|
|
</PopoverTrigger_Shadcn_>
|
|
<PopoverContent_Shadcn_ className="p-0 w-64" side="bottom" align="start">
|
|
<Command_Shadcn_>
|
|
<CommandInput_Shadcn_ placeholder="Find schema..." />
|
|
<CommandList_Shadcn_>
|
|
<CommandEmpty_Shadcn_>No schemas found</CommandEmpty_Shadcn_>
|
|
<CommandGroup_Shadcn_>
|
|
<ScrollArea className={(schemas || []).length > 7 ? 'h-[210px]' : ''}>
|
|
{schemas?.map((schema) => (
|
|
<CommandItem_Shadcn_
|
|
key={schema.id}
|
|
className="cursor-pointer flex items-center justify-between space-x-2 w-full"
|
|
onSelect={() => {
|
|
onSelectSchema(schema.name)
|
|
setOpen(false)
|
|
}}
|
|
onClick={() => {
|
|
onSelectSchema(schema.name)
|
|
setOpen(false)
|
|
}}
|
|
>
|
|
<span>{schema.name}</span>
|
|
{schema.name === selectedSchemaName && (
|
|
<IconCheck className="text-brand" strokeWidth={2} />
|
|
)}
|
|
</CommandItem_Shadcn_>
|
|
))}
|
|
</ScrollArea>
|
|
</CommandGroup_Shadcn_>
|
|
{onSelectCreateSchema !== undefined && (
|
|
<CommandGroup_Shadcn_ className="border-t">
|
|
<CommandItem_Shadcn_
|
|
className="cursor-pointer flex items-center gap-x-2 w-full"
|
|
onSelect={() => {
|
|
onSelectCreateSchema()
|
|
setOpen(false)
|
|
}}
|
|
onClick={() => {
|
|
onSelectCreateSchema()
|
|
setOpen(false)
|
|
}}
|
|
>
|
|
<IconPlus />
|
|
Create a new schema
|
|
</CommandItem_Shadcn_>
|
|
</CommandGroup_Shadcn_>
|
|
)}
|
|
</CommandList_Shadcn_>
|
|
</Command_Shadcn_>
|
|
</PopoverContent_Shadcn_>
|
|
</Popover_Shadcn_>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default SchemaSelector
|