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

DeviceStreamWebView.tsx

109 lines3.3 KB

1import deviceStreamScript from "@t3tools/mobile-device-stream";
2import { useImperativeHandle, useLayoutEffect, useMemo, useRef, useState, type Ref } from "react";
3import { Platform } from "react-native";
4import { WebView } from "react-native-webview";
5​
6import {
7 deviceStreamDocument,
8 deviceStreamMessage,
9 type DeviceStreamConfiguration,
10} from "./device-stream-document";
11​
12export interface DeviceStreamRef {
13 home: () => void;
14 back: () => void;
15 appSwitcher: () => void;
16 rotate: () => void;
18​
19type NativeStreamBridge = {
20 readonly ref?: Ref<DeviceStreamRef>;
21 readonly onUnauthorized: () => Promise<void>;
22 readonly onInputConnected: (connected: boolean) => Promise<void>;
23};
24​
25export function DeviceStreamWebView({
26 ref,
27 ...props
28}: DeviceStreamConfiguration & NativeStreamBridge) {
29 const [attempt, setAttempt] = useState(0);
30 const configuration = JSON.stringify({
31 access: props.access,
32 platform: props.platform,
33 deviceId: props.deviceId,
34 colors: props.colors,
35 });
36 return (
37 <DeviceStreamDocumentView
38 key={`${attempt}:${configuration}`}
39 ref={ref}
40 configuration={configuration}
41 background={props.colors.background}
42 onUnauthorized={props.onUnauthorized}
43 onInputConnected={props.onInputConnected}
44 onRetry={() => setAttempt((attempt) => attempt + 1)}
45 />
46 );
48​
49function DeviceStreamDocumentView({
50 ref,
51 configuration,
52 background,
53 onUnauthorized,
54 onInputConnected,
55 onRetry,
56}: NativeStreamBridge & {
57 readonly configuration: string;
58 readonly background: string;
59 readonly onRetry: () => void;
60}) {
61 const webView = useRef<WebView>(null);
62 const source = useMemo(
63 () => ({
64 html: deviceStreamDocument(configuration, deviceStreamScript),
65 // Android WebCodecs needs a secure document; streams still use the environment's URLs.
66 baseUrl: Platform.OS === "android" ? "https://localhost/" : "file:///",
67 }),
68 [configuration],
69 );
70 const command = (button: keyof DeviceStreamRef) => {
71 webView.current?.injectJavaScript(
72 `window.T3DeviceStream?.command(${JSON.stringify(button)}); true;`,
73 );
74 };
75 useImperativeHandle(ref, () => ({
76 home: () => command("home"),
77 back: () => command("back"),
78 appSwitcher: () => command("appSwitcher"),
79 rotate: () => command("rotate"),
80 }));
81 useLayoutEffect(() => {
82 const view = webView.current;
83 return () => view?.injectJavaScript("window.T3DeviceStream?.stop(); true;");
84 }, []);
85 return (
86 <WebView
87 ref={webView}
88 source={source}
89 originWhitelist={["*"]}
90 scrollEnabled={false}
91 bounces={false}
92 mixedContentMode="always"
93 allowUniversalAccessFromFileURLs
94 contentInsetAdjustmentBehavior="never"
95 setSupportMultipleWindows={false}
96 style={{ flex: 1, backgroundColor: background }}
97 onLoadStart={() => void onInputConnected(false)}
98 onShouldStartLoadWithRequest={(request) =>
99 request.url === "about:blank" || request.url === source.baseUrl
100 }
101 onMessage={(event) => {
102 const message = deviceStreamMessage(event.nativeEvent.data);
103 if (message?.type === "unauthorized") void onUnauthorized();
104 else if (message?.type === "input") void onInputConnected(message.connected);
105 else if (message?.type === "retry") onRetry();
106 }}
107 />
108 );