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 const [seed, setSeed] = useState(value);
45 if (seed !== value) {
46 setSeed(value);
47 setDraft(value);
48 }
49 const empty = draft.trim().length === 0;
50 const saveDisabled = !canWriteSourceControl || saving || (empty && !allowEmpty);
51
52 return (
53 <div
54 className={cn("space-y-2", className)}
55 onKeyDown={(event) => {
56 if (event.nativeEvent.isComposing || event.keyCode === 229) return;
57 if (
58 event.key === "Enter" &&
59 (event.metaKey || event.ctrlKey) &&
60 !event.shiftKey &&
61 !event.altKey
62 ) {
63 event.preventDefault();
64 event.stopPropagation();
65 if (
66 !saveDisabled &&
67 !event.repeat &&
68 readEnvironmentScope(environmentId, AuthSourceControlWriteScope)
69 )
70 onSave(draft);
71 return;
72 }
73 if (event.key !== "Escape" || saving) return;
74 event.preventDefault();
75 onCancel();
76 }}
77 >
78 <ToggleGroup
79 aria-label="Markdown editor mode"
80 variant="segmented"
81 value={[preview ? "preview" : "write"]}
82 disabled={saving}
83 onValueChange={(next) => {
84 const mode = next[0];
85 if (mode === "write" || mode === "preview") setPreview(mode === "preview");
86 }}
87 >
88 <Toggle value="write">Write</Toggle>
89 <Toggle value="preview">Preview</Toggle>
90 </ToggleGroup>
91 {preview ? (
92 <div className="rounded-lg border border-border/60 px-3 py-2">
93 {empty ? (
94 <p className="text-xs text-muted-foreground">Nothing to preview.</p>
95 ) : (
96 <PullRequestMarkdown
97 text={draft}
98 cwd={cwd}
99 environmentId={environmentId}
100 threadRef={threadRef}
101 />
102 )}
103 </div>
104 ) : (
105 <Textarea
106 autoFocus
107 disabled={saving}
108 value={draft}
109 rows={6}
110 placeholder={placeholder}
111 aria-label={label}
112 onChange={(event) => setDraft(event.target.value)}
113 />
114 )}
115 <div className="flex justify-end gap-2">
116 <Button size="xs" variant="ghost" disabled={saving} onClick={onCancel}>
117 Cancel
118 </Button>
119 <Button
120 size="xs"
121 variant="outline"
122 disabled={!canWriteSourceControl || saving || (empty && !allowEmpty)}
123 onClick={() => {
124 if (
125 !readEnvironmentScope(environmentId, AuthSourceControlWriteScope) ||
126 saving ||
127 (empty && !allowEmpty)
128 )
129 return;
130 onSave(draft);
131 }}
132 >
133 {saving ? "Saving..." : "Save"}
134 </Button>
135 </div>
136 </div>
137 );
138}