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

PullRequestMarkdownEditor.tsx

116 lines3.6 KB

1import { useState } from "react";
2import type { EnvironmentId } 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 placeholder,
23 label,
24 saving,
25 allowEmpty = false,
26 className,
27 onSave,
28 onCancel,
29}: {
30 readonly value: string;
31 readonly cwd: string;
32 readonly environmentId: EnvironmentId;
33 readonly placeholder?: string | undefined;
34 readonly label: string;
35 readonly saving: boolean;
36 /** A description may be cleared, which is how one is removed; a remark may not be emptied. */
37 readonly allowEmpty?: boolean;
38 readonly className?: string | undefined;
39 readonly onSave: (next: string) => void;
40 readonly onCancel: () => void;
41}) {
42 const [draft, setDraft] = useState(value);
43 const [preview, setPreview] = useState(false);
44 // The words this draft started from. React keeps a component instance wherever the same
45 // position and key come round again, so an editor opened on one remark can be handed another's
46 // words without being rebuilt — and saving would then write the first remark's text onto the
47 // second. Different words mean a different subject, and the draft starts again from them.
48 const [seed, setSeed] = useState(value);
49 if (seed !== value) {
50 setSeed(value);
51 setDraft(value);
52 }
53 const empty = draft.trim().length === 0;
54​
55 return (
56 <div
57 className={cn("space-y-2", className)}
58 onKeyDown={(event) => {
59 if (event.key !== "Escape" || saving) return;
60 event.preventDefault();
61 onCancel();
62 }}
63 >
64 <div className="flex items-center gap-1">
65 <Button
66 size="xs"
67 variant={preview ? "ghost" : "outline"}
68 disabled={saving}
69 onClick={() => setPreview(false)}
70 >
71 Write
72 </Button>
73 <Button
74 size="xs"
75 variant={preview ? "outline" : "ghost"}
76 disabled={saving}
77 onClick={() => setPreview(true)}
78 >
79 Preview
80 </Button>
81 </div>
82 {preview ? (
83 <div className="rounded-lg border border-border/60 px-3 py-2">
84 {empty ? (
85 <p className="text-xs text-muted-foreground">Nothing to preview.</p>
86 ) : (
87 <PullRequestMarkdown text={draft} cwd={cwd} environmentId={environmentId} />
88 )}
89 </div>
90 ) : (
91 <Textarea
92 autoFocus
93 disabled={saving}
94 value={draft}
95 rows={6}
96 placeholder={placeholder}
97 aria-label={label}
98 onChange={(event) => setDraft(event.target.value)}
99 />
100 )}
101 <div className="flex justify-end gap-2">
102 <Button size="xs" variant="ghost" disabled={saving} onClick={onCancel}>
103 Cancel
104 </Button>
105 <Button
106 size="xs"
107 variant="outline"
108 disabled={saving || (empty && !allowEmpty)}
109 onClick={() => onSave(draft)}
110 >
111 {saving ? "Saving..." : "Save"}
112 </Button>
113 </div>
114 </div>
115 );