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

ConnectionStatusDot.tsx

127 lines3.3 KB

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";
13import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
14import { themeColorWithAlpha, type MobileThemeVariables } from "../../lib/mobileTheme";
15​
16export type ConnectionStatusDotState = RemoteClientConnectionState;
17​
18function statusDotTone(
19 state: ConnectionStatusDotState,
20 theme: MobileThemeVariables,
21 dark: boolean,
22): {
23 readonly dotColor: string;
24 readonly haloColor: string;
25} {
26 switch (state) {
27 case "available":
28 case "unsupported":
29 return {
30 dotColor: theme["--color-icon-muted"],
31 haloColor: themeColorWithAlpha(theme["--color-icon-muted"], 0.42),
32 };
33 case "connected":
34 return {
35 dotColor: dark ? "#34d399" : "#059669",
36 haloColor: themeColorWithAlpha(dark ? "#34d399" : "#059669", 0.48),
37 };
38 case "connecting":
39 case "reconnecting":
40 return {
41 dotColor: theme["--color-warning-foreground"],
42 haloColor: themeColorWithAlpha(theme["--color-warning-foreground"], 0.5),
43 };
44 case "offline":
45 case "error":
46 return {
47 dotColor: theme["--color-danger-foreground"],
48 haloColor: themeColorWithAlpha(theme["--color-danger-foreground"], 0.48),
49 };
50 }
52​
53function usePulseAnimation(pulse: boolean) {
54 const pulseProgress = useSharedValue(0);
55​
56 useEffect(() => {
57 if (pulse) {
58 pulseProgress.value = withRepeat(
59 withTiming(1, {
60 duration: 1100,
61 easing: Easing.out(Easing.cubic),
62 }),
63 -1,
64 false,
65 );
66 return;
67 }
68​
69 cancelAnimation(pulseProgress);
70 pulseProgress.value = withTiming(0, {
71 duration: 180,
72 easing: Easing.out(Easing.quad),
73 });
74 }, [pulse, pulseProgress]);
75​
76 return pulseProgress;
78​
79export function ConnectionStatusDot(props: {
80 readonly state: ConnectionStatusDotState;
81 readonly pulse: boolean;
82 readonly size?: number;
83}) {
84 const pulseProgress = usePulseAnimation(props.pulse);
85 const { themeAppearance, themeVariables } = useAppearancePreferences();
86 const tone = statusDotTone(props.state, themeVariables, themeAppearance === "dark");
87 const dotSize = props.size ?? 10;
88 const haloSize = dotSize + 4;
89 const containerSize = haloSize + 4;
90​
91 const haloStyle = useAnimatedStyle(() => ({
92 opacity: props.pulse ? 0.14 + (1 - pulseProgress.value) * 0.3 : 0,
93 transform: [{ scale: 0.78 + pulseProgress.value * 1.16 }],
94 }));
95​
96 return (
97 <View
98 style={{
99 width: containerSize,
100 height: containerSize,
101 alignItems: "center",
102 justifyContent: "center",
103 }}
104 >
105 <Animated.View
106 style={[
107 haloStyle,
108 {
109 position: "absolute",
110 width: haloSize,
111 height: haloSize,
112 borderRadius: haloSize / 2,
113 backgroundColor: tone.haloColor,
114 },
115 ]}
116 />
117 <View
118 style={{
119 width: dotSize,
120 height: dotSize,
121 borderRadius: dotSize / 2,
122 backgroundColor: tone.dotColor,
123 }}
124 />
125 </View>
126 );