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

WorkspaceConnectionTitle.tsx

187 lines6.4 KB

1import type { NativeStackNavigationOptions } from "@react-navigation/native-stack";
2import { useEffect, useRef, useState, type ReactNode } from "react";
3import { ActivityIndicator, Animated, Platform, Pressable, View } from "react-native";
4​
5import { SymbolView } from "../../components/AppSymbol";
6import { AppText as Text } from "../../components/AppText";
7import { useAndroidControlSizing } from "../../components/useAndroidControlSizing";
8import {
9 brandTitleOffset,
10 CompactBrandTitle,
11 getCompactBrandHeaderOptions,
12} from "../../components/CompactBrandTitle";
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;
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: {
56 readonly children: ReactNode;
57 readonly grow?: boolean;
58 readonly maxWidth?: number;
59}) {
60 const opacity = useRef(new Animated.Value(0)).current;
61​
62 useEffect(() => {
63 const animation = Animated.timing(opacity, {
64 duration: FADE_IN_MS,
65 toValue: 1,
66 useNativeDriver: false,
67 });
68 animation.start();
69 return () => animation.stop();
70 }, [opacity]);
71​
72 return (
73 <Animated.View
74 style={[
75 { alignItems: "center", flexDirection: "row", maxWidth: props.maxWidth, opacity },
76 props.grow ? { flex: 1, minWidth: 0 } : null,
77 ]}
78 >
79 {props.children}
80 </Animated.View>
81 );
83​
84/**
85 * Renders the brand/title slot of a thread-list surface, swapping the brand
86 * for the workspace connection status while an environment is unavailable.
87 *
88 * Both states occupy the same slot, so connection changes never shift the
89 * layout below. While connected the brand renders untouched — no wrapper —
90 * keeping the native header item on the exact element tree that predates the
91 * status swap. Replaces the old WorkspaceConnectionStatus pill, which inserted
92 * a row above the thread list.
93 */
94export function WorkspaceConnectionTitle(props: {
95 /** Content shown while connected (brand lockup or a screen title). */
96 readonly brand: ReactNode;
97 /** Opens environment settings. Status is not pressable when omitted. */
98 readonly onPress?: () => void;
99 /** Fill the available row width (in-flow headers) instead of hugging content (native title slots). */
100 readonly grow?: boolean;
101 readonly size?: "navbar" | "pageTitle";
102 /** Horizontal correction so the status aligns with the brand in native title slots. */
103 readonly statusOffset?: number;
104 /** Space available beside the native header actions. */
105 readonly maxWidth?: number;
106}) {
107 const status = useDelayedConnectionStatus();
108 const size = props.size ?? "navbar";
109 const { scale } = useAndroidControlSizing();
110​
111 if (status === null) {
112 return props.grow ? (
113 <View style={{ alignItems: "center", flex: 1, flexDirection: "row", minWidth: 0 }}>
114 {props.brand}
115 </View>
116 ) : (
117 <>{props.brand}</>
118 );
119 }
120​
121 return (
122 <StatusFadeIn grow={props.grow} maxWidth={props.maxWidth}>
123 <Pressable
124 accessibilityHint="Opens environment settings"
125 accessibilityLabel={status.label}
126 accessibilityRole="button"
127 disabled={props.onPress === undefined}
128 hitSlop={8}
129 onPress={props.onPress}
130 className="flex-row items-center gap-2"
131 style={[
132 { flexShrink: 1, marginLeft: props.statusOffset ?? 0 },
133 Platform.OS === "android" && { gap: 7 * scale },
134 ]}
135 >
136 {status.showsProgress ? (
137 <ActivityIndicator
138 colorClassName={"accent-icon-muted"}
139 size={Platform.OS === "android" ? Math.round(20 * scale) : "small"}
140 />
141 ) : (
142 <SymbolView
143 name="wifi.slash"
144 size={Math.round((size === "pageTitle" ? 17 : 15) * scale)}
145 tintColorClassName={"accent-icon-muted"}
146 type="monochrome"
147 />
148 )}
149 <Text
150 className="font-t3-bold text-foreground-muted"
151 numberOfLines={1}
152 style={{ flexShrink: 1, fontSize: (size === "pageTitle" ? 20 : 16) * scale }}
153 >
154 {status.label}
155 </Text>
156 </Pressable>
157 </StatusFadeIn>
158 );
160​
161/**
162 * getCompactBrandHeaderOptions with the brand slot upgraded to the
163 * connection-status swap. Screens with an environment-settings callback apply
164 * this over the static brand options at mount.
165 */
166export function getConnectionAwareBrandHeaderOptions(opts: {
167 readonly headerWidth: number;
168 readonly trailingItemCount?: number;
169 readonly onOpenEnvironments: () => void;
170 readonly fallbackTitleStyle?: NativeStackNavigationOptions["headerTitleStyle"];
171}): NativeStackNavigationOptions {
172 // Leave room for bar margins, title spacing and the 44-point native actions.
173 // Long status labels must not push Settings into UIKit's overflow menu.
174 const maxWidth = Math.max(0, opts.headerWidth - 64 - 44 * (opts.trailingItemCount ?? 1));
175​
176 return {
177 ...getCompactBrandHeaderOptions(opts.fallbackTitleStyle),
178 headerTitle: () => (
179 <WorkspaceConnectionTitle
180 brand={<CompactBrandTitle />}
181 maxWidth={maxWidth}
182 onPress={opts.onOpenEnvironments}
183 statusOffset={brandTitleOffset()}
184 />
185 ),
186 };