sailfork / t3code-github / apps/ mobile/ src/ features/ browser/ PreviewStreamWebView.tsx

import { Button } from "../../components/ui/Button";
import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
import * as Dialog from "../../components/ui/Dialog";
import previewStreamScript from "@t3tools/mobile-preview-stream";
import {
  previewStreamControlLabel,
  previewStreamHostSetupMessage,
  type PreviewStreamControl,
  type PreviewStreamDownload,
  type PreviewStreamFileChooser,
  type PreviewStreamInput,
} from "@t3tools/client-runtime/preview/server-browser-stream";
import type { EnvironmentId, PreviewStreamHostSetup } from "@t3tools/contracts";
import {
  useEffectEvent,
  useImperativeHandle,
  useLayoutEffect,
  useRef,
  useState,
  type Ref,
} from "react";
import { Platform, Pressable, TextInput, View } from "react-native";
import { WebView } from "react-native-webview";
import * as Clipboard from "expo-clipboard";

import { AppText } from "../../components/ui/AppText";
import { PdfPreview } from "../../components/PdfPreview";
import { downloadAndShareAttachment } from "../../lib/attachmentDownload";
import { beginForegroundHandoff } from "../../lib/foreground-handoff";
import { usePreviewStreamAccess } from "../../state/preview";
import { previewPromptText } from "./previewPromptText";

import {
  previewStreamDocument,
  previewStreamMessage,
  type PreviewStreamConfiguration,
} from "./preview-stream-document";

export interface PreviewStreamRef {
  readonly command: (input: PreviewStreamInput) => void;
  readonly paste: () => void;
  readonly togglePictureInPicture: () => void;
}

export interface PreviewPictureInPictureState {
  readonly supported: boolean;
  readonly active: boolean;
}

type NativeStreamBridge = {
  readonly ref?: Ref<PreviewStreamRef>;
  readonly onUnauthorized: () => void;
  /** Asks for fresh access after a refusal, once `delayMs` has passed. */
  readonly onRefused: (delayMs: number) => void;
  readonly onGone?: () => void;
  readonly onViewport?: (viewport: { readonly width: number; readonly height: number }) => void;
  readonly onControl?: (control: PreviewStreamControl | null) => void;
  readonly onPictureInPicture?: (state: PreviewPictureInPictureState, detail?: string) => void;
  /** True while frames show, so commands reach the page. */
  readonly onStreamingChange?: (streaming: boolean) => void;
  readonly compact?: boolean;
  readonly pdfUri?: string | null;
};

async function sendFilesToPage(chooser: PreviewStreamFileChooser, pick: boolean) {
  const body = new FormData();
  if (pick) {
    const { getDocumentAsync } = await import("expo-document-picker");
    const endHandoff = beginForegroundHandoff();
    let result: Awaited<ReturnType<typeof getDocumentAsync>>;
    try {
      result = await getDocumentAsync({ multiple: chooser.multiple, copyToCacheDirectory: true });
    } finally {
      endHandoff();
    }
    for (const asset of result.canceled ? [] : result.assets) {
      body.append("file", {
        uri: asset.uri,
        name: asset.name,
        type: asset.mimeType ?? "application/octet-stream",
      } as unknown as Blob);
    }
  }
  const response = await fetch(chooser.uploadUrl, {
    method: "POST",
    body,
    credentials: chooser.credentials ? "include" : "omit",
  });
  if (!response.ok) throw new Error((await response.text()) || "The upload was refused.");
}

function offerDownload(download: PreviewStreamDownload) {
  Dialog.alert(`Downloaded ${download.fileName}`, undefined, [
    { text: "Not now", style: "cancel" },
    {
      text: "Save or share",
      onPress: () =>
        void downloadAndShareAttachment({
          url: download.url,
          attachment: { name: download.fileName, mimeType: "application/octet-stream" },
          signal: new AbortController().signal,
        }).catch((cause: unknown) =>
          Dialog.alert(
            "Could not save the download",
            cause instanceof Error ? cause.message : undefined,
          ),
        ),
    },
  ]);
}

const MAX_REFUSALS = 3;

export function PreviewStreamWebView(
  props: Omit<PreviewStreamConfiguration, "access"> &
    Omit<NativeStreamBridge, "onUnauthorized" | "onRefused"> & {
      readonly environmentId: EnvironmentId;
      readonly paused?: boolean;
    },
) {
  const { access, error, refresh, retryAfter } = usePreviewStreamAccess(props.environmentId);
  if (access && !props.paused) {
    return (
      <AuthorizedPreviewStream
        {...props}
        access={access}
        onUnauthorized={refresh}
        onRefused={retryAfter}
      />
    );
  }
  return (
    <View
      className={
        props.compact
          ? "flex-1 items-center justify-center"
          : "flex-1 items-center justify-center gap-4 px-6"
      }
    >
      {props.compact || !error ? <AppActivityIndicator colorClassName="accent-icon" /> : null}
      {!props.compact && (
        <>
          <AppText
            selectable={!!error}
            className={
              error ? "text-center text-sm text-foreground-muted" : "text-sm text-foreground-muted"
            }
          >
            {error || "Connecting to browser..."}
          </AppText>
          {error && (
            <View className="items-center">
              <Button label="Retry" tone="secondary" onPress={refresh} />
            </View>
          )}
        </>
      )}
    </View>
  );
}

function AuthorizedPreviewStream({
  ref,
  ...props
}: PreviewStreamConfiguration & NativeStreamBridge) {
  const [attempt, setAttempt] = useState(0);
  const [previousAccess, setPreviousAccess] = useState(props.access);
  // Wait for refreshed access, including cookie credentials with unchanged JSON.
  if (props.access !== previousAccess) {
    setPreviousAccess(props.access);
    setAttempt((current) => current + 1);
  }
  const processRetried = useRef(false);
  const unauthorized = useRef(0);
  const configuration = JSON.stringify({
    access: props.access,
    threadId: props.threadId,
    tabId: props.tabId,
    interactive: props.interactive,
    background: props.background,
  } satisfies PreviewStreamConfiguration);
  return (
    <PreviewStreamDocumentView
      key={`${attempt}:${configuration}`}
      {...props}
      ref={ref}
      configuration={configuration}
      onUnauthorized={() => {
        const refusals = ++unauthorized.current;
        if (refusals >= MAX_REFUSALS) return false;
        props.onRefused(refusals === 1 ? 0 : 1_000 * 2 ** (refusals - 1));
        return true;
      }}
      onRetry={() => {
        processRetried.current = false;
        unauthorized.current = 0;
        props.onUnauthorized();
      }}
      onStreaming={() => {
        processRetried.current = false;
        unauthorized.current = 0;
      }}
      onRecoverProcess={() => {
        if (processRetried.current) return false;
        processRetried.current = true;
        setAttempt((current) => current + 1);
        return true;
      }}
    />
  );
}

function PreviewStreamDocumentView({
  ref,
  configuration,
  background,
  compact,
  pdfUri,
  onUnauthorized,
  onGone,
  onViewport,
  onControl,
  onPictureInPicture,
  onStreamingChange,
  onRetry,
  onStreaming,
  onRecoverProcess,
}: Omit<NativeStreamBridge, "onUnauthorized" | "onRefused"> & {
  readonly configuration: string;
  readonly background: string;
  readonly onUnauthorized: () => boolean;
  readonly onRetry: () => void;
  readonly onStreaming: () => void;
  readonly onRecoverProcess: () => boolean;
}) {
  const webView = useRef<WebView<object>>(null);
  const active = useRef(false);
  const failed = useRef(false);
  const bootstrapTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
  const [status, setStatus] = useState<"connecting" | "streaming" | "error">("connecting");
  const [error, setError] = useState<string | null>(null);
  const [gone, setGone] = useState(false);
  const [hostSetup, setHostSetup] = useState<PreviewStreamHostSetup | null>(null);
  const [control, setControl] = useState<PreviewStreamControl | null>(null);
  const dialogRef = useRef<PreviewStreamControl["dialog"]>(null);
  const [promptText, setPromptText] = useState("");
  const [fileChooser, setFileChooser] = useState<PreviewStreamFileChooser | null>(null);
  const answerFileChooser = (pick: boolean) => {
    const chooser = fileChooser;
    if (!chooser) return;
    setFileChooser(null);
    void sendFilesToPage(chooser, pick).catch((cause: unknown) =>
      Dialog.alert(
        "Could not send the files to the page",
        cause instanceof Error ? cause.message : undefined,
      ),
    );
  };
  const command = (input: PreviewStreamInput) =>
    webView.current?.injectJavaScript(
      `window.T3PreviewStream?.command(${JSON.stringify(input)}); true;`,
    );
  // Reads here rather than in the screen: a tab switch during the iOS paste prompt unmounts
  // this view and nulls its WebView ref, so the text can never type into another tab.
  const paste = () =>
    void Clipboard.getStringAsync().then(
      (text) => {
        if (text) command({ type: "text", text });
      },
      () => Dialog.alert("Could not read the clipboard"),
    );
  const clearBootstrapTimer = () => {
    if (bootstrapTimer.current === null) return;
    clearTimeout(bootstrapTimer.current);
    bootstrapTimer.current = null;
  };
  const fail = (message: string) => {
    if (!active.current || failed.current) return;
    failed.current = true;
    clearBootstrapTimer();
    webView.current?.injectJavaScript("window.T3PreviewStream?.stop(); true;");
    onStreamingChange?.(false);
    setControl(null);
    setFileChooser(null);
    onControl?.(null);
    setError(message);
    setStatus("error");
  };
  const bootstrapTimedOut = useEffectEvent(() =>
    fail("Browser viewer could not start. Reconnect to try again."),
  );
  const streamEnded = useEffectEvent(() => {
    onStreamingChange?.(false);
    onControl?.(null);
  });
  // The stream lives exactly as long as this view.
  // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- starts the bootstrap timeout and stops the WebView stream when this view unmounts
  useLayoutEffect(() => {
    active.current = true;
    const view = webView.current;
    bootstrapTimer.current = setTimeout(bootstrapTimedOut, 15_000);
    return () => {
      if (bootstrapTimer.current !== null) clearTimeout(bootstrapTimer.current);
      bootstrapTimer.current = null;
      active.current = false;
      view?.injectJavaScript("window.T3PreviewStream?.stop(); true;");
      streamEnded();
    };
  }, []);
  const source = {
    html: previewStreamDocument(configuration, previewStreamScript),
    baseUrl: Platform.OS === "android" ? "https://localhost/" : "file:///",
  };
  useImperativeHandle(ref, () => ({
    command,
    paste,
    togglePictureInPicture: () =>
      webView.current?.injectJavaScript("window.T3PreviewStream?.pictureInPicture(); true;"),
  }));
  const processTerminated = () => {
    if (!active.current || failed.current) return;
    if (!onRecoverProcess()) fail("Browser viewer stopped. Reconnect to try again.");
  };
  return (
    <View className="flex-1" style={{ backgroundColor: background }}>
      {!compact ? (
        <View className="flex-row items-center justify-between gap-2 border-b border-secondary-border px-3 py-2">
          <AppText className="text-xs text-foreground-muted">
            {previewStreamControlLabel(control)}
          </AppText>
          {control?.canOperate ? (
            <Button
              label={control.controller === "you" ? "Release control" : "Take control"}
              tone="secondary"
              disabled={control.controller === "another-viewer"}
              onPress={() =>
                command({ type: control.controller === "you" ? "releaseControl" : "takeControl" })
              }
            />
          ) : null}
        </View>
      ) : null}
      <View className="relative flex-1">
        <WebView<object>
          ref={webView}
          source={source}
          originWhitelist={["*"]}
          scrollEnabled={false}
          bounces={false}
          mixedContentMode="always"
          allowUniversalAccessFromFileURLs
          contentInsetAdjustmentBehavior="never"
          setSupportMultipleWindows={false}
          allowsInlineMediaPlayback
          allowsPictureInPictureMediaPlayback
          mediaPlaybackRequiresUserAction={false}
          style={{ flex: 1, backgroundColor: background }}
          onError={() => fail("Browser viewer could not load. Reconnect to try again.")}
          onHttpError={() => fail("Browser viewer could not load. Reconnect to try again.")}
          onContentProcessDidTerminate={processTerminated}
          onRenderProcessGone={processTerminated}
          onShouldStartLoadWithRequest={(request) =>
            request.url === "about:blank" || request.url === source.baseUrl
          }
          onMessage={(event) => {
            if (!active.current || failed.current) return;
            const message = previewStreamMessage(event.nativeEvent.data);
            if (message === null) return;
            switch (message.type) {
              case "control": {
                setControl(message);
                const previousDialog = dialogRef.current;
                dialogRef.current = message.dialog;
                setPromptText((draft) => previewPromptText(previousDialog, message.dialog, draft));
                onControl?.(message);
                return;
              }
              case "unauthorized":
                if (!onUnauthorized()) {
                  fail("This session can't open the browser stream. Reconnect to try again.");
                }
                return;
              case "gone":
                setGone(true);
                fail("This tab was closed.");
                onGone?.();
                return;
              case "hostSetup":
                setHostSetup({ need: message.need, command: message.command });
                fail(previewStreamHostSetupMessage(message));
                return;
              case "viewport":
                onViewport?.(message);
                return;
              case "clipboard":
                void Clipboard.setStringAsync(message.text).catch(() => undefined);
                return;
              case "download":
                offerDownload(message);
                return;
              case "fileChooser":
                setFileChooser(message.chooser);
                return;
              case "pictureInPicture":
                onPictureInPicture?.(message, message.detail);
                return;
              case "status":
                clearBootstrapTimer();
                if (message.status === "error") {
                  fail(message.detail ?? "Browser stream failed.");
                  return;
                }
                setStatus(message.status);
                if (message.status === "connecting") {
                  // Dialogs may have closed and reopened while this viewer was disconnected.
                  dialogRef.current = null;
                  setControl(null);
                  onControl?.(null);
                }
                onStreamingChange?.(message.status === "streaming");
                if (message.status === "streaming") onStreaming();
            }
          }}
        />
        {pdfUri ? (
          <View className="absolute inset-0">
            <PdfPreview key={pdfUri} uri={pdfUri} />
          </View>
        ) : null}
      </View>
      {!compact && fileChooser && control?.controller === "you" ? (
        <View className="absolute inset-x-3 top-16 gap-3 rounded-xl border border-secondary-border bg-secondary p-4">
          <AppText className="text-sm text-secondary-foreground">
            The page asks for {fileChooser.multiple ? "files" : "a file"}.
          </AppText>
          <View className="flex-row justify-end gap-3">
            <Button label="Cancel" tone="text" onPress={() => answerFileChooser(false)} />
            <Button
              label={`Choose ${fileChooser.multiple ? "files" : "file"}`}
              tone="secondary"
              onPress={() => answerFileChooser(true)}
            />
          </View>
        </View>
      ) : null}
      {!compact && control?.dialog ? (
        <View className="absolute inset-x-3 top-16 gap-3 rounded-xl border border-secondary-border bg-secondary p-4">
          <AppText className="text-sm text-secondary-foreground">{control.dialog.message}</AppText>
          {control.controller === "you" ? (
            <>
              {control.dialog.type === "prompt" ? (
                <TextInput
                  accessibilityLabel="Dialog response"
                  className="rounded-lg border border-input-border bg-input px-3 py-2 text-foreground"
                  placeholderTextColorClassName="accent-placeholder"
                  selectionColorClassName="accent-focus/32"
                  value={promptText}
                  onChangeText={setPromptText}
                />
              ) : null}
              <View className="flex-row justify-end gap-3">
                <Button
                  label="Dismiss"
                  tone="text"
                  onPress={() => command({ type: "dialog", accept: false })}
                />
                <Button
                  label="Accept"
                  tone="secondary"
                  onPress={() =>
                    command({
                      type: "dialog",
                      accept: true,
                      ...(control.dialog?.type === "prompt" ? { promptText } : {}),
                    })
                  }
                />
              </View>
            </>
          ) : (
            <AppText className="text-xs text-foreground-muted">Take control to respond.</AppText>
          )}
        </View>
      ) : null}
      {status !== "streaming" ? (
        <View
          className="absolute inset-0 items-center justify-center gap-4 px-6"
          style={{ backgroundColor: background }}
        >
          {status === "connecting" ? <AppActivityIndicator colorClassName="accent-icon" /> : null}
          {compact ? null : (
            <AppText
              accessibilityLiveRegion="polite"
              className="text-center text-sm text-foreground-muted"
            >
              {status === "error" ? error : "Connecting to browser..."}
            </AppText>
          )}
          {status === "error" && hostSetup && !compact ? (
            <Pressable
              accessibilityRole="button"
              accessibilityLabel={`Copy ${hostSetup.command}`}
              className="w-full flex-row items-center gap-2 rounded-xl border border-secondary-border bg-secondary px-4 py-3"
              onPress={() => {
                void Clipboard.setStringAsync(hostSetup.command).catch(() => undefined);
              }}
            >
              <AppText selectable className="flex-1 font-mono text-sm text-secondary-foreground">
                {hostSetup.command}
              </AppText>
              <AppText className="text-xs text-foreground-muted">Copy</AppText>
            </Pressable>
          ) : null}
          {status === "error" && !gone && !compact ? (
            <View className="items-center">
              <Button
                label={hostSetup ? "Try again" : "Reconnect"}
                tone="secondary"
                onPress={onRetry}
              />
            </View>
          ) : null}
        </View>
      ) : null}
    </View>
  );
}