| 1 | import { useState } from "react"; |
| 2 | |
| 3 | import { |
| 4 | AlertDialog, |
| 5 | AlertDialogAction, |
| 6 | AlertDialogCancel, |
| 7 | AlertDialogContent, |
| 8 | AlertDialogDescription, |
| 9 | AlertDialogFooter, |
| 10 | AlertDialogHeader, |
| 11 | AlertDialogTitle, |
| 12 | } from "../ui/alert-dialog"; |
| 13 | |
| 14 | /** |
| 15 | * Confirms clearing your conversation with @g1t (docs.g1t.sh/guides/ask/, |
| 16 | * "Clear the conversation"): the Ask panel's menu, the direct message's |
| 17 | * menu in Chat, and `/clear` typed in either open it. `onClear` deletes |
| 18 | * it; the dialog stays open, saying what went wrong, if that fails. |
| 19 | */ |
| 20 | export function ClearG1tDialog({ |
| 21 | open, |
| 22 | onOpenChange, |
| 23 | onClear, |
| 24 | }: { |
| 25 | open: boolean; |
| 26 | onOpenChange: (open: boolean) => void; |
| 27 | /** Clears it: null when done, else what went wrong. */ |
| 28 | onClear: () => Promise<string | null>; |
| 29 | }) { |
| 30 | const [busy, setBusy] = useState(false); |
| 31 | const [error, setError] = useState<string | null>(null); |
| 32 | return ( |
| 33 | <AlertDialog |
| 34 | open={open} |
| 35 | onOpenChange={(next) => { |
| 36 | if (busy) return; |
| 37 | setError(null); |
| 38 | onOpenChange(next); |
| 39 | }} |
| 40 | > |
| 41 | <AlertDialogContent> |
| 42 | <AlertDialogHeader> |
| 43 | <AlertDialogTitle>Clear your conversation with g1t?</AlertDialogTitle> |
| 44 | <AlertDialogDescription> |
| 45 | Everything you and g1t said is deleted for good, in Ask and in Chat: your questions, g1t's answers with their steps and |
| 46 | sources, and the notifications they sent you. g1t won't remember any of it. An answer still coming is stopped and thrown |
| 47 | away. This can't be undone. |
| 48 | </AlertDialogDescription> |
| 49 | </AlertDialogHeader> |
| 50 | {error && ( |
| 51 | <p role="alert" className="text-sm text-danger"> |
| 52 | {error} |
| 53 | </p> |
| 54 | )} |
| 55 | <AlertDialogFooter> |
| 56 | <AlertDialogCancel disabled={busy}>Cancel</AlertDialogCancel> |
| 57 | <AlertDialogAction |
| 58 | disabled={busy} |
| 59 | onClick={(event) => { |
| 60 | // Closed only once it is done. |
| 61 | event.preventDefault(); |
| 62 | setBusy(true); |
| 63 | setError(null); |
| 64 | void onClear().then((problem) => { |
| 65 | setBusy(false); |
| 66 | if (problem) setError(problem); |
| 67 | else onOpenChange(false); |
| 68 | }); |
| 69 | }} |
| 70 | > |
| 71 | {busy ? "Clearing…" : "Clear conversation"} |
| 72 | </AlertDialogAction> |
| 73 | </AlertDialogFooter> |
| 74 | </AlertDialogContent> |
| 75 | </AlertDialog> |
| 76 | ); |
| 77 | } |