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

ThreadBrowserFloat.tsx

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