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

IssueCommentComposer.tsx

270 lines10.9 KB

1import { useGT } from "gt-react-native";
2import { useRef, useState } from "react";
3import { Platform, View } from "react-native";
4import Animated from "react-native-reanimated";
5​
6import {
7 ComposerEditor,
8 type ComposerEditorHandle,
9 type ComposerEditorSelection,
10} from "../../components/ComposerEditor";
11import {
12 COMPOSER_BOTTOM_GUTTER,
13 ComposerSurface,
14 INLINE_EDITOR_MIN_WIDTH,
15 useCompactComposerEditor,
16 useComposerChrome,
17 useComposerTextStyle,
18} from "../../components/ComposerSurface";
19import { ComposerActionButton, ComposerToolbarRow } from "../../components/ComposerToolbar";
20import { AppText as Text } from "../../components/ui/AppText";
21import { useComposerTextRole } from "../settings/appearance/useScaledTextRole";
22import {
23 ComposerDictationCancelAction,
24 ComposerDictationDraftContent,
25 ComposerDictationError,
26 ComposerDictationPrimaryAction,
27 ComposerDictationStartAction,
28 ComposerDictationStatus,
29 ComposerDictationToolbar,
30} from "../voice-input/ComposerDictationControl";
31import { useVoiceInputController } from "../voice-input/useVoiceInputController";
32import { resolveVoiceComposerPresentation } from "../voice-input/voiceInputPresentation";
33​
34// The thread composer's editor starts after its attachment button; this keeps the same text inset.
35const EDITOR_LEADING_INSET = 14;
36​
37/**
38 * The comment box at the foot of an issue, in the thread composer's chrome: one row that grows
39 * with the draft, dictation, and a round send button. `blocked` replaces the editor with the reason.
40 */
41export function IssueCommentComposer(props: {
42 /** Names this draft to dictation, so a recording started here lands here. */
43 readonly ownerKey: string;
44 readonly label: string;
45 readonly blocked: string | null;
46 readonly posting: boolean;
47 readonly bottomInset: number;
48 readonly contentMaxWidth?: number;
49 /** Resolves true once the comment is posted, which clears the draft. */
50 readonly onSubmit: (body: string) => Promise<boolean>;
51 readonly onHeightChange: (height: number) => void;
52 readonly onFocusChange: (focused: boolean) => void;
53}) {
54 const gt = useGT();
55 const editorRef = useRef<ComposerEditorHandle>(null);
56 const [draft, setDraft] = useState("");
57 // Read by dictation and by send after an await, when the render's `draft` may be stale.
58 const latestDraft = useRef(draft);
59 const changeDraft = (text: string) => {
60 latestDraft.current = text;
61 setDraft(text);
62 };
63 const [selection, setSelection] = useState<ComposerEditorSelection>({ start: 0, end: 0 });
64 const bodyText = useComposerTextRole();
65 const textStyle = useComposerTextStyle(bodyText);
66 const chrome = useComposerChrome();
67 const [focused, setFocused] = useState(false);
68 // Animate only while typing, as the thread composer does; a restored draft lands in place.
69 const compactEditor = useCompactComposerEditor(bodyText, draft, Platform.OS === "ios" && focused);
70 const voiceInput = useVoiceInputController({
71 ownerKey: props.ownerKey,
72 label: props.label,
73 readDraftMessage: () => latestDraft.current,
74 selection,
75 disabled: props.blocked !== null,
76 hidden: props.blocked !== null,
77 onChangeDraftMessage: changeDraft,
78 onChangeSelection: setSelection,
79 });
80 const voicePresentation = resolveVoiceComposerPresentation(
81 voiceInput.state,
82 voiceInput.elapsedSeconds,
83 );
84 const dictating = voicePresentation.statusKind === "active";
85 const locked = props.posting || voiceInput.blocksSubmission;
86 const canSend = props.blocked === null && !locked && draft.trim().length > 0;
87​
88 const sending = useRef(false);
89 const flushAndSubmit = async () => {
90 await editorRef.current?.flushPendingInput();
91 const submitted = latestDraft.current;
92 if (submitted.trim().length === 0) return;
93 if ((await props.onSubmit(submitted)) && latestDraft.current === submitted) changeDraft("");
94 };
95 const send = () => {
96 if (!canSend || sending.current) return Promise.resolve();
97 sending.current = true;
98 // `finally` on the promise: the React Compiler does not compile a `try` without a `catch`.
99 return flushAndSubmit().finally(() => {
100 sending.current = false;
101 });
102 };
103 const sendButton = (
104 <ComposerActionButton
105 size={chrome.controlSize}
106 accessibilityLabel={props.posting ? gt("Posting comment") : gt("Post comment")}
107 icon="arrow.up"
108 variant="primary"
109 disabled={!canSend}
110 onPress={() => void send()}
111 />
112 );
113​
114 return (
115 <Animated.View
116 onLayout={(event) => props.onHeightChange(event.nativeEvent.layout.height)}
117 className={Platform.OS === "android" ? "bg-screen px-[12px]" : "px-[12px]"}
118 style={{ paddingTop: 6, paddingBottom: props.bottomInset + COMPOSER_BOTTOM_GUTTER }}
119 >
120 <View
121 className={
122 Platform.OS === "android"
123 ? "hidden"
124 : "absolute inset-0 bg-linear-to-b from-screen/0 via-screen/60 to-screen/90"
125 }
126 pointerEvents="none"
127 />
128 <View className="w-full self-center" style={{ maxWidth: props.contentMaxWidth }}>
129 <ComposerSurface
130 animateLayout={false}
131 style={{
132 borderRadius: chrome.borderRadius,
133 borderCurve: "circular",
134 overflow: "hidden",
135 paddingVertical: chrome.paddingVertical,
136 }}
137 >
138 {props.blocked !== null ? (
139 <View className="h-[44px] flex-row items-center">
140 <Text
141 className="min-w-0 flex-1 text-base text-foreground-muted"
142 style={{ paddingLeft: EDITOR_LEADING_INSET }}
143 numberOfLines={1}
144 ellipsizeMode="tail"
145 >
146 {props.blocked}
147 </Text>
148 {sendButton}
149 </View>
150 ) : (
151 <>
152 <ComposerDictationError
153 error={voiceInput.state.phase === "error" ? voiceInput.state.error : null}
154 onDismiss={voiceInput.cancel}
155 />
156 <View onLayout={compactEditor.onRowLayout}>
157 <ComposerDictationDraftContent
158 className="flex-row items-end"
159 compact
160 hidden={dictating}
161 >
162 <View style={{ width: EDITOR_LEADING_INSET - 8 }} />
163 <Animated.View
164 className="min-w-0 flex-1"
165 onLayout={compactEditor.onEditorLayout}
166 style={[
167 { minWidth: compactEditor.isExpanded ? 0 : INLINE_EDITOR_MIN_WIDTH },
168 compactEditor.containerStyle,
169 compactEditor.isExpanded ? { flex: 0, width: 0 } : undefined,
170 ]}
171 />
172 {/* TalkBack orders siblings by position; outside this row the editor reads after Send. */}
173 <Animated.View
174 style={[
175 compactEditor.editorStyle,
176 // iOS destroys a view under display: none; the editor would lose its focus and caret.
177 Platform.OS === "android" && dictating ? { display: "none" } : undefined,
178 ]}
179 >
180 <ComposerEditor
181 ref={editorRef}
182 multiline
183 value={draft}
184 readOnly={locked}
185 selection={selection}
186 onChangeText={(text) => {
187 if (!locked) changeDraft(text);
188 }}
189 onSelectionChange={setSelection}
190 placeholder={gt("Leave a comment")}
191 onFocus={() => {
192 setFocused(true);
193 props.onFocusChange(true);
194 }}
195 onBlur={() => {
196 setFocused(false);
197 props.onFocusChange(false);
198 }}
199 onSubmit={() => void send()}
200 submitTitle={gt("Comment", { $context: "verb, posts the comment" })}
201 {...compactEditor.editorProps}
202 textStyle={textStyle}
203 />
204 </Animated.View>
205 <View
206 className="min-w-0 shrink flex-row items-center"
207 style={
208 compactEditor.isExpanded ? { flex: 1, justifyContent: "flex-end" } : undefined
209 }
210 >
211 <ComposerDictationStartAction
212 size={chrome.controlSize}
213 state={voiceInput.state}
214 isAvailable={voiceInput.isAvailable}
215 disabled={locked}
216 onStart={voiceInput.start}
217 onCancel={voiceInput.cancel}
218 />
219 {sendButton}
220 </View>
221 </ComposerDictationDraftContent>
222 </View>
223 <Animated.View
224 accessibilityElementsHidden={!dictating}
225 collapsable={false}
226 importantForAccessibility={dictating ? "auto" : "no-hide-descendants"}
227 pointerEvents={dictating ? "auto" : "none"}
228 style={{ position: "absolute", bottom: chrome.paddingVertical, left: 0, right: 0 }}
229 >
230 <ComposerDictationToolbar showsDictation={dictating} visible={dictating}>
231 <ComposerToolbarRow
232 paddingBottom={0}
233 paddingHorizontal={0}
234 paddingTop={0}
235 style={{ gap: 0 }}
236 >
237 <ComposerDictationCancelAction
238 size={chrome.controlSize}
239 presentation={voicePresentation}
240 onCancel={voiceInput.cancel}
241 />
242 <ComposerDictationStatus
243 audioLevels={voiceInput.audioLevels}
244 elapsedSeconds={voiceInput.elapsedSeconds}
245 phase={voiceInput.state.phase}
246 presentation={voicePresentation}
247 />
248 <View className="shrink-0 flex-row items-center">
249 <ComposerDictationPrimaryAction
250 size={chrome.controlSize}
251 state={voiceInput.state}
252 presentation={voicePresentation}
253 isAvailable={voiceInput.isAvailable}
254 disabled={locked}
255 onStart={voiceInput.start}
256 onConfirm={voiceInput.stop}
257 onCancel={voiceInput.cancel}
258 />
259 {voicePresentation.showsSend ? sendButton : null}
260 </View>
261 </ComposerToolbarRow>
262 </ComposerDictationToolbar>
263 </Animated.View>
264 </>
265 )}
266 </ComposerSurface>
267 </View>
268 </Animated.View>
269 );