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