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

WorkspaceConnectionTitle.tsx

181 lines6.3 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 useEffect(() => {
72 if (!animate) return;
73 const animation = Animated.timing(opacity, {
74 duration: FADE_IN_MS,
75 toValue: 1,
76 useNativeDriver: false,
77 });
78 animation.start();
79 return () => animation.stop();
80 }, [animate, opacity]);
81​
82 return (
83 <Animated.View
84 style={[
85 {
86 alignItems: "center",
87 flexDirection: "row",
88 maxWidth: props.maxWidth,
89 // A hidden tab never runs the fade, so its value would still be 0 when the tab returns.
90 opacity: animate ? opacity : 1,
91 },
92 props.grow ? { flex: 1, minWidth: 0 } : null,
93 ]}
94 >
95 {props.children}
96 </Animated.View>
97 );
99​
100export function WorkspaceConnectionTitle(props: {
101 readonly brand: ReactNode;
102 readonly onPress?: () => void;
103 readonly grow?: boolean;
104 readonly size?: "navbar" | "pageTitle";
105 readonly statusOffset?: number;
106 readonly maxWidth?: number;
107}) {
108 const status = use(ConnectionStatusContext);
109 const size = props.size ?? "navbar";
110 const { scale } = useAndroidControlSizing();
111​
112 if (status === null) {
113 return props.grow ? (
114 <View style={{ alignItems: "center", flex: 1, flexDirection: "row", minWidth: 0 }}>
115 {props.brand}
116 </View>
117 ) : (
118 <>{props.brand}</>
119 );
120 }
121​
122 return (
123 <StatusFadeIn animate={status.fadeIn} grow={props.grow} maxWidth={props.maxWidth}>
124 <Pressable
125 accessibilityHint="Opens environment settings"
126 accessibilityLabel={status.label}
127 accessibilityRole="button"
128 disabled={props.onPress === undefined}
129 hitSlop={8}
130 onPress={props.onPress}
131 className="flex-row items-center gap-2"
132 style={[
133 { flexShrink: 1, marginLeft: props.statusOffset ?? 0 },
134 Platform.OS === "android" && { gap: 7 * scale },
135 ]}
136 >
137 {status.showsProgress ? (
138 <AppActivityIndicator
139 colorClassName={"accent-icon-muted"}
140 size={Platform.OS === "android" ? Math.round(20 * scale) : "small"}
141 />
142 ) : (
143 <SymbolView
144 name="wifi.slash"
145 size={Math.round((size === "pageTitle" ? 17 : 15) * scale)}
146 tintColorClassName={"accent-icon-muted"}
147 type="monochrome"
148 />
149 )}
150 <Text
151 className="font-t3-bold text-foreground-muted"
152 numberOfLines={1}
153 style={{ flexShrink: 1, fontSize: (size === "pageTitle" ? 20 : 16) * scale }}
154 >
155 {status.label}
156 </Text>
157 </Pressable>
158 </StatusFadeIn>
159 );
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 const maxWidth = Math.max(0, opts.headerWidth - 64 - 44 * (opts.trailingItemCount ?? 1));
169​
170 return {
171 ...getCompactBrandHeaderOptions(opts.fallbackTitleStyle),
172 headerTitle: () => (
173 <WorkspaceConnectionTitle
174 brand={<CompactBrandTitle />}
175 maxWidth={maxWidth}
176 onPress={opts.onOpenEnvironments}
177 statusOffset={brandTitleOffset()}
178 />
179 ),
180 };