import { zodResolver } from '@hookform/resolvers/zod' import { useForm } from 'react-hook-form' import { z } from 'zod' import { Button, FormControl_Shadcn_, FormField_Shadcn_, FormMessage_Shadcn_, Form_Shadcn_, Input_Shadcn_, Modal, } from 'ui' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import { useCurrentOrgPlan } from 'hooks/misc/useCurrentOrgPlan' import { Admonition } from 'ui-patterns' import { useRouter } from 'next/router' import { useSelectedOrganization } from 'hooks/misc/useSelectedOrganization' export interface CollectionFormValues { name: string retention_days: number } interface CollectionFormProps { initialValues?: Partial onSubmit: (values: CollectionFormValues) => void isLoading?: boolean onCancelClick: () => void } export const CollectionForm = ({ initialValues, onSubmit, isLoading, onCancelClick, }: CollectionFormProps) => { const { plan, isLoading: planLoading } = useCurrentOrgPlan() const router = useRouter() const currentOrg = useSelectedOrganization() function getMaxRetentionDays() { if (planLoading) return 1 if (plan?.id === 'free') return 3 else return 90 } const maxRetentionDays = getMaxRetentionDays() const FormSchema = z.object({ name: z.string().min(1), retention_days: z.coerce.number().min(1).max(maxRetentionDays).multipleOf(1).positive().int(), }) const form = useForm({ resolver: zodResolver(FormSchema), defaultValues: { name: initialValues?.name || '', retention_days: initialValues?.retention_days || maxRetentionDays, }, }) const handleSubmit = form.handleSubmit(onSubmit) const formatDays = (days: number) => { return days === 1 ? `1 day` : `${days} days` } const retentionDescription = (days: number) => { 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 (
( )} /> (
days
)} />
{plan?.id === 'free' && ( <> Upgrade to the pro plan to increase retention to 90 days. } /> )}
) }