Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PullRequestMarkdownEditor.tsx

120 lines3.9 KB

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​
59 return (
60 <div
61 className={cn("space-y-2", className)}
62 onKeyDown={(event) => {
63 if (event.key !== "Escape" || saving) return;
64 event.preventDefault();
65 onCancel();
66 }}
67 >
68 <ToggleGroup
69 aria-label="Markdown editor mode"
70 variant="segmented"
71 value={[preview ? "preview" : "write"]}
72 disabled={saving}
73 onValueChange={(next) => {
74 const mode = next[0];
75 if (mode === "write" || mode === "preview") setPreview(mode === "preview");
76 }}
77 >
78 <Toggle value="write">Write</Toggle>
79 <Toggle value="preview">Preview</Toggle>
80 </ToggleGroup>
81 {preview ? (
82 <div className="rounded-lg border border-border/60 px-3 py-2">
83 {empty ? (
84 <p className="text-xs text-muted-foreground">Nothing to preview.</p>
85 ) : (
86 <PullRequestMarkdown
87 text={draft}
88 cwd={cwd}
89 environmentId={environmentId}
90 threadRef={threadRef}
91 />
92 )}
93 </div>
94 ) : (
95 <Textarea
96 autoFocus
97 disabled={saving}
98 value={draft}
99 rows={6}
100 placeholder={placeholder}
101 aria-label={label}
102 onChange={(event) => setDraft(event.target.value)}
103 />
104 )}
105 <div className="flex justify-end gap-2">
106 <Button size="xs" variant="ghost" disabled={saving} onClick={onCancel}>
107 Cancel
108 </Button>
109 <Button
110 size="xs"
111 variant="outline"
112 disabled={saving || (empty && !allowEmpty)}
113 onClick={() => onSave(draft)}
114 >
115 {saving ? "Saving..." : "Save"}
116 </Button>
117 </div>
118 </div>
119 );