The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { createContext, use, useEffect, useState, type ReactNode } from "react"; |
| 2 | import { Animated, Platform, Pressable, View } from "react-native"; |
| 3 | import { useGT, useMessages } from "gt-react-native"; |
| 4 | |
| 5 | import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; |
| 6 | import { SymbolView } from "../../components/ui/AppSymbol"; |
| 7 | import { AppText as Text } from "../../components/ui/AppText"; |
| 8 | import { useAndroidControlSizing } from "../../components/ui/useAndroidControlSizing"; |
| 9 | import { useDeadlinePassed } from "../../lib/clock"; |
| 10 | import { useWorkspaceState } from "../../state/workspace"; |
| 11 | import { |
| 12 | workspaceConnectionStatusPresentation, |
| 13 | type WorkspaceConnectionStatusPresentation, |
| 14 | } from "./workspace-connection-status"; |
| 15 | |
| 16 | const STATUS_SHOW_DELAY_MS = 800; |
| 17 | const FADE_IN_MS = 250; |
| 18 | |
| 19 | type ConnectionStatus = WorkspaceConnectionStatusPresentation & { |
| 20 | /** Only while the status first appears. A header that shows it later shows it at once. */ |
| 21 | readonly fadeIn: boolean; |
| 22 | }; |
| 23 | |
| 24 | const ConnectionStatusContext = createContext<ConnectionStatus | null>(null); |
| 25 | |
| 26 | /** |
| 27 | * The delay hides brief reconnects, so it is the workspace's, not each header's: every header |
| 28 | * shows the status from the same moment. Headers read it from context instead of subscribing |
| 29 | * themselves, because a hidden Android tab drops its subscriptions and showed a stale status for |
| 30 | * a moment when it came back, while context still reaches it. |
| 31 | */ |
| 32 | export function WorkspaceConnectionStatusProvider(props: { readonly children: ReactNode }) { |
| 33 | const { state } = useWorkspaceState(); |
| 34 | const m = useMessages(); |
| 35 | const presentation = workspaceConnectionStatusPresentation(state, m); |
| 36 | const hasStatus = presentation !== null; |
| 37 | // When the current status run began; a gap in the status restarts the delay. |
| 38 | const [statusSince, setStatusSince] = useState(() => (hasStatus ? Date.now() : null)); |
| 39 | if (hasStatus !== (statusSince !== null)) { |
| 40 | // oxlint-disable-next-line react/purity -- Reads the clock only in the render-phase adjustment that runs when the status appears. |
| 41 | setStatusSince(hasStatus ? Date.now() : null); |
| 42 | } |
| 43 | const since = hasStatus ? statusSince : null; |
| 44 | const fadeStarted = useDeadlinePassed(since === null ? null : since + STATUS_SHOW_DELAY_MS); |
| 45 | const fadeEnded = useDeadlinePassed( |
| 46 | since === null ? null : since + STATUS_SHOW_DELAY_MS + FADE_IN_MS, |
| 47 | ); |
| 48 | |
| 49 | const label = presentation?.label; |
| 50 | const showsProgress = presentation?.showsProgress ?? false; |
| 51 | const status = |
| 52 | label === undefined || since === null || !fadeStarted |
| 53 | ? null |
| 54 | : { label, showsProgress, fadeIn: !fadeEnded }; |
| 55 | return <ConnectionStatusContext value={status}>{props.children}</ConnectionStatusContext>; |
| 56 | } |
| 57 | |
| 58 | /** Native-driver animation blanks views re-hosted in RNSScreenStackHeaderSubview; use the JS style path. */ |
| 59 | function StatusFadeIn(props: { |
| 60 | readonly children: ReactNode; |
| 61 | readonly animate: boolean; |
| 62 | readonly grow?: boolean; |
| 63 | readonly maxWidth?: number; |
| 64 | }) { |
| 65 | const { animate } = props; |
| 66 | const [opacity] = useState(() => new Animated.Value(0)); |
| 67 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- runs the Animated opacity fade-in and stops it on cleanup |
| 68 | useEffect(() => { |
| 69 | if (!animate) return; |
| 70 | const animation = Animated.timing(opacity, { |
| 71 | duration: FADE_IN_MS, |
| 72 | toValue: 1, |
| 73 | useNativeDriver: false, |
| 74 | }); |
| 75 | animation.start(); |
| 76 | return () => animation.stop(); |
| 77 | }, [animate, opacity]); |
| 78 | |
| 79 | return ( |
| 80 | <Animated.View |
| 81 | style={[ |
| 82 | { |
| 83 | alignItems: "center", |
| 84 | flexDirection: "row", |
| 85 | maxWidth: props.maxWidth, |
| 86 | // A hidden tab never runs the fade, so its value would still be 0 when the tab returns. |
| 87 | opacity: animate ? opacity : 1, |
| 88 | }, |
| 89 | props.grow ? { flex: 1, minWidth: 0 } : null, |
| 90 | ]} |
| 91 | > |
| 92 | {props.children} |
| 93 | </Animated.View> |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | export function WorkspaceConnectionTitle(props: { |
| 98 | readonly brand: ReactNode; |
| 99 | readonly onPress?: () => void; |
| 100 | readonly grow?: boolean; |
| 101 | readonly size?: "navbar" | "pageTitle"; |
| 102 | readonly statusOffset?: number; |
| 103 | readonly maxWidth?: number; |
| 104 | }) { |
| 105 | const status = use(ConnectionStatusContext); |
| 106 | const gt = useGT(); |
| 107 | const size = props.size ?? "navbar"; |
| 108 | const { scale } = useAndroidControlSizing(); |
| 109 | |
| 110 | if (status === null) { |
| 111 | return props.grow ? ( |
| 112 | <View style={{ alignItems: "center", flex: 1, flexDirection: "row", minWidth: 0 }}> |
| 113 | {props.brand} |
| 114 | </View> |
| 115 | ) : ( |
| 116 | <>{props.brand}</> |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | return ( |
| 121 | <StatusFadeIn animate={status.fadeIn} grow={props.grow} maxWidth={props.maxWidth}> |
| 122 | <Pressable |
| 123 | accessibilityHint={gt("Opens environment settings")} |
| 124 | accessibilityLabel={status.label} |
| 125 | accessibilityRole="button" |
| 126 | disabled={props.onPress === undefined} |
| 127 | hitSlop={8} |
| 128 | onPress={props.onPress} |
| 129 | className="flex-row items-center gap-2" |
| 130 | style={[ |
| 131 | { flexShrink: 1, marginLeft: props.statusOffset ?? 0 }, |
| 132 | Platform.OS === "android" && { gap: 7 * scale }, |
| 133 | ]} |
| 134 | > |
| 135 | {status.showsProgress ? ( |
| 136 | <AppActivityIndicator |
| 137 | colorClassName={"accent-icon-muted"} |
| 138 | size={Platform.OS === "android" ? Math.round(20 * scale) : "small"} |
| 139 | /> |
| 140 | ) : ( |
| 141 | <SymbolView |
| 142 | name="wifi.slash" |
| 143 | size={Math.round((size === "pageTitle" ? 17 : 15) * scale)} |
| 144 | tintColorClassName={"accent-icon-muted"} |
| 145 | type="monochrome" |
| 146 | /> |
| 147 | )} |
| 148 | <Text |
| 149 | className="font-t3-bold text-foreground-muted" |
| 150 | numberOfLines={1} |
| 151 | style={{ flexShrink: 1, fontSize: (size === "pageTitle" ? 20 : 16) * scale }} |
| 152 | > |
| 153 | {status.label} |
| 154 | </Text> |
| 155 | </Pressable> |
| 156 | </StatusFadeIn> |
| 157 | ); |
| 158 | } |