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