Open History to see the last commit
1import { useEffect } from "react";
2import { View } from "react-native";
3import Animated, {
4 cancelAnimation,
5 Easing,
6 useAnimatedStyle,
7 useSharedValue,
8 withRepeat,
9 withTiming,
10} from "react-native-reanimated";
11
12import type { RemoteClientConnectionState } from "../../lib/connection";
13
14export type ConnectionStatusDotState = RemoteClientConnectionState;
15
16function statusDotTone(state: ConnectionStatusDotState): {
17 readonly dotColor: string;
18 readonly haloColor: string;
19} {
20 switch (state) {
21 // Unsupported is not a failure: the machine is fine, this build just
22 // cannot talk to it, so it wears the same neutral dot as "available".
23 case "available":
24 case "unsupported":
25 return {
26 dotColor: "#9ca3af",
27 haloColor: "rgba(156,163,175,0.42)",
28 };
29 case "connected":
30 return {
31 dotColor: "#34d399",
32 haloColor: "rgba(52,211,153,0.48)",
33 };
34 case "connecting":
35 case "reconnecting":
36 return {
37 dotColor: "#f59e0b",
38 haloColor: "rgba(245,158,11,0.5)",
39 };
40 case "offline":
41 case "error":
42 return {
43 dotColor: "#ef4444",
44 haloColor: "rgba(239,68,68,0.48)",
45 };
46 }
47}
48
49function usePulseAnimation(pulse: boolean) {
50 const pulseProgress = useSharedValue(0);
51
52 useEffect(() => {
53 if (pulse) {
54 pulseProgress.value = withRepeat(
55 withTiming(1, {
56 duration: 1100,
57 easing: Easing.out(Easing.cubic),
58 }),
59 -1,
60 false,
61 );
62 return;
63 }
64
65 cancelAnimation(pulseProgress);
66 pulseProgress.value = withTiming(0, {
67 duration: 180,
68 easing: Easing.out(Easing.quad),
69 });
70 }, [pulse, pulseProgress]);
71
72 return pulseProgress;
73}
74
75export function ConnectionStatusDot(props: {
76 readonly state: ConnectionStatusDotState;
77 readonly pulse: boolean;
78 readonly size?: number;
79}) {
80 const pulseProgress = usePulseAnimation(props.pulse);
81 const tone = statusDotTone(props.state);
82 const dotSize = props.size ?? 10;
83 const haloSize = dotSize + 4;
84 const containerSize = haloSize + 4;
85
86 const haloStyle = useAnimatedStyle(() => ({
87 opacity: props.pulse ? 0.14 + (1 - pulseProgress.value) * 0.3 : 0,
88 transform: [{ scale: 0.78 + pulseProgress.value * 1.16 }],
89 }));
90
91 return (
92 <View
93 style={{
94 width: containerSize,
95 height: containerSize,
96 alignItems: "center",
97 justifyContent: "center",
98 }}
99 >
100 <Animated.View
101 style={[
102 haloStyle,
103 {
104 position: "absolute",
105 width: haloSize,
106 height: haloSize,
107 borderRadius: haloSize / 2,
108 backgroundColor: tone.haloColor,
109 },
110 ]}
111 />
112 <View
113 style={{
114 width: dotSize,
115 height: dotSize,
116 borderRadius: dotSize / 2,
117 backgroundColor: tone.dotColor,
118 }}
119 />
120 </View>
121 );
122}