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 case "available":
22 return {
23 dotColor: "#9ca3af",
24 haloColor: "rgba(156,163,175,0.42)",
25 };
26 case "connected":
27 return {
28 dotColor: "#34d399",
29 haloColor: "rgba(52,211,153,0.48)",
30 };
31 case "connecting":
32 case "reconnecting":
33 return {
34 dotColor: "#f59e0b",
35 haloColor: "rgba(245,158,11,0.5)",
36 };
37 case "offline":
38 case "error":
39 return {
40 dotColor: "#ef4444",
41 haloColor: "rgba(239,68,68,0.48)",
42 };
43 }
44}
45
46function usePulseAnimation(pulse: boolean) {
47 const pulseProgress = useSharedValue(0);
48
49 useEffect(() => {
50 if (pulse) {
51 pulseProgress.value = withRepeat(
52 withTiming(1, {
53 duration: 1100,
54 easing: Easing.out(Easing.cubic),
55 }),
56 -1,
57 false,
58 );
59 return;
60 }
61
62 cancelAnimation(pulseProgress);
63 pulseProgress.value = withTiming(0, {
64 duration: 180,
65 easing: Easing.out(Easing.quad),
66 });
67 }, [pulse, pulseProgress]);
68
69 return pulseProgress;
70}
71
72export function ConnectionStatusDot(props: {
73 readonly state: ConnectionStatusDotState;
74 readonly pulse: boolean;
75 readonly size?: number;
76}) {
77 const pulseProgress = usePulseAnimation(props.pulse);
78 const tone = statusDotTone(props.state);
79 const dotSize = props.size ?? 10;
80 const haloSize = dotSize + 4;
81 const containerSize = haloSize + 4;
82
83 const haloStyle = useAnimatedStyle(() => ({
84 opacity: props.pulse ? 0.14 + (1 - pulseProgress.value) * 0.3 : 0,
85 transform: [{ scale: 0.78 + pulseProgress.value * 1.16 }],
86 }));
87
88 return (
89 <View
90 style={{
91 width: containerSize,
92 height: containerSize,
93 alignItems: "center",
94 justifyContent: "center",
95 }}
96 >
97 <Animated.View
98 style={[
99 haloStyle,
100 {
101 position: "absolute",
102 width: haloSize,
103 height: haloSize,
104 borderRadius: haloSize / 2,
105 backgroundColor: tone.haloColor,
106 },
107 ]}
108 />
109 <View
110 style={{
111 width: dotSize,
112 height: dotSize,
113 borderRadius: dotSize / 2,
114 backgroundColor: tone.dotColor,
115 }}
116 />
117 </View>
118 );
119}