Open History to see the last commit
1import type { NativeStackNavigationOptions } from "@react-navigation/native-stack";
2import { createContext, use, useEffect, useMemo, useState, type ReactNode } from "react";
3import { Animated, Platform, Pressable, View } from "react-native";
4
5import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
6import { SymbolView } from "../../components/ui/AppSymbol";
7import { AppText as Text } from "../../components/ui/AppText";
8import { useAndroidControlSizing } from "../../components/ui/useAndroidControlSizing";
9import {
10 brandTitleOffset,
11 CompactBrandTitle,
12 getCompactBrandHeaderOptions,
13} from "../../components/CompactBrandTitle";
14import { useWorkspaceState } from "../../state/workspace";
15import {
16 workspaceConnectionStatusPresentation,
17 type WorkspaceConnectionStatusPresentation,
18} from "./workspace-connection-status";
19
20const STATUS_SHOW_DELAY_MS = 800;
21const FADE_IN_MS = 250;
22
23type ConnectionStatus = WorkspaceConnectionStatusPresentation & {
24 /** Only while the status first appears. A header that shows it later shows it at once. */
25 readonly fadeIn: boolean;
26};
27
28const ConnectionStatusContext = createContext<ConnectionStatus | null>(null);
29
30/**
31 * The delay hides brief reconnects, so it is the workspace's, not each header's: every header
32 * shows the status from the same moment. Headers read it from context instead of subscribing
33 * themselves, because a hidden Android tab drops its subscriptions and showed a stale status for
34 * a moment when it came back, while context still reaches it.
35 */
36export function WorkspaceConnectionStatusProvider(props: { readonly children: ReactNode }) {
37 const { state } = useWorkspaceState();
38 const presentation = workspaceConnectionStatusPresentation(state);
39 const hasStatus = presentation !== null;
40 const [phase, setPhase] = useState<"waiting" | "fading" | "shown">("waiting");
41
42 useEffect(() => {
43 if (!hasStatus) return;
44 const timers = [
45 setTimeout(() => setPhase("fading"), STATUS_SHOW_DELAY_MS),
46 setTimeout(() => setPhase("shown"), STATUS_SHOW_DELAY_MS + FADE_IN_MS),
47 ];
48 return () => {
49 for (const timer of timers) clearTimeout(timer);
50 setPhase("waiting");
51 };
52 }, [hasStatus]);
53
54 const label = presentation?.label;
55 const showsProgress = presentation?.showsProgress ?? false;
56 const status = useMemo(
57 () =>
58 label === undefined || phase === "waiting"
59 ? null
60 : { label, showsProgress, fadeIn: phase === "fading" },
61 [label, showsProgress, phase],
62 );
63 return <ConnectionStatusContext value={status}>{props.children}</ConnectionStatusContext>;
64}
65
66/**
67 * Deliberately JS-driven: this can mount inside a native header item
68 * (RNSScreenStackHeaderSubview), where native-driver animated nodes blank the
69 * re-hosted view entirely. The JS driver updates opacity through the ordinary
70 * style path, which those subviews handle.
71 */
72function StatusFadeIn(props: {
73 readonly children: ReactNode;
74 readonly animate: boolean;
75 readonly grow?: boolean;
76 readonly maxWidth?: number;
77}) {
78 const { animate } = props;
79 const [opacity] = useState(() => new Animated.Value(0));
80
81 useEffect(() => {
82 if (!animate) return;
83 const animation = Animated.timing(opacity, {
84 duration: FADE_IN_MS,
85 toValue: 1,
86 useNativeDriver: false,
87 });
88 animation.start();
89 return () => animation.stop();
90 }, [animate, opacity]);
91
92 return (
93 <Animated.View
94 style={[
95 {
96 alignItems: "center",
97 flexDirection: "row",
98 maxWidth: props.maxWidth,
99 // A hidden tab never runs the fade, so its value would still be 0 when the tab returns.
100 opacity: animate ? opacity : 1,
101 },
102 props.grow ? { flex: 1, minWidth: 0 } : null,
103 ]}
104 >
105 {props.children}
106 </Animated.View>
107 );
108}
109
110export function WorkspaceConnectionTitle(props: {
111 readonly brand: ReactNode;
112 readonly onPress?: () => void;
113 readonly grow?: boolean;
114 readonly size?: "navbar" | "pageTitle";
115 readonly statusOffset?: number;
116 readonly maxWidth?: number;
117}) {
118 const status = use(ConnectionStatusContext);
119 const size = props.size ?? "navbar";
120 const { scale } = useAndroidControlSizing();
121
122 if (status === null) {
123 return props.grow ? (
124 <View style={{ alignItems: "center", flex: 1, flexDirection: "row", minWidth: 0 }}>
125 {props.brand}
126 </View>
127 ) : (
128 <>{props.brand}</>
129 );
130 }
131
132 return (
133 <StatusFadeIn animate={status.fadeIn} grow={props.grow} maxWidth={props.maxWidth}>
134 <Pressable
135 accessibilityHint="Opens environment settings"
136 accessibilityLabel={status.label}
137 accessibilityRole="button"
138 disabled={props.onPress === undefined}
139 hitSlop={8}
140 onPress={props.onPress}
141 className="flex-row items-center gap-2"
142 style={[
143 { flexShrink: 1, marginLeft: props.statusOffset ?? 0 },
144 Platform.OS === "android" && { gap: 7 * scale },
145 ]}
146 >
147 {status.showsProgress ? (
148 <AppActivityIndicator
149 colorClassName={"accent-icon-muted"}
150 size={Platform.OS === "android" ? Math.round(20 * scale) : "small"}
151 />
152 ) : (
153 <SymbolView
154 name="wifi.slash"
155 size={Math.round((size === "pageTitle" ? 17 : 15) * scale)}
156 tintColorClassName={"accent-icon-muted"}
157 type="monochrome"
158 />
159 )}
160 <Text
161 className="font-t3-bold text-foreground-muted"
162 numberOfLines={1}
163 style={{ flexShrink: 1, fontSize: (size === "pageTitle" ? 20 : 16) * scale }}
164 >
165 {status.label}
166 </Text>
167 </Pressable>
168 </StatusFadeIn>
169 );
170}
171
172export function getConnectionAwareBrandHeaderOptions(opts: {
173 readonly headerWidth: number;
174 readonly trailingItemCount?: number;
175 readonly onOpenEnvironments: () => void;
176 readonly fallbackTitleStyle?: NativeStackNavigationOptions["headerTitleStyle"];
177}): NativeStackNavigationOptions {
178 const maxWidth = Math.max(0, opts.headerWidth - 64 - 44 * (opts.trailingItemCount ?? 1));
179
180 return {
181 ...getCompactBrandHeaderOptions(opts.fallbackTitleStyle),
182 headerTitle: () => (
183 <WorkspaceConnectionTitle
184 brand={<CompactBrandTitle />}
185 maxWidth={maxWidth}
186 onPress={opts.onOpenEnvironments}
187 statusOffset={brandTitleOffset()}
188 />
189 ),
190 };
191}