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