Open History to see the last commit
1import { useState } from "react";
2
3import { cn } from "~/lib/utils";
4
5import { Button } from "../ui/button";
6import { Textarea } from "../ui/textarea";
7import { PullRequestMarkdown } from "./PullRequestMarkdown";
8
9/**
10 * The box a body is rewritten in — a description, or a remark already posted. It owns the draft
11 * and nothing else: the caller sends the request and says whether it is still in flight, so the
12 * same box serves every mutation without knowing which one it is.
13 *
14 * Preview renders through the same component the saved body will be read through, which is the
15 * only way to see what a host's markdown will actually become before it is sent.
16 */
17export function PullRequestMarkdownEditor({
18 value,
19 cwd,
20 placeholder,
21 label,
22 saving,
23 allowEmpty = false,
24 className,
25 onSave,
26 onCancel,
27}: {
28 readonly value: string;
29 readonly cwd: string;
30 readonly placeholder?: string | undefined;
31 readonly label: string;
32 readonly saving: boolean;
33 /** A description may be cleared, which is how one is removed; a remark may not be emptied. */
34 readonly allowEmpty?: boolean;
35 readonly className?: string | undefined;
36 readonly onSave: (next: string) => void;
37 readonly onCancel: () => void;
38}) {
39 const [draft, setDraft] = useState(value);
40 const [preview, setPreview] = useState(false);
41 // The words this draft started from. React keeps a component instance wherever the same
42 // position and key come round again, so an editor opened on one remark can be handed another's
43 // words without being rebuilt — and saving would then write the first remark's text onto the
44 // second. Different words mean a different subject, and the draft starts again from them.
45 const [seed, setSeed] = useState(value);
46 if (seed !== value) {
47 setSeed(value);
48 setDraft(value);
49 }
50 const empty = draft.trim().length === 0;
51
52 return (
53 <div
54 className={cn("space-y-2", className)}
55 onKeyDown={(event) => {
56 if (event.key !== "Escape" || saving) return;
57 event.preventDefault();
58 onCancel();
59 }}
60 >
61 <div className="flex items-center gap-1">
62 <Button
63 size="xs"
64 variant={preview ? "ghost" : "outline"}
65 disabled={saving}
66 onClick={() => setPreview(false)}
67 >
68 Write
69 </Button>
70 <Button
71 size="xs"
72 variant={preview ? "outline" : "ghost"}
73 disabled={saving}
74 onClick={() => setPreview(true)}
75 >
76 Preview
77 </Button>
78 </div>
79 {preview ? (
80 <div className="rounded-lg border border-border/60 px-3 py-2">
81 {empty ? (
82 <p className="text-xs text-muted-foreground">Nothing to preview.</p>
83 ) : (
84 <PullRequestMarkdown text={draft} cwd={cwd} />
85 )}
86 </div>
87 ) : (
88 <Textarea
89 autoFocus
90 disabled={saving}
91 value={draft}
92 rows={6}
93 placeholder={placeholder}
94 aria-label={label}
95 onChange={(event) => setDraft(event.target.value)}
96 />
97 )}
98 <div className="flex justify-end gap-2">
99 <Button size="xs" variant="ghost" disabled={saving} onClick={onCancel}>
100 Cancel
101 </Button>
102 <Button
103 size="xs"
104 variant="outline"
105 disabled={saving || (empty && !allowEmpty)}
106 onClick={() => onSave(draft)}
107 >
108 {saving ? "Saving..." : "Save"}
109 </Button>
110 </div>
111 </div>
112 );
113}