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

ThreadBrowserFloat.tsx

124 lines4.6 KB

1import { useIsFocused } from "@react-navigation/native";
2import type { EnvironmentId, PreviewSessionSnapshot, ThreadId } from "@t3tools/contracts";
3import { useCallback, useEffect, useRef, useState } from "react";
4import { AppState, Pressable, View } from "react-native";
5import Animated, { FadeIn, FadeOut, ReduceMotion } from "react-native-reanimated";
6​
7import { SymbolView } from "../../components/ui/AppSymbol";
8import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
9import { browserTabTitle } from "./browserTabs";
10import { PreviewStreamWebView } from "./PreviewStreamWebView";
11​
12const PLAYER_LONG_SIDE = 184;
13const PLAYER_ENTERING = FadeIn.duration(180).reduceMotion(ReduceMotion.System);
14const PLAYER_EXITING = FadeOut.duration(120).reduceMotion(ReduceMotion.System);
15​
16export function ThreadBrowserFloat(props: {
17 readonly environmentId: EnvironmentId;
18 readonly threadId: ThreadId;
19 readonly tabs: ReadonlyArray<PreviewSessionSnapshot>;
20 readonly loaded: boolean;
21 readonly top: number;
22 readonly onOpen: (tabId: string) => void;
23}) {
24 const [tabId, setTabId] = useState<string | null>(null);
25 const known = useRef<ReadonlyMap<string, string | undefined> | null>(null);
26 useEffect(() => {
27 if (!props.loaded) return;
28 const previous = known.current;
29 known.current = new Map(props.tabs.map((tab) => [tab.tabId, tab.revealRequest?.id]));
30 if (previous === null) return;
31 const opened = props.tabs.findLast(
32 (tab) =>
33 tab.reveal === true &&
34 (tab.revealRequest
35 ? previous.get(tab.tabId) !== tab.revealRequest.id
36 : !previous.has(tab.tabId)),
37 );
38 if (opened !== undefined) setTabId(opened.tabId);
39 }, [props.loaded, props.tabs]);
40 const focused = useIsFocused();
41 const [foreground, setForeground] = useState(AppState.currentState !== "background");
42 useEffect(() => {
43 const subscription = AppState.addEventListener("change", (state) =>
44 setForeground(state !== "background"),
45 );
46 return () => subscription.remove();
47 }, []);
48 const tab = props.tabs.find((entry) => entry.tabId === tabId) ?? null;
49 const onOpen = props.onOpen;
50 const open = useCallback(() => {
51 if (tabId === null) return;
52 setTabId(null);
53 onOpen(tabId);
54 }, [onOpen, tabId]);
55 const close = useCallback(() => setTabId(null), []);
56 if (!tab || !focused) return null;
57 return (
58 <FloatingBrowserPlayer
59 environmentId={props.environmentId}
60 threadId={props.threadId}
61 tab={tab}
62 live={foreground}
63 top={props.top}
64 onOpen={open}
65 onClose={close}
66 />
67 );
69​
70function FloatingBrowserPlayer(props: {
71 readonly environmentId: EnvironmentId;
72 readonly threadId: ThreadId;
73 readonly tab: PreviewSessionSnapshot;
74 readonly live: boolean;
75 readonly top: number;
76 readonly onOpen: () => void;
77 readonly onClose: () => void;
78}) {
79 const { themeVariables } = useAppearancePreferences();
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 interactive={false}
105 background={background}
106 compact
107 paused={!props.live}
108 onViewport={setViewport}
109 onGone={props.onClose}
110 />
111 </View>
112 </Pressable>
113 <Pressable
114 accessibilityRole="button"
115 accessibilityLabel="Close floating preview"
116 hitSlop={8}
117 onPress={props.onClose}
118 className="absolute right-1.5 top-1.5 size-6 items-center justify-center rounded-full bg-black/55"
119 >
120 <SymbolView name="xmark" size={11} tintColor="#ffffff" type="monochrome" />
121 </Pressable>
122 </Animated.View>
123 );