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/** Native-driver animation blanks views re-hosted in RNSScreenStackHeaderSubview; use the JS style path. */
67function StatusFadeIn(props: {
68 readonly children: ReactNode;
69 readonly animate: boolean;
70 readonly grow?: boolean;
71 readonly maxWidth?: number;
72}) {
73 const { animate } = props;
74 const [opacity] = useState(() => new Animated.Value(0));
75
76 useEffect(() => {
77 if (!animate) return;
78 const animation = Animated.timing(opacity, {
79 duration: FADE_IN_MS,
80 toValue: 1,
81 useNativeDriver: false,
82 });
83 animation.start();
84 return () => animation.stop();
85 }, [animate, opacity]);
86
87 return (
88 <Animated.View
89 style={[
90 {
91 alignItems: "center",
92 flexDirection: "row",
93 maxWidth: props.maxWidth,
94 // A hidden tab never runs the fade, so its value would still be 0 when the tab returns.
95 opacity: animate ? opacity : 1,
96 },
97 props.grow ? { flex: 1, minWidth: 0 } : null,
98 ]}
99 >
100 {props.children}
101 </Animated.View>
102 );
103}
104
105export function WorkspaceConnectionTitle(props: {
106 readonly brand: ReactNode;
107 readonly onPress?: () => void;
108 readonly grow?: boolean;
109 readonly size?: "navbar" | "pageTitle";
110 readonly statusOffset?: number;
111 readonly maxWidth?: number;
112}) {
113 const status = use(ConnectionStatusContext);
114 const size = props.size ?? "navbar";
115 const { scale } = useAndroidControlSizing();
116
117 if (status === null) {
118 return props.grow ? (
119 <View style={{ alignItems: "center", flex: 1, flexDirection: "row", minWidth: 0 }}>
120 {props.brand}
121 </View>
122 ) : (
123 <>{props.brand}</>
124 );
125 }
126
127 return (
128 <StatusFadeIn animate={status.fadeIn} grow={props.grow} maxWidth={props.maxWidth}>
129 <Pressable
130 accessibilityHint="Opens environment settings"
131 accessibilityLabel={status.label}
132 accessibilityRole="button"
133 disabled={props.onPress === undefined}
134 hitSlop={8}
135 onPress={props.onPress}
136 className="flex-row items-center gap-2"
137 style={[
138 { flexShrink: 1, marginLeft: props.statusOffset ?? 0 },
139 Platform.OS === "android" && { gap: 7 * scale },
140 ]}
141 >
142 {status.showsProgress ? (
143 <AppActivityIndicator
144 colorClassName={"accent-icon-muted"}
145 size={Platform.OS === "android" ? Math.round(20 * scale) : "small"}
146 />
147 ) : (
148 <SymbolView
149 name="wifi.slash"
150 size={Math.round((size === "pageTitle" ? 17 : 15) * scale)}
151 tintColorClassName={"accent-icon-muted"}
152 type="monochrome"
153 />
154 )}
155 <Text
156 className="font-t3-bold text-foreground-muted"
157 numberOfLines={1}
158 style={{ flexShrink: 1, fontSize: (size === "pageTitle" ? 20 : 16) * scale }}
159 >
160 {status.label}
161 </Text>
162 </Pressable>
163 </StatusFadeIn>
164 );
165}
166
167export function getConnectionAwareBrandHeaderOptions(opts: {
168 readonly headerWidth: number;
169 readonly trailingItemCount?: number;
170 readonly onOpenEnvironments: () => void;
171 readonly fallbackTitleStyle?: NativeStackNavigationOptions["headerTitleStyle"];
172}): NativeStackNavigationOptions {
173 const maxWidth = Math.max(0, opts.headerWidth - 64 - 44 * (opts.trailingItemCount ?? 1));
174
175 return {
176 ...getCompactBrandHeaderOptions(opts.fallbackTitleStyle),
177 headerTitle: () => (
178 <WorkspaceConnectionTitle
179 brand={<CompactBrandTitle />}
180 maxWidth={maxWidth}
181 onPress={opts.onOpenEnvironments}
182 statusOffset={brandTitleOffset()}
183 />
184 ),
185 };
186}