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

ThreadBrowserFloat.tsx

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