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