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

GitActionProgressOverlay.tsx

173 lines5.5 KB

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