"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { ActionButton } from "@/components/ui/action-button"; import ActionConfirmingDialog from "@/components/ui/action-confirming-dialog"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { toast } from "@/components/ui/use-toast"; import { zodResolver } from "@hookform/resolvers/zod"; import { AlertTriangle, Eye, EyeOff, Trash2 } from "lucide-react"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { useDeleteAccount, useWhoAmI, } from "@karakeep/shared-react/hooks/users"; import { Button } from "../ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "../ui/card"; const createDeleteAccountSchema = (isLocalUser: boolean) => z.object({ password: isLocalUser ? z.string().min(1, "Password is required") : z.string().optional(), }); export function DeleteAccount() { const router = useRouter(); const [showPassword, setShowPassword] = useState(false); const [isDialogOpen, setIsDialogOpen] = useState(false); const { data: user } = useWhoAmI(); const isLocalUser = user?.localUser ?? false; const deleteAccountSchema = createDeleteAccountSchema(isLocalUser); const form = useForm>({ resolver: zodResolver(deleteAccountSchema), defaultValues: { password: "", }, }); const deleteAccountMutation = useDeleteAccount({ onSuccess: () => { toast({ description: "Your account has been successfully deleted.", }); // Redirect to home page after successful deletion router.push("/"); setIsDialogOpen(false); }, onError: (error) => { if (error.data?.code === "UNAUTHORIZED") { toast({ description: "Invalid password. Please try again.", variant: "destructive", }); } else { toast({ description: "Failed to delete account. Please try again.", variant: "destructive", }); } }, }); const onSubmit = (values: z.infer) => { deleteAccountMutation.mutate({ password: values.password }); }; return ( Danger Zone

Delete Account

Permanently delete your account and all associated data. This action cannot be undone.

This action is irreversible

All your bookmarks, lists, tags, highlights, and other data will be permanently deleted. This cannot be undone.

{isLocalUser && ( ( Enter your password to confirm deletion
)} /> )} } actionButton={() => ( Delete Account )} >
); }