import { useEffect, useState, type ReactNode } from "react"
import { Button } from "../components/ui/button"
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "../components/ui/dialog"
import { Input } from "../components/ui/input"
import { SpinnerIcon } from "../components/ui/spinner-icon"
import { cn } from "./utils"
type TypeToConfirmFieldProps = {
labelWord: string
value: string
onChange: (value: string) => void
disabled?: boolean
className?: string
}
/** Requires the admin to type an exact confirmation word (case-insensitive). */
export function TypeToConfirmField({
labelWord,
value,
onChange,
disabled,
className,
}: TypeToConfirmFieldProps) {
const expected = labelWord.trim().toUpperCase()
return (
onChange(e.target.value)}
disabled={disabled}
placeholder={expected}
autoComplete="off"
spellCheck={false}
onCopy={(e) => e.preventDefault()}
onCut={(e) => e.preventDefault()}
onPaste={(e) => e.preventDefault()}
onDrop={(e) => e.preventDefault()}
className="rounded-[6px] font-mono uppercase tracking-wide"
aria-label={`Type ${expected} to confirm`}
/>
)
}
export function matchesConfirmWord(value: string, expectedWord: string): boolean {
return value.trim().toUpperCase() === expectedWord.trim().toUpperCase()
}
type TypeToConfirmDialogProps = {
open: boolean
onOpenChange: (open: boolean) => void
title: string
description?: ReactNode
confirmWord: string
confirmLabel?: string
cancelLabel?: string
confirming?: boolean
variant?: "default" | "destructive"
onConfirm: () => void | Promise
}
/** Separate “Are you sure?” modal that requires typing a confirmation word. */
export function TypeToConfirmDialog({
open,
onOpenChange,
title,
description,
confirmWord,
confirmLabel = "Confirm",
cancelLabel = "Cancel",
confirming = false,
variant = "default",
onConfirm,
}: TypeToConfirmDialogProps) {
const [confirmText, setConfirmText] = useState("")
const confirmMatched = matchesConfirmWord(confirmText, confirmWord)
useEffect(() => {
if (!open) return
setConfirmText("")
}, [open, confirmWord])
const handleConfirm = async () => {
if (!confirmMatched || confirming) return
await onConfirm()
}
return (
)
}