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

WorkspaceConnectionTitle.tsx

182 lines6.4 KB

1import type { NativeStackNavigationOptions } from "@react-navigation/native-stack";
2import { createContext, use, useEffect, 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 { useDeadlinePassed } from "../../lib/clock";
15import { useWorkspaceState } from "../../state/workspace";
16import {
17 workspaceConnectionStatusPresentation,
18 type WorkspaceConnectionStatusPresentation,
19} from "./workspace-connection-status";
20​
21const STATUS_SHOW_DELAY_MS = 800;
22const FADE_IN_MS = 250;
23​
24type ConnectionStatus = WorkspaceConnectionStatusPresentation & {
25 /** Only while the status first appears. A header that shows it later shows it at once. */
26 readonly fadeIn: boolean;
27};
28​
29const ConnectionStatusContext = createContext<ConnectionStatus | null>(null);
30​
31/**
32 * The delay hides brief reconnects, so it is the workspace's, not each header's: every header
33 * shows the status from the same moment. Headers read it from context instead of subscribing
34 * themselves, because a hidden Android tab drops its subscriptions and showed a stale status for
35 * a moment when it came back, while context still reaches it.
36 */
37export function WorkspaceConnectionStatusProvider(props: { readonly children: ReactNode }) {
38 const { state } = useWorkspaceState();
39 const presentation = workspaceConnectionStatusPresentation(state);
40 const hasStatus = presentation !== null;
41 // When the current status run began; a gap in the status restarts the delay.
42 const [statusSince, setStatusSince] = useState(() => (hasStatus ? Date.now() : null));
43 if (hasStatus !== (statusSince !== null)) {
44 // oxlint-disable-next-line react/purity -- Reads the clock only in the render-phase adjustment that runs when the status appears.
45 setStatusSince(hasStatus ? Date.now() : null);
46 }
47 const since = hasStatus ? statusSince : null;
48 const fadeStarted = useDeadlinePassed(since === null ? null : since + STATUS_SHOW_DELAY_MS);
49 const fadeEnded = useDeadlinePassed(
50 since === null ? null : since + STATUS_SHOW_DELAY_MS + FADE_IN_MS,
51 );
52​
53 const label = presentation?.label;
54 const showsProgress = presentation?.showsProgress ?? false;
55 const status =
56 label === undefined || since === null || !fadeStarted
57 ? null
58 : { label, showsProgress, fadeIn: !fadeEnded };
59 return <ConnectionStatusContext value={status}>{props.children}</ConnectionStatusContext>;
61​
62/** Native-driver animation blanks views re-hosted in RNSScreenStackHeaderSubview; use the JS style path. */
63function StatusFadeIn(props: {
64 readonly children: ReactNode;
65 readonly animate: boolean;
66 readonly grow?: boolean;
67 readonly maxWidth?: number;
68}) {
69 const { animate } = props;
70 const [opacity] = useState(() => new Animated.Value(0));
71 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- runs the Animated opacity fade-in and stops it on cleanup
72 useEffect(() => {
73 if (!animate) return;
74 const animation = Animated.timing(opacity, {
75 duration: FADE_IN_MS,
76 toValue: 1,
77 useNativeDriver: false,
78 });
79 animation.start();
80 return () => animation.stop();
81 }, [animate, opacity]);
82​
83 return (
84 <Animated.View
85 style={[
86 {
87 alignItems: "center",
88 flexDirection: "row",
89 maxWidth: props.maxWidth,
90 // A hidden tab never runs the fade, so its value would still be 0 when the tab returns.
91 opacity: animate ? opacity : 1,
92 },
93 props.grow ? { flex: 1, minWidth: 0 } : null,
94 ]}
95 >
96 {props.children}
97 </Animated.View>
98 );
100​
101export function WorkspaceConnectionTitle(props: {
102 readonly brand: ReactNode;
103 readonly onPress?: () => void;
104 readonly grow?: boolean;
105 readonly size?: "navbar" | "pageTitle";
106 readonly statusOffset?: number;
107 readonly maxWidth?: number;
108}) {
109 const status = use(ConnectionStatusContext);
110 const size = props.size ?? "navbar";
111 const { scale } = useAndroidControlSizing();
112​
113 if (status === null) {
114 return props.grow ? (
115 <View style={{ alignItems: "center", flex: 1, flexDirection: "row", minWidth: 0 }}>
116 {props.brand}
117 </View>
118 ) : (
119 <>{props.brand}</>
120 );
121 }
122​
123 return (
124 <StatusFadeIn animate={status.fadeIn} grow={props.grow} maxWidth={props.maxWidth}>
125 <Pressable
126 accessibilityHint="Opens environment settings"
127 accessibilityLabel={status.label}
128 accessibilityRole="button"
129 disabled={props.onPress === undefined}
130 hitSlop={8}
131 onPress={props.onPress}
132 className="flex-row items-center gap-2"
133 style={[
134 { flexShrink: 1, marginLeft: props.statusOffset ?? 0 },
135 Platform.OS === "android" && { gap: 7 * scale },
136 ]}
137 >
138 {status.showsProgress ? (
139 <AppActivityIndicator
140 colorClassName={"accent-icon-muted"}
141 size={Platform.OS === "android" ? Math.round(20 * scale) : "small"}
142 />
143 ) : (
144 <SymbolView
145 name="wifi.slash"
146 size={Math.round((size === "pageTitle" ? 17 : 15) * scale)}
147 tintColorClassName={"accent-icon-muted"}
148 type="monochrome"
149 />
150 )}
151 <Text
152 className="font-t3-bold text-foreground-muted"
153 numberOfLines={1}
154 style={{ flexShrink: 1, fontSize: (size === "pageTitle" ? 20 : 16) * scale }}
155 >
156 {status.label}
157 </Text>
158 </Pressable>
159 </StatusFadeIn>
160 );
162​
163export function getConnectionAwareBrandHeaderOptions(opts: {
164 readonly headerWidth: number;
165 readonly trailingItemCount?: number;
166 readonly onOpenEnvironments: () => void;
167 readonly fallbackTitleStyle?: NativeStackNavigationOptions["headerTitleStyle"];
168}): NativeStackNavigationOptions {
169 const maxWidth = Math.max(0, opts.headerWidth - 64 - 44 * (opts.trailingItemCount ?? 1));
170​
171 return {
172 ...getCompactBrandHeaderOptions(opts.fallbackTitleStyle),
173 headerTitle: () => (
174 <WorkspaceConnectionTitle
175 brand={<CompactBrandTitle />}
176 maxWidth={maxWidth}
177 onPress={opts.onOpenEnvironments}
178 statusOffset={brandTitleOffset()}
179 />
180 ),
181 };