Open History to see the last commit
1import type { NativeStackNavigationOptions } from "@react-navigation/native-stack";
2import { useEffect, useRef, 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 { useWorkspaceState } from "../../state/workspace";
15import {
16 workspaceConnectionStatusPresentation,
17 type WorkspaceConnectionStatusPresentation,
18} from "./workspace-connection-status";
19
20const STATUS_SHOW_DELAY_MS = 800;
21const FADE_IN_MS = 250;
22
23function useDelayedConnectionStatus(): WorkspaceConnectionStatusPresentation | null {
24 const { state } = useWorkspaceState();
25 const presentation = workspaceConnectionStatusPresentation(state);
26 const hasStatus = presentation !== null;
27 const [visible, setVisible] = useState(false);
28
29 useEffect(() => {
30 if (!hasStatus) {
31 setVisible(false);
32 return;
33 }
34 const timer = setTimeout(() => setVisible(true), STATUS_SHOW_DELAY_MS);
35 return () => clearTimeout(timer);
36 }, [hasStatus]);
37
38 return visible ? presentation : null;
39}
40
41/**
42 * Deliberately JS-driven: this can mount inside a native header item
43 * (RNSScreenStackHeaderSubview), where native-driver animated nodes blank the
44 * re-hosted view entirely. The JS driver updates opacity through the ordinary
45 * style path, which those subviews handle.
46 */
47function StatusFadeIn(props: {
48 readonly children: ReactNode;
49 readonly grow?: boolean;
50 readonly maxWidth?: number;
51}) {
52 const opacity = useRef(new Animated.Value(0)).current;
53
54 useEffect(() => {
55 const animation = Animated.timing(opacity, {
56 duration: FADE_IN_MS,
57 toValue: 1,
58 useNativeDriver: false,
59 });
60 animation.start();
61 return () => animation.stop();
62 }, [opacity]);
63
64 return (
65 <Animated.View
66 style={[
67 { alignItems: "center", flexDirection: "row", maxWidth: props.maxWidth, opacity },
68 props.grow ? { flex: 1, minWidth: 0 } : null,
69 ]}
70 >
71 {props.children}
72 </Animated.View>
73 );
74}
75
76export function WorkspaceConnectionTitle(props: {
77 readonly brand: ReactNode;
78 readonly onPress?: () => void;
79 readonly grow?: boolean;
80 readonly size?: "navbar" | "pageTitle";
81 readonly statusOffset?: number;
82 readonly maxWidth?: number;
83}) {
84 const status = useDelayedConnectionStatus();
85 const size = props.size ?? "navbar";
86 const { scale } = useAndroidControlSizing();
87
88 if (status === null) {
89 return props.grow ? (
90 <View style={{ alignItems: "center", flex: 1, flexDirection: "row", minWidth: 0 }}>
91 {props.brand}
92 </View>
93 ) : (
94 <>{props.brand}</>
95 );
96 }
97
98 return (
99 <StatusFadeIn grow={props.grow} maxWidth={props.maxWidth}>
100 <Pressable
101 accessibilityHint="Opens environment settings"
102 accessibilityLabel={status.label}
103 accessibilityRole="button"
104 disabled={props.onPress === undefined}
105 hitSlop={8}
106 onPress={props.onPress}
107 className="flex-row items-center gap-2"
108 style={[
109 { flexShrink: 1, marginLeft: props.statusOffset ?? 0 },
110 Platform.OS === "android" && { gap: 7 * scale },
111 ]}
112 >
113 {status.showsProgress ? (
114 <AppActivityIndicator
115 colorClassName={"accent-icon-muted"}
116 size={Platform.OS === "android" ? Math.round(20 * scale) : "small"}
117 />
118 ) : (
119 <SymbolView
120 name="wifi.slash"
121 size={Math.round((size === "pageTitle" ? 17 : 15) * scale)}
122 tintColorClassName={"accent-icon-muted"}
123 type="monochrome"
124 />
125 )}
126 <Text
127 className="font-t3-bold text-foreground-muted"
128 numberOfLines={1}
129 style={{ flexShrink: 1, fontSize: (size === "pageTitle" ? 20 : 16) * scale }}
130 >
131 {status.label}
132 </Text>
133 </Pressable>
134 </StatusFadeIn>
135 );
136}
137
138export function getConnectionAwareBrandHeaderOptions(opts: {
139 readonly headerWidth: number;
140 readonly trailingItemCount?: number;
141 readonly onOpenEnvironments: () => void;
142 readonly fallbackTitleStyle?: NativeStackNavigationOptions["headerTitleStyle"];
143}): NativeStackNavigationOptions {
144 const maxWidth = Math.max(0, opts.headerWidth - 64 - 44 * (opts.trailingItemCount ?? 1));
145
146 return {
147 ...getCompactBrandHeaderOptions(opts.fallbackTitleStyle),
148 headerTitle: () => (
149 <WorkspaceConnectionTitle
150 brand={<CompactBrandTitle />}
151 maxWidth={maxWidth}
152 onPress={opts.onOpenEnvironments}
153 statusOffset={brandTitleOffset()}
154 />
155 ),
156 };
157}