Open History to see the last commit
1import type {
2 NativeStackHeaderItem,
3 NativeStackNavigationOptions,
4} from "@react-navigation/native-stack";
5import { useEffect, useRef, useState, type ReactNode } from "react";
6import { ActivityIndicator, Animated, Platform, Pressable, View } from "react-native";
7
8import { SymbolView } from "../../components/AppSymbol";
9import { AppText as Text } from "../../components/AppText";
10import { brandTitleOffset, CompactBrandTitle } from "../../components/CompactBrandTitle";
11import { useThemeColor } from "../../lib/useThemeColor";
12import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
13import { useWorkspaceState } from "../../state/workspace";
14import {
15 workspaceConnectionStatusPresentation,
16 type WorkspaceConnectionStatusPresentation,
17} from "./workspace-connection-status";
18
19/**
20 * Delay before a connection interruption surfaces in the title slot. Sub-second
21 * blips (the common reconnect case) resolve without any UI at all.
22 */
23const STATUS_SHOW_DELAY_MS = 800;
24const FADE_IN_MS = 250;
25
26/**
27 * Connection status presentation, debounced for display: null until the
28 * workspace has been in a non-connected state for STATUS_SHOW_DELAY_MS,
29 * then live-updating until the workspace reconnects (null again immediately).
30 */
31function useDelayedConnectionStatus(): WorkspaceConnectionStatusPresentation | null {
32 const { state } = useWorkspaceState();
33 const presentation = workspaceConnectionStatusPresentation(state);
34 const hasStatus = presentation !== null;
35 const [visible, setVisible] = useState(false);
36
37 useEffect(() => {
38 if (!hasStatus) {
39 setVisible(false);
40 return;
41 }
42 const timer = setTimeout(() => setVisible(true), STATUS_SHOW_DELAY_MS);
43 return () => clearTimeout(timer);
44 }, [hasStatus]);
45
46 return visible ? presentation : null;
47}
48
49/**
50 * One-shot entrance fade for the status label. Deliberately JS-driven: this can
51 * mount inside a native header item (RNSScreenStackHeaderSubview), where
52 * native-driver animated nodes blank the re-hosted view entirely. The JS driver
53 * updates opacity through the ordinary style path, which those subviews handle.
54 */
55function StatusFadeIn(props: { readonly children: ReactNode; readonly grow?: boolean }) {
56 const opacity = useRef(new Animated.Value(0)).current;
57
58 useEffect(() => {
59 const animation = Animated.timing(opacity, {
60 duration: FADE_IN_MS,
61 toValue: 1,
62 useNativeDriver: false,
63 });
64 animation.start();
65 return () => animation.stop();
66 }, [opacity]);
67
68 return (
69 <Animated.View
70 style={[
71 { alignItems: "center", flexDirection: "row", opacity },
72 props.grow ? { flex: 1, minWidth: 0 } : null,
73 ]}
74 >
75 {props.children}
76 </Animated.View>
77 );
78}
79
80/**
81 * Renders the brand/title slot of a thread-list surface, swapping the brand
82 * for the workspace connection status while an environment is unavailable.
83 *
84 * Both states occupy the same slot, so connection changes never shift the
85 * layout below. While connected the brand renders untouched — no wrapper —
86 * keeping the native header item on the exact element tree that predates the
87 * status swap. Replaces the old WorkspaceConnectionStatus pill, which inserted
88 * a row above the thread list.
89 */
90export function WorkspaceConnectionTitle(props: {
91 /** Content shown while connected (brand lockup or a screen title). */
92 readonly brand: ReactNode;
93 /** Opens environment settings. Status is not pressable when omitted. */
94 readonly onPress?: () => void;
95 /** Fill the available row width (in-flow headers) instead of hugging content (native title slots). */
96 readonly grow?: boolean;
97 readonly size?: "navbar" | "pageTitle";
98 /** Horizontal correction so the status aligns with the brand in native title slots. */
99 readonly statusOffset?: number;
100}) {
101 const iconColor = String(useThemeColor("--color-icon-muted"));
102 const status = useDelayedConnectionStatus();
103 const size = props.size ?? "navbar";
104
105 if (status === null) {
106 return props.grow ? (
107 <View style={{ alignItems: "center", flex: 1, flexDirection: "row", minWidth: 0 }}>
108 {props.brand}
109 </View>
110 ) : (
111 <>{props.brand}</>
112 );
113 }
114
115 return (
116 <StatusFadeIn grow={props.grow}>
117 <Pressable
118 accessibilityHint="Opens environment settings"
119 accessibilityLabel={status.label}
120 accessibilityRole="button"
121 disabled={props.onPress === undefined}
122 hitSlop={8}
123 onPress={props.onPress}
124 className="flex-row items-center gap-2"
125 style={{ marginLeft: props.statusOffset ?? 0 }}
126 >
127 {status.showsProgress ? (
128 <ActivityIndicator color={iconColor} size="small" />
129 ) : (
130 <SymbolView
131 name="wifi.slash"
132 size={size === "pageTitle" ? 17 : 15}
133 tintColor={iconColor}
134 type="monochrome"
135 />
136 )}
137 <Text
138 className={
139 size === "pageTitle"
140 ? "text-[20px] font-t3-bold text-foreground-muted"
141 : "text-[16px] font-t3-bold text-foreground-muted"
142 }
143 numberOfLines={1}
144 style={{ flexShrink: 1 }}
145 >
146 {status.label}
147 </Text>
148 </Pressable>
149 </StatusFadeIn>
150 );
151}
152
153/**
154 * getCompactBrandHeaderOptions with the brand slot upgraded to the
155 * connection-status swap. Screens with an environment-settings callback apply
156 * this over the static brand options at mount.
157 */
158export function getConnectionAwareBrandHeaderOptions(opts: {
159 readonly onOpenEnvironments: () => void;
160 readonly fallbackTitleStyle?: NativeStackNavigationOptions["headerTitleStyle"];
161}): NativeStackNavigationOptions {
162 if (Platform.OS === "ios" && NATIVE_LIQUID_GLASS_SUPPORTED) {
163 return {
164 headerTitle: "Threads",
165 headerTitleStyle: { color: "transparent", fontSize: 18, fontWeight: "800" },
166 title: "Threads",
167 unstable_headerLeftItems: (): NativeStackHeaderItem[] => [
168 {
169 element: (
170 <WorkspaceConnectionTitle
171 brand={<CompactBrandTitle nativeLeadingItem />}
172 onPress={opts.onOpenEnvironments}
173 statusOffset={brandTitleOffset(true)}
174 />
175 ),
176 hidesSharedBackground: true,
177 type: "custom",
178 },
179 ],
180 };
181 }
182
183 return {
184 headerTitle: () => (
185 <WorkspaceConnectionTitle
186 brand={<CompactBrandTitle />}
187 onPress={opts.onOpenEnvironments}
188 statusOffset={brandTitleOffset(false)}
189 />
190 ),
191 headerTitleStyle: opts.fallbackTitleStyle,
192 title: "Threads",
193 };
194}