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 { Toggle, ToggleGroup } from "../ui/toggle-group";
13import { PullRequestMarkdown } from "./PullRequestMarkdown";
14import {
15 PullRequestMarkdownTextarea,
16 type PullRequestCompletionScope,
17} from "./PullRequestMarkdownTextarea";
18
19export function PullRequestMarkdownEditor({
20 value,
21 cwd,
22 environmentId,
23 completionScope,
24 threadRef = null,
25 placeholder,
26 label,
27 saving,
28 allowEmpty = false,
29 className,
30 onSave,
31 onCancel,
32}: {
33 readonly value: string;
34 readonly cwd: string;
35 readonly environmentId: EnvironmentId;
36 readonly completionScope: PullRequestCompletionScope | null;
37 readonly threadRef?: ScopedThreadRef | null;
38 readonly placeholder?: string | undefined;
39 readonly label: string;
40 readonly saving: boolean;
41 readonly allowEmpty?: boolean;
42 readonly className?: string | undefined;
43 readonly onSave: (next: string) => void;
44 readonly onCancel: () => void;
45}) {
46 const canWriteSourceControl = useEnvironmentScope(environmentId, AuthSourceControlWriteScope);
47 const [draft, setDraft] = useState(value);
48 const [preview, setPreview] = useState(false);
49 const [seed, setSeed] = useState(value);
50 if (seed !== value) {
51 setSeed(value);
52 setDraft(value);
53 }
54 const empty = draft.trim().length === 0;
55 const saveDisabled = !canWriteSourceControl || saving || (empty && !allowEmpty);
56
57 return (
58 <div
59 className={cn("space-y-2", className)}
60 onKeyDown={(event) => {
61 if (event.nativeEvent.isComposing || event.keyCode === 229) return;
62 if (
63 event.key === "Enter" &&
64 (event.metaKey || event.ctrlKey) &&
65 !event.shiftKey &&
66 !event.altKey
67 ) {
68 event.preventDefault();
69 event.stopPropagation();
70 if (
71 !saveDisabled &&
72 !event.repeat &&
73 readEnvironmentScope(environmentId, AuthSourceControlWriteScope)
74 )
75 onSave(draft);
76 return;
77 }
78 if (event.key !== "Escape" || saving) return;
79 event.preventDefault();
80 onCancel();
81 }}
82 >
83 <ToggleGroup
84 aria-label="Markdown editor mode"
85 variant="segmented"
86 value={[preview ? "preview" : "write"]}
87 disabled={saving}
88 onValueChange={(next) => {
89 const mode = next[0];
90 if (mode === "write" || mode === "preview") setPreview(mode === "preview");
91 }}
92 >
93 <Toggle value="write">Write</Toggle>
94 <Toggle value="preview">Preview</Toggle>
95 </ToggleGroup>
96 {preview ? (
97 <div className="rounded-lg border border-border/60 px-3 py-2">
98 {empty ? (
99 <p className="text-xs text-muted-foreground">Nothing to preview.</p>
100 ) : (
101 <PullRequestMarkdown
102 text={draft}
103 cwd={cwd}
104 environmentId={environmentId}
105 threadRef={threadRef}
106 />
107 )}
108 </div>
109 ) : (
110 <PullRequestMarkdownTextarea
111 autoFocus
112 disabled={saving}
113 completionScope={completionScope}
114 value={draft}
115 rows={6}
116 placeholder={placeholder}
117 aria-label={label}
118 onValueChange={setDraft}
119 />
120 )}
121 <div className="flex justify-end gap-2">
122 <Button size="xs" variant="ghost" disabled={saving} onClick={onCancel}>
123 Cancel
124 </Button>
125 <Button
126 size="xs"
127 variant="outline"
128 disabled={!canWriteSourceControl || saving || (empty && !allowEmpty)}
129 onClick={() => {
130 if (
131 !readEnvironmentScope(environmentId, AuthSourceControlWriteScope) ||
132 saving ||
133 (empty && !allowEmpty)
134 )
135 return;
136 onSave(draft);
137 }}
138 >
139 {saving ? "Saving..." : "Save"}
140 </Button>
141 </div>
142 </div>
143 );
144}