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

WorkspaceConnectionTitle.tsx

183 lines6.2 KB

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