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

ThreadBrowserFloat.tsx

129 lines4.9 KB

1import { useIsFocused } from "@react-navigation/native";
2import type { EnvironmentId, PreviewSessionSnapshot, ThreadId } from "@t3tools/contracts";
3import { previewPdfAssetUrl } from "@t3tools/shared/preview";
4import * as Option from "effect/Option";
5import { useCallback, useEffect, useRef, useState } from "react";
6import { Pressable, View } from "react-native";
7import Animated, { FadeIn, FadeOut, ReduceMotion } from "react-native-reanimated";
8​
9import { SymbolView } from "../../components/ui/AppSymbol";
10import { useAppForeground } from "../../lib/appState";
11import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
12import { browserTabTitle } from "./browserTabs";
13import { PreviewStreamWebView } from "./PreviewStreamWebView";
14import { usePreparedConnection } from "../../state/session";
15​
16const PLAYER_LONG_SIDE = 184;
17const PLAYER_ENTERING = FadeIn.duration(180).reduceMotion(ReduceMotion.System);
18const PLAYER_EXITING = FadeOut.duration(120).reduceMotion(ReduceMotion.System);
19​
20export function ThreadBrowserFloat(props: {
21 readonly environmentId: EnvironmentId;
22 readonly threadId: ThreadId;
23 readonly tabs: ReadonlyArray<PreviewSessionSnapshot>;
24 readonly loaded: boolean;
25 readonly top: number;
26 readonly onOpen: (tabId: string) => void;
27}) {
28 const [tabId, setTabId] = useState<string | null>(null);
29 const known = useRef<ReadonlyMap<string, string | undefined> | null>(null);
30 useEffect(() => {
31 if (!props.loaded) return;
32 const previous = known.current;
33 known.current = new Map(props.tabs.map((tab) => [tab.tabId, tab.revealRequest?.id]));
34 if (previous === null) return;
35 const opened = props.tabs.findLast(
36 (tab) =>
37 tab.reveal === true &&
38 (tab.revealRequest
39 ? previous.get(tab.tabId) !== tab.revealRequest.id
40 : !previous.has(tab.tabId)),
41 );
42 if (opened !== undefined) setTabId(opened.tabId);
43 }, [props.loaded, props.tabs]);
44 const focused = useIsFocused();
45 const foreground = useAppForeground();
46 const tab = props.tabs.find((entry) => entry.tabId === tabId) ?? null;
47 const onOpen = props.onOpen;
48 const open = useCallback(() => {
49 if (tabId === null) return;
50 setTabId(null);
51 onOpen(tabId);
52 }, [onOpen, tabId]);
53 const close = useCallback(() => setTabId(null), []);
54 if (!tab || !focused) return null;
55 return (
56 <FloatingBrowserPlayer
57 environmentId={props.environmentId}
58 threadId={props.threadId}
59 tab={tab}
60 live={foreground}
61 top={props.top}
62 onOpen={open}
63 onClose={close}
64 />
65 );
67​
68function FloatingBrowserPlayer(props: {
69 readonly environmentId: EnvironmentId;
70 readonly threadId: ThreadId;
71 readonly tab: PreviewSessionSnapshot;
72 readonly live: boolean;
73 readonly top: number;
74 readonly onOpen: () => void;
75 readonly onClose: () => void;
76}) {
77 const { themeVariables } = useAppearancePreferences();
78 const prepared = usePreparedConnection(props.environmentId);
79 const httpBaseUrl = Option.isSome(prepared) ? prepared.value.httpBaseUrl : null;
80 const [viewport, setViewport] = useState<{ width: number; height: number } | null>(null);
81 const aspect = Math.min(Math.max(viewport ? viewport.width / viewport.height : 16 / 10, 0.5), 2);
82 const width = aspect >= 1 ? PLAYER_LONG_SIDE : Math.round(PLAYER_LONG_SIDE * aspect);
83 const height = Math.round(width / aspect);
84 const background = themeVariables["--color-sheet-solid"];
85 return (
86 <Animated.View
87 entering={PLAYER_ENTERING}
88 exiting={PLAYER_EXITING}
89 className="absolute right-3 z-30 overflow-hidden rounded-2xl border border-border shadow-md shadow-black/20"
90 style={{ top: props.top, width, height, backgroundColor: background }}
91 >
92 <Pressable
93 accessibilityRole="button"
94 accessibilityLabel={`Open browser, ${browserTabTitle(props.tab)}`}
95 accessibilityHint="Opens the browser tab full screen"
96 onPress={props.onOpen}
97 className="flex-1"
98 >
99 <View pointerEvents="none" className="flex-1">
100 <PreviewStreamWebView
101 environmentId={props.environmentId}
102 threadId={props.threadId}
103 tabId={props.tab.tabId}
104 pdfUri={
105 props.tab.navStatus._tag === "Success"
106 ? previewPdfAssetUrl(props.tab.navStatus.url, httpBaseUrl)
107 : null
108 }
109 interactive={false}
110 background={background}
111 compact
112 paused={!props.live}
113 onViewport={setViewport}
114 onGone={props.onClose}
115 />
116 </View>
117 </Pressable>
118 <Pressable
119 accessibilityRole="button"
120 accessibilityLabel="Close floating preview"
121 hitSlop={8}
122 onPress={props.onClose}
123 className="absolute right-1.5 top-1.5 size-6 items-center justify-center rounded-full bg-black/55"
124 >
125 <SymbolView name="xmark" size={11} tintColor="#ffffff" type="monochrome" />
126 </Pressable>
127 </Animated.View>
128 );