import { zodResolver } from '@hookform/resolvers/zod' import { useRouter } from 'next/router' import { useEffect } from 'react' import { useForm } from 'react-hook-form' import { toast } from 'sonner' import { z } from 'zod' import { useParams } from 'common' import { useCreateCollection } from 'data/analytics/warehouse-collections-create-mutation' import { useCurrentOrgPlan } from 'hooks/misc/useCurrentOrgPlan' import { useSelectedOrganization } from 'hooks/misc/useSelectedOrganization' import { Button, FormControl_Shadcn_, FormField_Shadcn_, FormMessage_Shadcn_, Form_Shadcn_, Input_Shadcn_, Modal, } from 'ui' import { Admonition } from 'ui-patterns/admonition' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' export const CreateWarehouseCollectionModal = ({ open, onOpenChange, }: { open: boolean onOpenChange: (open: boolean) => void }) => { const router = useRouter() const { ref } = useParams() const { plan, isLoading: planLoading } = useCurrentOrgPlan() const currentOrg = useSelectedOrganization() const { mutate: createCollection, isLoading } = useCreateCollection({ onSuccess: (data) => { onOpenChange(false) toast.success('Collection created successfully') router.push(`/project/${ref}/logs/collections/${data.token}`) }, onError: (error) => { toast.error(error.message) }, }) function getMaxRetentionDays() { if (planLoading) return 1 if (plan?.id === 'free') return 3 else return 90 } const FormSchema = z.object({ name: z.string().min(1), retention_days: z.coerce .number() .min(1) .max(getMaxRetentionDays()) .multipleOf(1) .positive() .int(), }) const form = useForm>({ resolver: zodResolver(FormSchema), defaultValues: { retention_days: getMaxRetentionDays(), }, }) useEffect(() => { if (!open) { form.reset() } }, [open, form]) const onSubmit = form.handleSubmit(async (vals) => { if (!ref) { toast.error('Project ref not found') return } createCollection({ projectRef: ref, name: vals.name, retention_days: vals.retention_days, }) }) const formatDays = (days: number) => { if (days === 1) return `1 day` return `${days} days` } const retentionDescription = (days: number) => { const maxRetentionDays = getMaxRetentionDays() if (planLoading) return '' if (!Boolean(days) || days < 1) return <>Your plan allows for a maximum of {formatDays(maxRetentionDays)}. return ( <> Your logs will be removed after {formatDays(days)}.
Your plan allows for a maximum of {formatDays(maxRetentionDays)}. ) } return ( onOpenChange(false)} header="Create collection" visible={open} hideFooter >

An event collection stores time-based data and related information in Supabase's analytics system. You can use SQL to analyze this data without affecting the performance of your main database operations.

( )} /> (
days
)} />
{plan?.id === 'free' && ( <> Upgrade to the pro plan to increase retention to 90 days. } /> )}
) }