The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 2 | import { requireNativeModule, requireNativeView } from "expo"; |
| 3 | import { useRef, type Ref } from "react"; |
| 4 | import { |
| 5 | Platform, |
| 6 | StyleSheet, |
| 7 | View, |
| 8 | type NativeSyntheticEvent, |
| 9 | type ViewProps, |
| 10 | } from "react-native"; |
| 11 | |
| 12 | import { readPreviewTunnelUrl } from "../../state/previewTunnel"; |
| 13 | |
| 14 | export interface TunnelNavigation { |
| 15 | readonly url: string; |
| 16 | readonly title: string; |
| 17 | readonly loading: boolean; |
| 18 | readonly canGoBack: boolean; |
| 19 | readonly canGoForward: boolean; |
| 20 | /** iOS only: the page's theme-color, else its background, as #rrggbb; empty when unknown. */ |
| 21 | readonly pageColor?: string; |
| 22 | } |
| 23 | |
| 24 | const TUNNEL_FAILURE_CAUSES = ["environment", "port", "outdated"] as const; |
| 25 | type TunnelFailureCause = (typeof TUNNEL_FAILURE_CAUSES)[number]; |
| 26 | |
| 27 | export interface TunnelWebViewRef { |
| 28 | readonly loadUrl: (url: string) => Promise<void>; |
| 29 | readonly goBack: () => Promise<void>; |
| 30 | readonly goForward: () => Promise<void>; |
| 31 | readonly reload: () => Promise<void>; |
| 32 | } |
| 33 | |
| 34 | const PreviewTunnel = requireNativeModule<{ |
| 35 | resolveAccess(requestId: string, url: string | null): void; |
| 36 | }>("T3PreviewTunnel"); |
| 37 | |
| 38 | const NativeTunnelWebView = requireNativeView< |
| 39 | ViewProps & { |
| 40 | readonly ref?: Ref<TunnelWebViewRef>; |
| 41 | readonly environmentId: string; |
| 42 | readonly url: string; |
| 43 | readonly topInset?: number; |
| 44 | readonly onAccessRequest: (event: NativeSyntheticEvent<{ requestId: string }>) => void; |
| 45 | readonly onNavigationChange: (event: NativeSyntheticEvent<TunnelNavigation>) => void; |
| 46 | readonly onLoadError: ( |
| 47 | event: NativeSyntheticEvent<{ |
| 48 | code: number; |
| 49 | description: string; |
| 50 | url: string; |
| 51 | debug?: string; |
| 52 | cause?: string; |
| 53 | }>, |
| 54 | ) => void; |
| 55 | readonly onPageScroll: (event: NativeSyntheticEvent<{ y: number }>) => void; |
| 56 | } |
| 57 | >("T3PreviewTunnel"); |
| 58 | |
| 59 | /** A native webview whose localhost is the environment's loopback, reached through its tunnel. */ |
| 60 | export function TunnelWebView({ |
| 61 | environmentId, |
| 62 | url, |
| 63 | ref, |
| 64 | onNavigationChange, |
| 65 | onLoadError, |
| 66 | onScrollDirection, |
| 67 | onAccess, |
| 68 | topInset, |
| 69 | ...props |
| 70 | }: ViewProps & { |
| 71 | readonly environmentId: EnvironmentId; |
| 72 | readonly url: string; |
| 73 | readonly ref?: Ref<TunnelWebViewRef>; |
| 74 | readonly onNavigationChange: (navigation: TunnelNavigation) => void; |
| 75 | /** |
| 76 | * `url` is the address that failed, or empty when the platform does not report it. |
| 77 | * `unsupported` means this device's webview cannot route localhost at all. |
| 78 | */ |
| 79 | readonly onLoadError: (failure: { |
| 80 | readonly url: string; |
| 81 | readonly unsupported: boolean; |
| 82 | readonly description: string; |
| 83 | /** What the native proxy saw, shown with the error so a report carries it. iOS only. */ |
| 84 | readonly debug?: string; |
| 85 | /** |
| 86 | * Whether the environment itself was out of reach (`environment`), answered that nothing |
| 87 | * listens on the port (`port`), or runs a T3 Code server without the tunnel (`outdated`). |
| 88 | */ |
| 89 | readonly cause?: TunnelFailureCause; |
| 90 | }) => void; |
| 91 | /** Whether the last connection could get into the environment's tunnel at all. */ |
| 92 | readonly onAccess?: (available: boolean) => void; |
| 93 | /** iOS only: room at the top the page scrolls under, for a bar laid over it. */ |
| 94 | readonly topInset?: number; |
| 95 | /** Reported every few dozen points the page scrolls; "up" also at the top. */ |
| 96 | readonly onScrollDirection?: (direction: "up" | "down") => void; |
| 97 | }) { |
| 98 | const scrollY = useRef(0); |
| 99 | // Styling goes on a plain View: a native view from `requireNativeView` ignores className. |
| 100 | return ( |
| 101 | <View {...props}> |
| 102 | <NativeTunnelWebView |
| 103 | style={StyleSheet.absoluteFill} |
| 104 | ref={ref} |
| 105 | environmentId={environmentId} |
| 106 | url={url} |
| 107 | {...(Platform.OS === "ios" ? { topInset: topInset ?? 0 } : {})} |
| 108 | onAccessRequest={({ nativeEvent }) => { |
| 109 | void readPreviewTunnelUrl(environmentId).then((tunnelUrl) => { |
| 110 | onAccess?.(tunnelUrl !== null); |
| 111 | PreviewTunnel.resolveAccess(nativeEvent.requestId, tunnelUrl); |
| 112 | }); |
| 113 | }} |
| 114 | onNavigationChange={({ nativeEvent }) => onNavigationChange(nativeEvent)} |
| 115 | onPageScroll={({ nativeEvent }) => { |
| 116 | const down = nativeEvent.y > 0 && nativeEvent.y > scrollY.current; |
| 117 | scrollY.current = nativeEvent.y; |
| 118 | onScrollDirection?.(down ? "down" : "up"); |
| 119 | }} |
| 120 | onLoadError={({ nativeEvent }) => |
| 121 | onLoadError({ |
| 122 | url: nativeEvent.url, |
| 123 | // The view reports a setup failure with no URL and code -1, before any page loads. |
| 124 | unsupported: nativeEvent.code === -1 && nativeEvent.url === "", |
| 125 | description: nativeEvent.description, |
| 126 | debug: nativeEvent.debug, |
| 127 | cause: TUNNEL_FAILURE_CAUSES.find((cause) => cause === nativeEvent.cause), |
| 128 | }) |
| 129 | } |
| 130 | /> |
| 131 | </View> |
| 132 | ); |
| 133 | } |