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

GitActionProgressOverlay.tsx

182 lines5.9 KB

1import * as Haptics from "expo-haptics";
2import { GlassView } from "expo-glass-effect";
3import { SymbolView } from "../../components/AppSymbol";
4import { useCallback, useEffect, useRef } from "react";
5import { ActivityIndicator, Pressable, StyleSheet, useColorScheme, View } from "react-native";
6import Animated, { FadeIn, FadeOut, LinearTransition } from "react-native-reanimated";
7import { useSafeAreaInsets } from "react-native-safe-area-context";
8​
9import { AppText as Text } from "../../components/AppText";
10import { APP_BAR_HEIGHT } from "../../lib/layoutMetrics";
11import { themeColorWithAlpha } from "../../lib/mobileTheme";
12import { tryOpenExternalUrl } from "../../lib/openExternalUrl";
13import { useUniwindTheme } from "../../lib/useUniwindTheme";
14import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
15import type { GitActionProgress } from "../../state/use-vcs-action-state";
16​
17const OVERLAY_LAYOUT_TRANSITION = LinearTransition.duration(220);
18const OVERLAY_TOP_GAP = 8;
19const AnimatedGlassView = Animated.createAnimatedComponent(GlassView);
20​
21export function GitActionProgressOverlay(props: {
22 readonly progress: GitActionProgress;
23 readonly onDismiss: () => void;
24}) {
25 const { progress, onDismiss } = props;
26 const insets = useSafeAreaInsets();
27 const prevPhaseRef = useRef(progress.phase);
28​
29 useEffect(() => {
30 const prev = prevPhaseRef.current;
31 prevPhaseRef.current = progress.phase;
32​
33 if (prev === "running" && progress.phase === "success") {
34 Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
35 } else if (prev === "running" && progress.phase === "error") {
36 Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error);
37 }
38 }, [progress.phase]);
39​
40 const handlePress = useCallback(() => {
41 if (progress.prUrl) {
42 void tryOpenExternalUrl(progress.prUrl, "pull-request");
43 return;
44 }
45 if (progress.phase === "success" || progress.phase === "error") {
46 onDismiss();
47 }
48 }, [onDismiss, progress.phase, progress.prUrl]);
49​
50 if (progress.phase === "idle") {
51 return null;
52 }
53​
54 return (
55 <Animated.View
56 entering={NATIVE_LIQUID_GLASS_SUPPORTED ? undefined : FadeIn.duration(200)}
57 exiting={FadeOut.duration(150)}
58 className="absolute inset-x-3 z-[100]"
59 style={{ top: insets.top + APP_BAR_HEIGHT + OVERLAY_TOP_GAP }}
60 pointerEvents="box-none"
61 >
62 <Pressable onPress={handlePress}>
63 <OverlayContent progress={progress} />
64 </Pressable>
65 </Animated.View>
66 );
68​
69function OverlayContent(props: { readonly progress: GitActionProgress }) {
70 const { progress } = props;
71 const colors = useUniwindTheme();
72 const glassSurface = colors["--color-glass-surface"];
73 const foreground = colors["--color-foreground"];
74 const shadowColor = colors["--color-primary-shadow"];
75 const isDarkMode = useColorScheme() === "dark";
76 const glassTint = themeColorWithAlpha(String(glassSurface), isDarkMode ? 0.48 : 0.38);
77 const glassBorder = themeColorWithAlpha(String(foreground), isDarkMode ? 0.18 : 0.12);
78 const content = (
79 <>
80 <OverlayIcon phase={progress.phase} />
81​
82 <View className="flex-1 gap-0.5">
83 {progress.label ? (
84 <Text className="text-sm font-t3-bold text-foreground" numberOfLines={1}>
85 {progress.label}
86 </Text>
87 ) : null}
88 {progress.description ? (
89 <Text className="text-2xs text-foreground-muted" numberOfLines={1}>
90 {progress.description}
91 </Text>
92 ) : null}
93 </View>
94​
95 {progress.prUrl ? (
96 <SymbolView
97 name="arrow.up.right"
98 size={13}
99 tintColorClassName={"accent-icon"}
100 type="monochrome"
101 />
102 ) : null}
103 </>
104 );
105​
106 if (NATIVE_LIQUID_GLASS_SUPPORTED) {
107 return (
108 <Animated.View
109 layout={OVERLAY_LAYOUT_TRANSITION}
110 style={{
111 borderCurve: "continuous",
112 borderRadius: 26,
113 elevation: 12,
114 shadowColor,
115 shadowOffset: { width: 0, height: 8 },
116 shadowOpacity: isDarkMode ? 0.42 : 0.2,
117 shadowRadius: 18,
118 }}
119 >
120 <AnimatedGlassView
121 colorScheme={isDarkMode ? "dark" : "light"}
122 glassEffectStyle="regular"
123 isInteractive
124 layout={OVERLAY_LAYOUT_TRANSITION}
125 tintColor={glassTint}
126 style={{
127 borderColor: glassBorder,
128 borderCurve: "continuous",
129 borderRadius: 26,
130 borderWidth: StyleSheet.hairlineWidth,
131 overflow: "hidden",
132 }}
133 >
134 <Animated.View
135 entering={FadeIn.delay(60).duration(140)}
136 className="flex-row items-center gap-2.5 px-3.5 py-3"
137 >
138 {content}
139 </Animated.View>
140 </AnimatedGlassView>
141 </Animated.View>
142 );
143 }
144​
145 const bgClass =
146 progress.phase === "error" ? "border-danger-border bg-danger" : "bg-card border-border";
147​
148 return (
149 <Animated.View
150 layout={OVERLAY_LAYOUT_TRANSITION}
151 className={`flex-row items-center gap-2.5 rounded-[26px] border border-continuous px-3.5 py-3 shadow-lg shadow-black/10 ${bgClass}`}
152 >
153 {content}
154 </Animated.View>
155 );
157​
158function OverlayIcon(props: { readonly phase: GitActionProgress["phase"] }) {
159 switch (props.phase) {
160 case "running":
161 return <ActivityIndicator size="small" colorClassName={"accent-icon"} />;
162 case "success":
163 return (
164 <View className="h-6 w-6 items-center justify-center rounded-full bg-green-500">
165 <SymbolView name="checkmark" size={12} tintColor="white" type="monochrome" />
166 </View>
167 );
168 case "error":
169 return (
170 <View className="h-6 w-6 items-center justify-center rounded-full bg-danger">
171 <SymbolView
172 name="exclamationmark.triangle"
173 size={12}
174 tintColorClassName="accent-danger-foreground"
175 type="monochrome"
176 />
177 </View>
178 );
179 default:
180 return null;
181 }