import { zodResolver } from '@hookform/resolvers/zod' import { useParams } from 'common' import { useRef, useState } from 'react' import { useForm } from 'react-hook-form' import { toast } from 'sonner' import { AlertDialog, AlertDialogAction, AlertDialogBody, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, Button, Card, CardContent, Checkbox, Form, FormControl, FormField, } from 'ui' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import { FormLayout } from 'ui-patterns/form/Layout/FormLayout' import { PageSection, PageSectionContent, PageSectionMeta, PageSectionSummary, PageSectionTitle, } from 'ui-patterns/PageSection' import { z } from 'zod' import { useWarehouseReplicatedTargets } from './useWarehouseReplicatedTargets' import { type WarehouseSetupTarget } from './Warehouse.utils' import { AlertError } from '@/components/ui/AlertError' import { useWarehouseDisableMutation } from '@/data/warehouse/warehouse-disable-mutation' import { useTrack } from '@/lib/telemetry/track' const FormSchema = z.object({ shouldDeleteData: z.boolean() }) type FormValues = z.infer const defaultValues: FormValues = { shouldDeleteData: false } export const WarehouseDisableCard = () => { const { ref: projectRef } = useParams() const form = useForm({ resolver: zodResolver(FormSchema), defaultValues }) const track = useTrack() const [isConfirming, setIsConfirming] = useState(false) const replicatedTargets = useWarehouseReplicatedTargets({ projectRef }) // Snapshotted when the user confirms: the mutation's own onSuccess awaits the post-disable // refetches, after which nothing reads as replicated. const targetsAtConfirm = useRef(undefined) const disableMutation = useWarehouseDisableMutation({ onSuccess: async () => { // Tracked from the mutation's onSuccess, not mutateAsync's: the status refetch swaps this // card out of the panel, and mutate-level callbacks are skipped once it has unmounted. const targets = targetsAtConfirm.current track('warehouse_disabled', { ...(targets !== undefined && { schemaTargetCount: targets.filter((target) => target.type === 'schema').length, tableTargetCount: targets.filter((target) => target.type === 'table').length, }), }) setIsConfirming(false) toast.success('Warehouse disable requested') }, onError: () => {}, }) const handleDisable = ({ shouldDeleteData }: FormValues) => { if (!projectRef) return undefined targetsAtConfirm.current = replicatedTargets // Returned so the dialog stays open on failure and closes once the disable succeeds. return disableMutation.mutateAsync({ projectRef, shouldDeleteData }) } const disableError = disableMutation.error const isLegacyStorageError = disableError?.message === 'Legacy Warehouse data must be retained when disabling Warehouse.' return ( Disable Disable Warehouse

Stops replication and disconnects the Warehouse endpoint. Removes its pipeline, publication, catalog access, and foreign tables.

{disableError && !isLegacyStorageError && (
)}
( field.onChange(checked === true)} disabled={disableMutation.isPending} /> )} />
{isLegacyStorageError && (
)}
Cancel { return form.handleSubmit(handleDisable)() }} > Disable Warehouse
) }