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

IssueCommentComposer.tsx

269 lines10.8 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 onChangeDraftMessage: changeDraft,
77 onChangeSelection: setSelection,
78 });
79 const voicePresentation = resolveVoiceComposerPresentation(
80 voiceInput.state,
81 voiceInput.elapsedSeconds,
82 );
83 const dictating = voicePresentation.statusKind === "active";
84 const locked = props.posting || voiceInput.blocksSubmission;
85 const canSend = props.blocked === null && !locked && draft.trim().length > 0;
86​
87 const sending = useRef(false);
88 const flushAndSubmit = async () => {
89 await editorRef.current?.flushPendingInput();
90 const submitted = latestDraft.current;
91 if (submitted.trim().length === 0) return;
92 if ((await props.onSubmit(submitted)) && latestDraft.current === submitted) changeDraft("");
93 };
94 const send = () => {
95 if (!canSend || sending.current) return Promise.resolve();
96 sending.current = true;
97 // `finally` on the promise: the React Compiler does not compile a `try` without a `catch`.
98 return flushAndSubmit().finally(() => {
99 sending.current = false;
100 });
101 };
102 const sendButton = (
103 <ComposerActionButton
104 size={chrome.controlSize}
105 accessibilityLabel={props.posting ? gt("Posting comment") : gt("Post comment")}
106 icon="arrow.up"
107 variant="primary"
108 disabled={!canSend}
109 onPress={() => void send()}
110 />
111 );
112​
113 return (
114 <Animated.View
115 onLayout={(event) => props.onHeightChange(event.nativeEvent.layout.height)}
116 className={Platform.OS === "android" ? "bg-screen px-[12px]" : "px-[12px]"}
117 style={{ paddingTop: 6, paddingBottom: props.bottomInset + COMPOSER_BOTTOM_GUTTER }}
118 >
119 <View
120 className={
121 Platform.OS === "android"
122 ? "hidden"
123 : "absolute inset-0 bg-linear-to-b from-screen/0 via-screen/60 to-screen/90"
124 }
125 pointerEvents="none"
126 />
127 <View className="w-full self-center" style={{ maxWidth: props.contentMaxWidth }}>
128 <ComposerSurface
129 animateLayout={false}
130 style={{
131 borderRadius: chrome.borderRadius,
132 borderCurve: "circular",
133 overflow: "hidden",
134 paddingVertical: chrome.paddingVertical,
135 }}
136 >
137 <ComposerDictationError
138 error={voiceInput.state.phase === "error" ? voiceInput.state.error : null}
139 onDismiss={voiceInput.cancel}
140 />
141 {props.blocked !== null ? (
142 <View className="h-[44px] flex-row items-center">
143 <Text
144 className="min-w-0 flex-1 text-base text-foreground-muted"
145 style={{ paddingLeft: EDITOR_LEADING_INSET }}
146 numberOfLines={1}
147 ellipsizeMode="tail"
148 >
149 {props.blocked}
150 </Text>
151 {sendButton}
152 </View>
153 ) : (
154 <>
155 <View onLayout={compactEditor.onRowLayout}>
156 <ComposerDictationDraftContent
157 className="flex-row items-end"
158 compact
159 hidden={dictating}
160 >
161 <View style={{ width: EDITOR_LEADING_INSET - 8 }} />
162 <Animated.View
163 className="min-w-0 flex-1"
164 onLayout={compactEditor.onEditorLayout}
165 style={[
166 { minWidth: compactEditor.isExpanded ? 0 : INLINE_EDITOR_MIN_WIDTH },
167 compactEditor.containerStyle,
168 compactEditor.isExpanded ? { flex: 0, width: 0 } : undefined,
169 ]}
170 />
171 {/* TalkBack orders siblings by position; outside this row the editor reads after Send. */}
172 <Animated.View
173 style={[
174 compactEditor.editorStyle,
175 // iOS destroys a view under display: none; the editor would lose its focus and caret.
176 Platform.OS === "android" && dictating ? { display: "none" } : undefined,
177 ]}
178 >
179 <ComposerEditor
180 ref={editorRef}
181 multiline
182 value={draft}
183 readOnly={locked}
184 selection={selection}
185 onChangeText={(text) => {
186 if (!locked) changeDraft(text);
187 }}
188 onSelectionChange={setSelection}
189 placeholder={gt("Leave a comment")}
190 onFocus={() => {
191 setFocused(true);
192 props.onFocusChange(true);
193 }}
194 onBlur={() => {
195 setFocused(false);
196 props.onFocusChange(false);
197 }}
198 onSubmit={() => void send()}
199 submitTitle={gt("Comment", { $context: "verb, posts the comment" })}
200 {...compactEditor.editorProps}
201 textStyle={textStyle}
202 />
203 </Animated.View>
204 <View
205 className="min-w-0 shrink flex-row items-center"
206 style={
207 compactEditor.isExpanded ? { flex: 1, justifyContent: "flex-end" } : undefined
208 }
209 >
210 <ComposerDictationStartAction
211 size={chrome.controlSize}
212 state={voiceInput.state}
213 isAvailable={voiceInput.isAvailable}
214 disabled={locked}
215 onStart={voiceInput.start}
216 onCancel={voiceInput.cancel}
217 />
218 {sendButton}
219 </View>
220 </ComposerDictationDraftContent>
221 </View>
222 <Animated.View
223 accessibilityElementsHidden={!dictating}
224 collapsable={false}
225 importantForAccessibility={dictating ? "auto" : "no-hide-descendants"}
226 pointerEvents={dictating ? "auto" : "none"}
227 style={{ position: "absolute", bottom: chrome.paddingVertical, left: 0, right: 0 }}
228 >
229 <ComposerDictationToolbar showsDictation={dictating} visible={dictating}>
230 <ComposerToolbarRow
231 paddingBottom={0}
232 paddingHorizontal={0}
233 paddingTop={0}
234 style={{ gap: 0 }}
235 >
236 <ComposerDictationCancelAction
237 size={chrome.controlSize}
238 presentation={voicePresentation}
239 onCancel={voiceInput.cancel}
240 />
241 <ComposerDictationStatus
242 audioLevels={voiceInput.audioLevels}
243 elapsedSeconds={voiceInput.elapsedSeconds}
244 phase={voiceInput.state.phase}
245 presentation={voicePresentation}
246 />
247 <View className="shrink-0 flex-row items-center">
248 <ComposerDictationPrimaryAction
249 size={chrome.controlSize}
250 state={voiceInput.state}
251 presentation={voicePresentation}
252 isAvailable={voiceInput.isAvailable}
253 disabled={locked}
254 onStart={voiceInput.start}
255 onConfirm={voiceInput.stop}
256 onCancel={voiceInput.cancel}
257 />
258 {voicePresentation.showsSend ? sendButton : null}
259 </View>
260 </ComposerToolbarRow>
261 </ComposerDictationToolbar>
262 </Animated.View>
263 </>
264 )}
265 </ComposerSurface>
266 </View>
267 </Animated.View>
268 );