import { zodResolver } from '@hookform/resolvers/zod' import { useState } from 'react' import { SubmitHandler, useForm } from 'react-hook-form' import { toast } from 'sonner' import z from 'zod' import { PermissionAction } from '@supabase/shared-types/out/constants' import EnableExtensionModal from 'components/interfaces/Database/Extensions/EnableExtensionModal' import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext' import { useDatabaseExtensionsQuery } from 'data/database-extensions/database-extensions-query' import { useDatabaseQueueCreateMutation } from 'data/database-queues/database-queues-create-mutation' import { useCheckPermissions } from 'hooks/misc/useCheckPermissions' import { Badge, Button, Form_Shadcn_, FormControl_Shadcn_, FormField_Shadcn_, Input, Input_Shadcn_, RadioGroupStacked, RadioGroupStackedItem, Separator, SheetFooter, SheetHeader, SheetSection, SheetTitle, } from 'ui' import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import { QUEUE_TYPES } from './Queues.constants' export interface CreateQueueSheetProps { isClosing: boolean setIsClosing: (v: boolean) => void onClose: () => void } const normalQueueSchema = z.object({ type: z.literal('normal'), }) const partitionedQueueSchema = z.object({ type: z.literal('partitioned'), partitionInterval: z.number(), retentionInterval: z.number(), }) const unloggedQueueSchema = z.object({ type: z.literal('unlogged'), }) const FormSchema = z.object({ name: z .string() .trim() .min(1, 'Please provide a name for your queue') .max(47, "The name can't be longer than 47 characters"), values: z.discriminatedUnion('type', [ normalQueueSchema, partitionedQueueSchema, unloggedQueueSchema, ]), }) export type CreateQueueForm = z.infer export type QueueType = CreateQueueForm['values'] const FORM_ID = 'create-queue-sidepanel' export const CreateQueueSheet = ({ isClosing, setIsClosing, onClose }: CreateQueueSheetProps) => { const [showEnableExtensionModal, setShowEnableExtensionModal] = useState(false) const { mutate: createQueue, isLoading } = useDatabaseQueueCreateMutation() const canToggleExtensions = useCheckPermissions( PermissionAction.TENANT_SQL_ADMIN_WRITE, 'extensions' ) const form = useForm({ resolver: zodResolver(FormSchema), defaultValues: { name: '', values: { type: 'normal', }, }, }) const { project } = useProjectContext() const isEdited = form.formState.isDirty // if the form hasn't been touched and the user clicked esc or the backdrop, close the sheet if (!isEdited && isClosing) { onClose() } const onClosePanel = () => { if (isEdited) { setIsClosing(true) } else { onClose() } } const onSubmit: SubmitHandler = async ({ name, values }) => { let query = `SELECT pgmq.create('${name}');` if (values.type === 'partitioned') { query = `select from pgmq.create_partitioned('${name}', '${values.partitionInterval}', '${values.retentionInterval}');` } if (values.type === 'unlogged') { query = `SELECT pgmq.create_unlogged('${name}');` } createQueue( { projectRef: project!.ref, connectionString: project?.connectionString, query, }, { onSuccess: () => { toast.success(`Successfully created queue ${name}`) onClose() }, } ) } const { data } = useDatabaseExtensionsQuery({ projectRef: project?.ref, connectionString: project?.connectionString, }) const pgPartmanExtension = (data ?? []).find((ext) => ext.name === 'pg_partman') const pgPartmanExtensionInstalled = pgPartmanExtension?.installed_version != undefined const queueType = form.watch('values.type') return ( <>
Create a new queue
( )} /> ( {QUEUE_TYPES.map((definition) => (
{definition.icon}

{definition.label}

{definition.description}

{definition.value === 'partitioned' ? (
COMING SOON
) : null} {/* {!pgPartmanExtensionInstalled && definition.value === 'partitioned' ? (
pg_partman needs to be installed to use this type
) : null} */}
))}
)} /> {/* {!pgPartmanExtensionInstalled && ( Enable pg_partman for partitioned queues } description={
This will allow you to create partitioned queues which can handle a large amount of messages setShowEnableExtensionModal(true)} tooltip={{ content: { side: 'bottom', text: 'You need additional permissions to enable database extensions', }, }} > Install pg_partman extension
} /> )} */}
{queueType === 'partitioned' && ( <> ( ms

} />
)} /> ( ms

} />
)} />
)}
setIsClosing(false)} onConfirm={() => onClose()} >

There are unsaved changes. Are you sure you want to close the panel? Your changes will be lost.

{pgPartmanExtension && ( setShowEnableExtensionModal(false)} /> )} ) }