|
| 1 | +import { useState } from "react"; |
| 2 | +import { useTransitionPresence } from "@chopin/editor/transition-presence"; |
| 3 | + |
| 4 | +import { DocumentActionsMenu } from "../document-actions-menu"; |
| 5 | +import { motionImmediately } from "../motion-input"; |
| 6 | +import { NavigationDialog } from "../navigation-dialog"; |
| 7 | +import { TerminalAlert } from "../terminal-alert"; |
| 8 | +import { AuditPlate } from "./frame"; |
| 9 | + |
| 10 | +import type { FormEvent } from "react"; |
| 11 | +import type { DocumentAction } from "../document-actions-menu"; |
| 12 | + |
| 13 | +export function InteractiveSpecimens() { |
| 14 | + let [title, setTitle] = useState("Design system audit"); |
| 15 | + let [draft, setDraft] = useState(title); |
| 16 | + let [archived, setArchived] = useState(false); |
| 17 | + let [deleted, setDeleted] = useState(false); |
| 18 | + let [dialog, setDialog] = useState<"rename" | "delete" | undefined>(); |
| 19 | + let [failure, setFailure] = useState(false); |
| 20 | + let [error, setError] = useState(false); |
| 21 | + let presence = useTransitionPresence(dialog, 220, motionImmediately()); |
| 22 | + |
| 23 | + function action(value: DocumentAction) { |
| 24 | + if (value === "archive" || value === "restore") { |
| 25 | + setArchived(value === "archive"); |
| 26 | + return; |
| 27 | + } |
| 28 | + setDraft(title); |
| 29 | + setError(false); |
| 30 | + setDialog(value); |
| 31 | + } |
| 32 | + |
| 33 | + function save(event: FormEvent<HTMLFormElement>) { |
| 34 | + event.preventDefault(); |
| 35 | + if (!draft.trim()) return; |
| 36 | + if (failure) { |
| 37 | + setError(true); |
| 38 | + return; |
| 39 | + } |
| 40 | + setTitle(draft.trim()); |
| 41 | + setDialog(undefined); |
| 42 | + } |
| 43 | + |
| 44 | + return ( |
| 45 | + <AuditPlate |
| 46 | + description="Open the production menu and modal shell. Changes stay in this local specimen." |
| 47 | + item="interactive-document-actions" |
| 48 | + title="Interactive document actions" |
| 49 | + > |
| 50 | + <div className="flex flex-wrap items-center gap-3"> |
| 51 | + <span className="text-sm font-medium">{deleted ? "Document deleted" : title}</span> |
| 52 | + {deleted |
| 53 | + ? ( |
| 54 | + <button |
| 55 | + className="btn btn-md btn-secondary" |
| 56 | + onClick={() => { |
| 57 | + setTitle("Design system audit"); |
| 58 | + setArchived(false); |
| 59 | + setDeleted(false); |
| 60 | + }} |
| 61 | + type="button" |
| 62 | + > |
| 63 | + Reset specimen |
| 64 | + </button> |
| 65 | + ) |
| 66 | + : ( |
| 67 | + <DocumentActionsMenu |
| 68 | + channel={{ |
| 69 | + title, |
| 70 | + archivedAt: archived ? "2026-09-28T00:00:00.000Z" : undefined, |
| 71 | + }} |
| 72 | + className="btn btn-md btn-secondary" |
| 73 | + onAction={action} |
| 74 | + trigger="Document actions" |
| 75 | + /> |
| 76 | + )} |
| 77 | + <label className="flex items-center gap-2 text-sm text-text-secondary"> |
| 78 | + <input |
| 79 | + checked={failure} |
| 80 | + className="choice-control" |
| 81 | + onChange={event => setFailure(event.target.checked)} |
| 82 | + type="checkbox" |
| 83 | + /> |
| 84 | + Simulate save error |
| 85 | + </label> |
| 86 | + </div> |
| 87 | + {presence.phase !== "closed" && presence.value === "rename" && ( |
| 88 | + <NavigationDialog |
| 89 | + motion={presence} |
| 90 | + onDismiss={() => setDialog(undefined)} |
| 91 | + title="Rename document" |
| 92 | + > |
| 93 | + <form className="mt-4 flex min-w-0 flex-col gap-2" onSubmit={save}> |
| 94 | + <label className="sr-only" htmlFor="audit-interactive-title">Document title</label> |
| 95 | + <input |
| 96 | + aria-invalid={error || undefined} |
| 97 | + className="field h-8 min-w-0 w-full px-2 text-sm" |
| 98 | + id="audit-interactive-title" |
| 99 | + maxLength={120} |
| 100 | + onChange={event => { |
| 101 | + setDraft(event.target.value); |
| 102 | + setError(false); |
| 103 | + }} |
| 104 | + value={draft} |
| 105 | + /> |
| 106 | + {error && ( |
| 107 | + <TerminalAlert className="text-sm text-destructive-ink"> |
| 108 | + Could not rename document. |
| 109 | + </TerminalAlert> |
| 110 | + )} |
| 111 | + <div className="flex justify-end gap-2"> |
| 112 | + <button |
| 113 | + className="btn btn-md btn-ghost" |
| 114 | + onClick={() => setDialog(undefined)} |
| 115 | + type="button" |
| 116 | + > |
| 117 | + Cancel |
| 118 | + </button> |
| 119 | + <button className="btn btn-md btn-primary" disabled={!draft.trim()} type="submit"> |
| 120 | + Save |
| 121 | + </button> |
| 122 | + </div> |
| 123 | + </form> |
| 124 | + </NavigationDialog> |
| 125 | + )} |
| 126 | + {presence.phase !== "closed" && presence.value === "delete" && ( |
| 127 | + <NavigationDialog |
| 128 | + motion={presence} |
| 129 | + onDismiss={() => setDialog(undefined)} |
| 130 | + title="Delete document permanently?" |
| 131 | + > |
| 132 | + <p className="mt-3 text-sm text-text-secondary"> |
| 133 | + <strong className="font-semibold text-text-primary">{title}</strong>{" "} |
| 134 | + will be permanently deleted. This cannot be undone. |
| 135 | + </p> |
| 136 | + <div className="mt-5 flex justify-end gap-2"> |
| 137 | + <button |
| 138 | + className="btn btn-md btn-secondary" |
| 139 | + onClick={() => setDialog(undefined)} |
| 140 | + type="button" |
| 141 | + > |
| 142 | + Cancel |
| 143 | + </button> |
| 144 | + <button |
| 145 | + className="btn btn-md btn-destructive" |
| 146 | + onClick={() => { |
| 147 | + setDeleted(true); |
| 148 | + setDialog(undefined); |
| 149 | + }} |
| 150 | + type="button" |
| 151 | + > |
| 152 | + Delete permanently |
| 153 | + </button> |
| 154 | + </div> |
| 155 | + </NavigationDialog> |
| 156 | + )} |
| 157 | + </AuditPlate> |
| 158 | + ); |
| 159 | +} |
0 commit comments