The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 265 lines 10.6 KB
Raw
1 import { useGT } from "gt-react-native";
2 import { useRef, useState } from "react";
3 import { Platform, View } from "react-native";
4 import Animated from "react-native-reanimated";
5
6 import {
7 ComposerEditor,
8 type ComposerEditorHandle,
9 type ComposerEditorSelection,
10 } from "../../components/ComposerEditor";
11 import {
12 COMPOSER_BOTTOM_GUTTER,
13 ComposerSurface,
14 INLINE_EDITOR_MIN_WIDTH,
15 useCompactComposerEditor,
16 useComposerChrome,
17 useComposerTextStyle,
18 } from "../../components/ComposerSurface";
19 import { ComposerActionButton, ComposerToolbarRow } from "../../components/ComposerToolbar";
20 import { AppText as Text } from "../../components/ui/AppText";
21 import { useComposerTextRole } from "../settings/appearance/useScaledTextRole";
22 import {
23 ComposerDictationCancelAction,
24 ComposerDictationDraftContent,
25 ComposerDictationError,
26 ComposerDictationPrimaryAction,
27 ComposerDictationStartAction,
28 ComposerDictationStatus,
29 ComposerDictationToolbar,
30 } from "../voice-input/ComposerDictationControl";
31 import { useVoiceInputController } from "../voice-input/useVoiceInputController";
32 import { resolveVoiceComposerPresentation } from "../voice-input/voiceInputPresentation";
33
34 // The thread composer's editor starts after its attachment button; this keeps the same text inset.
35 const 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 */
41 export 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={[compactEditor.editorStyle, dictating ? { display: "none" } : undefined]}
174 >
175 <ComposerEditor
176 ref={editorRef}
177 multiline
178 value={draft}
179 readOnly={locked}
180 selection={selection}
181 onChangeText={(text) => {
182 if (!locked) changeDraft(text);
183 }}
184 onSelectionChange={setSelection}
185 placeholder={gt("Leave a comment")}
186 onFocus={() => {
187 setFocused(true);
188 props.onFocusChange(true);
189 }}
190 onBlur={() => {
191 setFocused(false);
192 props.onFocusChange(false);
193 }}
194 onSubmit={() => void send()}
195 submitTitle={gt("Comment", { $context: "verb, posts the comment" })}
196 {...compactEditor.editorProps}
197 textStyle={textStyle}
198 />
199 </Animated.View>
200 <View
201 className="min-w-0 shrink flex-row items-center"
202 style={
203 compactEditor.isExpanded ? { flex: 1, justifyContent: "flex-end" } : undefined
204 }
205 >
206 <ComposerDictationStartAction
207 size={chrome.controlSize}
208 state={voiceInput.state}
209 isAvailable={voiceInput.isAvailable}
210 disabled={locked}
211 onStart={voiceInput.start}
212 onCancel={voiceInput.cancel}
213 />
214 {sendButton}
215 </View>
216 </ComposerDictationDraftContent>
217 </View>
218 <Animated.View
219 accessibilityElementsHidden={!dictating}
220 collapsable={false}
221 importantForAccessibility={dictating ? "auto" : "no-hide-descendants"}
222 pointerEvents={dictating ? "auto" : "none"}
223 style={{ position: "absolute", bottom: chrome.paddingVertical, left: 0, right: 0 }}
224 >
225 <ComposerDictationToolbar showsDictation={dictating} visible={dictating}>
226 <ComposerToolbarRow
227 paddingBottom={0}
228 paddingHorizontal={0}
229 paddingTop={0}
230 style={{ gap: 0 }}
231 >
232 <ComposerDictationCancelAction
233 size={chrome.controlSize}
234 presentation={voicePresentation}
235 onCancel={voiceInput.cancel}
236 />
237 <ComposerDictationStatus
238 audioLevels={voiceInput.audioLevels}
239 elapsedSeconds={voiceInput.elapsedSeconds}
240 phase={voiceInput.state.phase}
241 presentation={voicePresentation}
242 />
243 <View className="shrink-0 flex-row items-center">
244 <ComposerDictationPrimaryAction
245 size={chrome.controlSize}
246 state={voiceInput.state}
247 presentation={voicePresentation}
248 isAvailable={voiceInput.isAvailable}
249 disabled={locked}
250 onStart={voiceInput.start}
251 onConfirm={voiceInput.stop}
252 onCancel={voiceInput.cancel}
253 />
254 {voicePresentation.showsSend ? sendButton : null}
255 </View>
256 </ComposerToolbarRow>
257 </ComposerDictationToolbar>
258 </Animated.View>
259 </>
260 )}
261 </ComposerSurface>
262 </View>
263 </Animated.View>
264 );
265 }