"use client"; import { useEffect, useId, useState } from "react"; import { AlertTriangle, Trash2, X } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; type DeleteLearningDangerZoneProps = { learningName: string; description: string; consequences: string[]; deleting: boolean; disabled?: boolean; onDelete: () => void | Promise; }; const REQUIRED_CONFIRMATION = "DELETE"; export function DeleteLearningDangerZone({ learningName, description, consequences, deleting, disabled = false, onDelete, }: DeleteLearningDangerZoneProps) { const [open, setOpen] = useState(false); const [confirmation, setConfirmation] = useState(""); const titleId = useId(); const descriptionId = useId(); const canDelete = confirmation === REQUIRED_CONFIRMATION && !deleting; useEffect(() => { if (!open) return; const onKeyDown = (event: KeyboardEvent) => { if (event.key === "Escape" && !deleting) { setOpen(false); setConfirmation(""); } }; window.addEventListener("keydown", onKeyDown); return () => window.removeEventListener("keydown", onKeyDown); }, [deleting, open]); const close = () => { if (deleting) return; setOpen(false); setConfirmation(""); }; const confirmDelete = async () => { if (!canDelete) return; await onDelete(); }; return ( <>

Danger zone

{description}

{open && (

Delete learning?

This permanently deletes {learningName}.

    {consequences.map((item) => (
  • {item}
  • ))}
  • This cannot be undone.
)} ); }