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

GitActionProgressOverlay.tsx

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