Open History to see the last commit
1import { useState } from "react";
2import {
3 AuthSourceControlWriteScope,
4 type EnvironmentId,
5 type ScopedThreadRef,
6} from "@t3tools/contracts";
7
8import { cn } from "~/lib/utils";
9import { readEnvironmentScope, useEnvironmentScope } from "~/state/session";
10
11import { Button } from "../ui/button";
12import { Textarea } from "../ui/textarea";
13import { Toggle, ToggleGroup } from "../ui/toggle-group";
14import { PullRequestMarkdown } from "./PullRequestMarkdown";
15
16export function PullRequestMarkdownEditor({
17 value,
18 cwd,
19 environmentId,
20 threadRef = null,
21 placeholder,
22 label,
23 saving,
24 allowEmpty = false,
25 className,
26 onSave,
27 onCancel,
28}: {
29 readonly value: string;
30 readonly cwd: string;
31 readonly environmentId: EnvironmentId;
32 readonly threadRef?: ScopedThreadRef | null;
33 readonly placeholder?: string | undefined;
34 readonly label: string;
35 readonly saving: boolean;
36 readonly allowEmpty?: boolean;
37 readonly className?: string | undefined;
38 readonly onSave: (next: string) => void;
39 readonly onCancel: () => void;
40}) {
41 const canWriteSourceControl = useEnvironmentScope(environmentId, AuthSourceControlWriteScope);
42 const [draft, setDraft] = useState(value);
43 const [preview, setPreview] = useState(false);
44 // React keeps a component instance wherever the same position and key come round again, so an
45 // editor opened on one remark can be handed another's words without being rebuilt — and saving
46 // would then write the first remark's text onto the second.
47 const [seed, setSeed] = useState(value);
48 if (seed !== value) {
49 setSeed(value);
50 setDraft(value);
51 }
52 const empty = draft.trim().length === 0;
53 const saveDisabled = !canWriteSourceControl || saving || (empty && !allowEmpty);
54
55 return (
56 <div
57 className={cn("space-y-2", className)}
58 onKeyDown={(event) => {
59 if (event.nativeEvent.isComposing || event.keyCode === 229) return;
60 if (
61 event.key === "Enter" &&
62 (event.metaKey || event.ctrlKey) &&
63 !event.shiftKey &&
64 !event.altKey
65 ) {
66 event.preventDefault();
67 event.stopPropagation();
68 if (
69 !saveDisabled &&
70 !event.repeat &&
71 readEnvironmentScope(environmentId, AuthSourceControlWriteScope)
72 )
73 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
123 size="xs"
124 variant="outline"
125 disabled={!canWriteSourceControl || saving || (empty && !allowEmpty)}
126 onClick={() => {
127 if (
128 !readEnvironmentScope(environmentId, AuthSourceControlWriteScope) ||
129 saving ||
130 (empty && !allowEmpty)
131 )
132 return;
133 onSave(draft);
134 }}
135 >
136 {saving ? "Saving..." : "Save"}
137 </Button>
138 </div>
139 </div>
140 );
141}