@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 557 lines 20.5 KB
Raw
1 import { errorDisplayMessage } from "@t3tools/client-runtime/errors";
2 import { Button } from "../../components/ui/Button";
3 import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
4 import * as Dialog from "../../components/ui/Dialog";
5 import previewStreamScript from "@t3tools/mobile-preview-stream";
6 import {
7 previewStreamControlLabel,
8 previewStreamHostSetupMessage,
9 type PreviewStreamControl,
10 type PreviewStreamDownload,
11 type PreviewStreamFileChooser,
12 type PreviewStreamInput,
13 } from "@t3tools/client-runtime/preview/server-browser-stream";
14 import type { EnvironmentId, PreviewStreamHostSetup } from "@t3tools/contracts";
15 import {
16 useEffectEvent,
17 useImperativeHandle,
18 useLayoutEffect,
19 useRef,
20 useState,
21 type Ref,
22 } from "react";
23 import { msg, useGT, useMessages } from "gt-react-native";
24 import { Platform, Pressable, TextInput, View } from "react-native";
25 import { WebView } from "react-native-webview";
26 import * as Clipboard from "expo-clipboard";
27
28 import { AppText } from "../../components/ui/AppText";
29 import { PdfPreview } from "../../components/PdfPreview";
30 import { m } from "../../i18n/messages";
31 import { downloadAndShareAttachment } from "../../lib/attachmentDownload";
32 import { beginForegroundHandoff } from "../../lib/foreground-handoff";
33 import { usePreviewStreamAccess } from "../../state/preview";
34 import { previewPromptText } from "./previewPromptText";
35
36 import {
37 previewStreamDocument,
38 previewStreamMessage,
39 type PreviewStreamConfiguration,
40 } from "./preview-stream-document";
41
42 export interface PreviewStreamRef {
43 readonly command: (input: PreviewStreamInput) => void;
44 readonly paste: () => void;
45 readonly togglePictureInPicture: () => void;
46 }
47
48 export interface PreviewPictureInPictureState {
49 readonly supported: boolean;
50 readonly active: boolean;
51 }
52
53 type NativeStreamBridge = {
54 readonly ref?: Ref<PreviewStreamRef>;
55 readonly onUnauthorized: () => void;
56 /** Asks for fresh access after a refusal, once `delayMs` has passed. */
57 readonly onRefused: (delayMs: number) => void;
58 readonly onGone?: () => void;
59 readonly onViewport?: (viewport: { readonly width: number; readonly height: number }) => void;
60 /** The direction a drag scrolls the page, every few dozen pixels of it. */
61 readonly onScrollDirection?: (direction: "up" | "down") => void;
62 readonly onControl?: (control: PreviewStreamControl | null) => void;
63 readonly onPictureInPicture?: (state: PreviewPictureInPictureState, detail?: string) => void;
64 /** True while frames show, so commands reach the page. */
65 readonly onStreamingChange?: (streaming: boolean) => void;
66 readonly compact?: boolean;
67 readonly pdfUri?: string | null;
68 };
69
70 async function sendFilesToPage(chooser: PreviewStreamFileChooser, pick: boolean) {
71 const body = new FormData();
72 if (pick) {
73 const { getDocumentAsync } = await import("expo-document-picker");
74 const endHandoff = beginForegroundHandoff();
75 let result: Awaited<ReturnType<typeof getDocumentAsync>>;
76 try {
77 result = await getDocumentAsync({ multiple: chooser.multiple, copyToCacheDirectory: true });
78 } finally {
79 endHandoff();
80 }
81 for (const asset of result.canceled ? [] : result.assets) {
82 body.append("file", {
83 uri: asset.uri,
84 name: asset.name,
85 type: asset.mimeType ?? "application/octet-stream",
86 } as unknown as Blob);
87 }
88 }
89 const response = await fetch(chooser.uploadUrl, {
90 method: "POST",
91 body,
92 credentials: chooser.credentials ? "include" : "omit",
93 });
94 if (!response.ok) throw new Error((await response.text()) || m(msg("The upload was refused.")));
95 }
96
97 function offerDownload(download: PreviewStreamDownload) {
98 Dialog.alert(m(msg("Downloaded {fileName}", { fileName: download.fileName })), undefined, [
99 { text: m(msg("Not now")), style: "cancel" },
100 {
101 text: m(msg("Save or share")),
102 onPress: () =>
103 void downloadAndShareAttachment({
104 url: download.url,
105 attachment: { name: download.fileName, mimeType: "application/octet-stream" },
106 signal: new AbortController().signal,
107 }).catch((cause: unknown) =>
108 Dialog.alert(
109 m(msg("Could not save the download")),
110 cause instanceof Error ? (errorDisplayMessage(cause, m) ?? cause.message) : undefined,
111 ),
112 ),
113 },
114 ]);
115 }
116
117 const MAX_REFUSALS = 3;
118
119 export function PreviewStreamWebView(
120 props: Omit<PreviewStreamConfiguration, "access" | "labels"> &
121 Omit<NativeStreamBridge, "onUnauthorized" | "onRefused"> & {
122 readonly environmentId: EnvironmentId;
123 readonly paused?: boolean;
124 },
125 ) {
126 const { access, error, refresh, retryAfter } = usePreviewStreamAccess(props.environmentId);
127 const gt = useGT();
128 if (access && !props.paused) {
129 return (
130 <AuthorizedPreviewStream
131 {...props}
132 access={access}
133 onUnauthorized={refresh}
134 onRefused={retryAfter}
135 />
136 );
137 }
138 return (
139 <View
140 className={
141 props.compact
142 ? "flex-1 items-center justify-center"
143 : "flex-1 items-center justify-center gap-4 px-6"
144 }
145 >
146 {props.compact || !error ? <AppActivityIndicator colorClassName="accent-icon" /> : null}
147 {!props.compact && (
148 <>
149 <AppText
150 selectable={!!error}
151 className={
152 error ? "text-center text-sm text-foreground-muted" : "text-sm text-foreground-muted"
153 }
154 >
155 {error || gt("Connecting to browser...")}
156 </AppText>
157 {error && (
158 <View className="items-center">
159 <Button label={gt("Retry")} tone="secondary" onPress={refresh} />
160 </View>
161 )}
162 </>
163 )}
164 </View>
165 );
166 }
167
168 function AuthorizedPreviewStream({
169 ref,
170 ...props
171 }: Omit<PreviewStreamConfiguration, "labels"> & NativeStreamBridge) {
172 const gt = useGT();
173 const [attempt, setAttempt] = useState(0);
174 const [previousAccess, setPreviousAccess] = useState(props.access);
175 // Wait for refreshed access, including cookie credentials with unchanged JSON.
176 if (props.access !== previousAccess) {
177 setPreviousAccess(props.access);
178 setAttempt((current) => current + 1);
179 }
180 const processRetried = useRef(false);
181 const unauthorized = useRef(0);
182 const configuration = JSON.stringify({
183 access: props.access,
184 threadId: props.threadId,
185 tabId: props.tabId,
186 interactive: props.interactive,
187 background: props.background,
188 labels: {
189 page: gt("Browser page"),
190 pageInput: gt("Browser page input"),
191 pictureInPictureNotReady: gt("The page has not shown yet. Try again in a moment."),
192 pictureInPictureUnavailable: gt("Picture in picture is unavailable."),
193 },
194 } satisfies PreviewStreamConfiguration);
195 return (
196 <PreviewStreamDocumentView
197 key={`${attempt}:${configuration}`}
198 {...props}
199 ref={ref}
200 configuration={configuration}
201 onUnauthorized={() => {
202 const refusals = ++unauthorized.current;
203 if (refusals >= MAX_REFUSALS) return false;
204 props.onRefused(refusals === 1 ? 0 : 1_000 * 2 ** (refusals - 1));
205 return true;
206 }}
207 onRetry={() => {
208 processRetried.current = false;
209 unauthorized.current = 0;
210 props.onUnauthorized();
211 }}
212 onStreaming={() => {
213 processRetried.current = false;
214 unauthorized.current = 0;
215 }}
216 onRecoverProcess={() => {
217 if (processRetried.current) return false;
218 processRetried.current = true;
219 setAttempt((current) => current + 1);
220 return true;
221 }}
222 />
223 );
224 }
225
226 function PreviewStreamDocumentView({
227 ref,
228 configuration,
229 background,
230 compact,
231 pdfUri,
232 onUnauthorized,
233 onGone,
234 onViewport,
235 onScrollDirection,
236 onControl,
237 onPictureInPicture,
238 onStreamingChange,
239 onRetry,
240 onStreaming,
241 onRecoverProcess,
242 }: Omit<NativeStreamBridge, "onUnauthorized" | "onRefused"> & {
243 readonly configuration: string;
244 readonly background: string;
245 readonly onUnauthorized: () => boolean;
246 readonly onRetry: () => void;
247 readonly onStreaming: () => void;
248 readonly onRecoverProcess: () => boolean;
249 }) {
250 const webView = useRef<WebView<object>>(null);
251 const gt = useGT();
252 const messages = useMessages();
253 const active = useRef(false);
254 const failed = useRef(false);
255 const bootstrapTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
256 const [status, setStatus] = useState<"connecting" | "streaming" | "error">("connecting");
257 const [error, setError] = useState<string | null>(null);
258 const [gone, setGone] = useState(false);
259 const [hostSetup, setHostSetup] = useState<PreviewStreamHostSetup | null>(null);
260 const [control, setControl] = useState<PreviewStreamControl | null>(null);
261 const dialogRef = useRef<PreviewStreamControl["dialog"]>(null);
262 const [promptText, setPromptText] = useState("");
263 const [fileChooser, setFileChooser] = useState<PreviewStreamFileChooser | null>(null);
264 const answerFileChooser = (pick: boolean) => {
265 const chooser = fileChooser;
266 if (!chooser) return;
267 setFileChooser(null);
268 void sendFilesToPage(chooser, pick).catch((cause: unknown) =>
269 Dialog.alert(
270 m(msg("Could not send the files to the page")),
271 cause instanceof Error ? (errorDisplayMessage(cause, m) ?? cause.message) : undefined,
272 ),
273 );
274 };
275 const command = (input: PreviewStreamInput) =>
276 webView.current?.injectJavaScript(
277 `window.T3PreviewStream?.command(${JSON.stringify(input)}); true;`,
278 );
279 // Reads here rather than in the screen: a tab switch during the iOS paste prompt unmounts
280 // this view and nulls its WebView ref, so the text can never type into another tab.
281 const paste = () =>
282 void Clipboard.getStringAsync().then(
283 (text) => {
284 if (text) command({ type: "text", text });
285 },
286 () => Dialog.alert(m(msg("Could not read the clipboard"))),
287 );
288 const clearBootstrapTimer = () => {
289 if (bootstrapTimer.current === null) return;
290 clearTimeout(bootstrapTimer.current);
291 bootstrapTimer.current = null;
292 };
293 const fail = (message: string) => {
294 if (!active.current || failed.current) return;
295 failed.current = true;
296 clearBootstrapTimer();
297 webView.current?.injectJavaScript("window.T3PreviewStream?.stop(); true;");
298 onStreamingChange?.(false);
299 setControl(null);
300 setFileChooser(null);
301 onControl?.(null);
302 setError(message);
303 setStatus("error");
304 };
305 const bootstrapTimedOut = useEffectEvent(() =>
306 fail(m(msg("Browser viewer could not start. Reconnect to try again."))),
307 );
308 const streamEnded = useEffectEvent(() => {
309 onStreamingChange?.(false);
310 onControl?.(null);
311 });
312 // The stream lives exactly as long as this view.
313 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- starts the bootstrap timeout and stops the WebView stream when this view unmounts
314 useLayoutEffect(() => {
315 active.current = true;
316 const view = webView.current;
317 bootstrapTimer.current = setTimeout(bootstrapTimedOut, 15_000);
318 return () => {
319 if (bootstrapTimer.current !== null) clearTimeout(bootstrapTimer.current);
320 bootstrapTimer.current = null;
321 active.current = false;
322 view?.injectJavaScript("window.T3PreviewStream?.stop(); true;");
323 streamEnded();
324 };
325 }, []);
326 const source = {
327 html: previewStreamDocument(
328 configuration,
329 previewStreamScript,
330 gt("Browser viewer stopped unexpectedly."),
331 ),
332 baseUrl: Platform.OS === "android" ? "https://localhost/" : "file:///",
333 };
334 useImperativeHandle(ref, () => ({
335 command,
336 paste,
337 togglePictureInPicture: () =>
338 webView.current?.injectJavaScript("window.T3PreviewStream?.pictureInPicture(); true;"),
339 }));
340 const processTerminated = () => {
341 if (!active.current || failed.current) return;
342 if (!onRecoverProcess()) fail(m(msg("Browser viewer stopped. Reconnect to try again.")));
343 };
344 return (
345 <View className="flex-1" style={{ backgroundColor: background }}>
346 {/* With control, the bar's menu offers releasing it, so the row would only take space. */}
347 {!compact && control?.controller !== "you" ? (
348 <View className="flex-row items-center justify-between gap-2 border-b border-secondary-border px-3 py-2">
349 <AppText className="text-xs text-foreground-muted">
350 {previewStreamControlLabel(control, messages)}
351 </AppText>
352 {control?.canOperate ? (
353 <Button
354 label={gt("Take control")}
355 tone="secondary"
356 disabled={control.controller === "another-viewer"}
357 onPress={() => command({ type: "takeControl" })}
358 />
359 ) : null}
360 </View>
361 ) : null}
362 <View className="relative flex-1">
363 <WebView<object>
364 ref={webView}
365 source={source}
366 originWhitelist={["*"]}
367 scrollEnabled={false}
368 bounces={false}
369 mixedContentMode="always"
370 allowUniversalAccessFromFileURLs
371 contentInsetAdjustmentBehavior="never"
372 setSupportMultipleWindows={false}
373 allowsInlineMediaPlayback
374 allowsPictureInPictureMediaPlayback
375 mediaPlaybackRequiresUserAction={false}
376 style={{ flex: 1, backgroundColor: background }}
377 onError={() => fail(m(msg("Browser viewer could not load. Reconnect to try again.")))}
378 onHttpError={() => fail(m(msg("Browser viewer could not load. Reconnect to try again.")))}
379 onContentProcessDidTerminate={processTerminated}
380 onRenderProcessGone={processTerminated}
381 onShouldStartLoadWithRequest={(request) =>
382 request.url === "about:blank" || request.url === source.baseUrl
383 }
384 onMessage={(event) => {
385 if (!active.current || failed.current) return;
386 const message = previewStreamMessage(event.nativeEvent.data);
387 if (message === null) return;
388 switch (message.type) {
389 case "control": {
390 setControl(message);
391 const previousDialog = dialogRef.current;
392 dialogRef.current = message.dialog;
393 setPromptText((draft) => previewPromptText(previousDialog, message.dialog, draft));
394 onControl?.(message);
395 return;
396 }
397 case "unauthorized":
398 if (!onUnauthorized()) {
399 fail(
400 m(msg("This session can't open the browser stream. Reconnect to try again.")),
401 );
402 }
403 return;
404 case "gone":
405 setGone(true);
406 fail(m(msg("This tab was closed.")));
407 onGone?.();
408 return;
409 case "hostSetup":
410 setHostSetup({ need: message.need, command: message.command });
411 fail(previewStreamHostSetupMessage(message, m));
412 return;
413 case "viewport":
414 onViewport?.(message);
415 return;
416 case "scroll":
417 onScrollDirection?.(message.direction);
418 return;
419 case "clipboard":
420 void Clipboard.setStringAsync(message.text).catch(() => undefined);
421 return;
422 case "download":
423 offerDownload(message);
424 return;
425 case "fileChooser":
426 setFileChooser(message.chooser);
427 return;
428 case "pictureInPicture":
429 onPictureInPicture?.(message, message.detail);
430 return;
431 case "status":
432 if (message.status !== "connecting") clearBootstrapTimer();
433 if (message.status === "error") {
434 fail(message.detail ?? m(msg("Browser stream failed.")));
435 return;
436 }
437 setStatus(message.status);
438 if (message.status === "connecting") {
439 // Dialogs may have closed and reopened while this viewer was disconnected.
440 dialogRef.current = null;
441 setControl(null);
442 onControl?.(null);
443 }
444 onStreamingChange?.(message.status === "streaming");
445 if (message.status === "streaming") onStreaming();
446 }
447 }}
448 />
449 {pdfUri ? (
450 <View className="absolute inset-0">
451 <PdfPreview key={pdfUri} uri={pdfUri} />
452 </View>
453 ) : null}
454 </View>
455 {!compact && fileChooser && control?.controller === "you" ? (
456 <View className="absolute inset-x-3 top-16 gap-3 rounded-xl border border-secondary-border bg-secondary p-4">
457 <AppText className="text-sm text-secondary-foreground">
458 {fileChooser.multiple
459 ? gt("The page asks for files.")
460 : gt("The page asks for a file.")}
461 </AppText>
462 <View className="flex-row justify-end gap-3">
463 <Button label={gt("Cancel")} tone="text" onPress={() => answerFileChooser(false)} />
464 <Button
465 label={fileChooser.multiple ? gt("Choose files") : gt("Choose file")}
466 tone="secondary"
467 onPress={() => answerFileChooser(true)}
468 />
469 </View>
470 </View>
471 ) : null}
472 {!compact && control?.dialog ? (
473 <View className="absolute inset-x-3 top-16 gap-3 rounded-xl border border-secondary-border bg-secondary p-4">
474 <AppText className="text-sm text-secondary-foreground">{control.dialog.message}</AppText>
475 {control.controller === "you" ? (
476 <>
477 {control.dialog.type === "prompt" ? (
478 <TextInput
479 accessibilityLabel={gt("Dialog response")}
480 className="rounded-lg border border-input-border bg-input px-3 py-2 text-foreground"
481 placeholderTextColorClassName="accent-placeholder"
482 selectionColorClassName="accent-focus/32"
483 value={promptText}
484 onChangeText={setPromptText}
485 />
486 ) : null}
487 <View className="flex-row justify-end gap-3">
488 <Button
489 label={gt("Dismiss", { $context: "rejects a page dialog" })}
490 tone="text"
491 onPress={() => command({ type: "dialog", accept: false })}
492 />
493 <Button
494 label={gt("Accept", { $context: "accepts a page dialog" })}
495 tone="secondary"
496 onPress={() =>
497 command({
498 type: "dialog",
499 accept: true,
500 ...(control.dialog?.type === "prompt" ? { promptText } : {}),
501 })
502 }
503 />
504 </View>
505 </>
506 ) : (
507 <AppText className="text-xs text-foreground-muted">
508 {gt("Take control to respond.")}
509 </AppText>
510 )}
511 </View>
512 ) : null}
513 {status !== "streaming" ? (
514 <View
515 className="absolute inset-0 items-center justify-center gap-4 px-6"
516 style={{ backgroundColor: background }}
517 >
518 {status === "connecting" ? <AppActivityIndicator colorClassName="accent-icon" /> : null}
519 {compact ? null : (
520 <AppText
521 accessibilityLiveRegion="polite"
522 className="text-center text-sm text-foreground-muted"
523 >
524 {status === "error" ? error : gt("Connecting to browser...")}
525 </AppText>
526 )}
527 {status === "error" && hostSetup && !compact ? (
528 <Pressable
529 accessibilityRole="button"
530 accessibilityLabel={gt("Copy {command}", { command: hostSetup.command })}
531 className="w-full flex-row items-center gap-2 rounded-xl border border-secondary-border bg-secondary px-4 py-3"
532 onPress={() => {
533 void Clipboard.setStringAsync(hostSetup.command).catch(() => undefined);
534 }}
535 >
536 <AppText selectable className="flex-1 font-mono text-sm text-secondary-foreground">
537 {hostSetup.command}
538 </AppText>
539 <AppText className="text-xs text-foreground-muted">
540 {gt("Copy", { $context: "verb" })}
541 </AppText>
542 </Pressable>
543 ) : null}
544 {status === "error" && !gone && !compact ? (
545 <View className="items-center">
546 <Button
547 label={hostSetup ? gt("Try again") : gt("Reconnect")}
548 tone="secondary"
549 onPress={onRetry}
550 />
551 </View>
552 ) : null}
553 </View>
554 ) : null}
555 </View>
556 );
557 }