import { Card } from "@/src/components/ui/card"; import { Input } from "@/src/components/ui/input"; import { api } from "@/src/utils/api"; import type * as z from "zod/v4"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { Form, FormControl, FormField, FormItem, FormMessage, } from "@/src/components/ui/form"; import Header from "@/src/components/layouts/header"; import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture"; import { LockIcon } from "lucide-react"; import { useQueryProject } from "@/src/features/projects/hooks"; import { useSession } from "next-auth/react"; import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess"; import { projectRetentionSchema } from "@/src/features/auth/lib/projectRetentionSchema"; import { ActionButton } from "@/src/components/ActionButton"; import { useHasEntitlement } from "@/src/features/entitlements/hooks"; export default function ConfigureRetention() { const { update: updateSession } = useSession(); const { project } = useQueryProject(); const capture = usePostHogClientCapture(); const hasAccess = useHasProjectAccess({ projectId: project?.id, scope: "project:update", }); const hasEntitlement = useHasEntitlement("data-retention"); const form = useForm({ resolver: zodResolver(projectRetentionSchema), defaultValues: { retention: project?.retentionDays ?? 0, }, }); const setRetention = api.projects.setRetention.useMutation({ onSuccess: (_) => { void updateSession(); }, onError: (error) => form.setError("retention", { message: error.message }), }); function onSubmit(values: z.infer) { if (!hasAccess || !project) return; capture("project_settings:retention_form_submit"); setRetention .mutateAsync({ projectId: project.id, retention: values.retention || null, // Fallback to null for indefinite retention }) .then(() => { form.reset(); }) .catch((error) => { console.error(error); }); } return (

Data retention automatically deletes events older than the specified number of days. The value must be 0 or at least 3 days. Set to 0 to retain data indefinitely. The deletion happens asynchronously, i.e. event may be available for a while after they expired.

{Boolean(form.getValues().retention) && form.getValues().retention !== project?.retentionDays ? (

Your Project's retention will be set from " {project?.retentionDays ?? "Indefinite"} " to " {Number(form.watch("retention")) === 0 ? "Indefinite" : Number(form.watch("retention"))} " days.

) : !Boolean(project?.retentionDays) ? (

Your Project retains data indefinitely.

) : (

Your Project's current retention is " {project?.retentionDays ?? ""} " days.

)}
(
{!hasAccess && ( )}
)} /> Save
); }