Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

WorkspaceConnectionTitle.tsx

192 lines6.4 KB

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