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

GitActionProgressOverlay.tsx

122 lines3.8 KB

1import * as Haptics from "expo-haptics";
2import { SymbolView } from "expo-symbols";
3import { useCallback, useEffect, useRef } from "react";
4import { ActivityIndicator, Pressable, View } from "react-native";
5import Animated, { FadeIn, FadeOut } from "react-native-reanimated";
6import { useSafeAreaInsets } from "react-native-safe-area-context";
7​
8import { AppText as Text } from "../../components/AppText";
9import { tryOpenExternalUrl } from "../../lib/openExternalUrl";
10import { useThemeColor } from "../../lib/useThemeColor";
11import type { GitActionProgress } from "../../state/use-vcs-action-state";
12​
13export function GitActionProgressOverlay(props: {
14 readonly progress: GitActionProgress;
15 readonly onDismiss: () => void;
16}) {
17 const { progress, onDismiss } = props;
18 const insets = useSafeAreaInsets();
19 const prevPhaseRef = useRef(progress.phase);
20​
21 useEffect(() => {
22 const prev = prevPhaseRef.current;
23 prevPhaseRef.current = progress.phase;
24​
25 if (prev === "running" && progress.phase === "success") {
26 Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
27 } else if (prev === "running" && progress.phase === "error") {
28 Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error);
29 }
30 }, [progress.phase]);
31​
32 const handlePress = useCallback(() => {
33 if (progress.prUrl) {
34 void tryOpenExternalUrl(progress.prUrl, "pull-request");
35 return;
36 }
37 if (progress.phase === "success" || progress.phase === "error") {
38 onDismiss();
39 }
40 }, [onDismiss, progress.phase, progress.prUrl]);
41​
42 if (progress.phase === "idle") {
43 return null;
44 }
45​
46 return (
47 <Animated.View
48 entering={FadeIn.duration(200)}
49 exiting={FadeOut.duration(150)}
50 style={{ top: insets.top + 48, left: 12, right: 12, position: "absolute", zIndex: 100 }}
51 pointerEvents="box-none"
52 >
53 <Pressable onPress={handlePress}>
54 <OverlayContent progress={progress} />
55 </Pressable>
56 </Animated.View>
57 );
59​
60function OverlayContent(props: { readonly progress: GitActionProgress }) {
61 const { progress } = props;
62 const iconColor = useThemeColor("--color-icon");
63​
64 const bgClass =
65 progress.phase === "error"
66 ? "bg-red-50 dark:bg-red-950/80 border-red-200 dark:border-red-800"
67 : "bg-card border-border";
68​
69 return (
70 <View
71 className={`flex-row items-center gap-2.5 rounded-2xl border px-3.5 py-3 shadow-lg shadow-black/10 ${bgClass}`}
72 >
73 <OverlayIcon phase={progress.phase} iconColor={iconColor} />
74​
75 <View className="flex-1 gap-0.5">
76 {progress.label ? (
77 <Text className="text-sm font-t3-bold text-foreground" numberOfLines={1}>
78 {progress.label}
79 </Text>
80 ) : null}
81 {progress.description ? (
82 <Text className="text-2xs text-foreground-muted" numberOfLines={1}>
83 {progress.description}
84 </Text>
85 ) : null}
86 </View>
87​
88 {progress.prUrl ? (
89 <SymbolView name="arrow.up.right" size={13} tintColor={iconColor} type="monochrome" />
90 ) : null}
91 </View>
92 );
94​
95function OverlayIcon(props: {
96 readonly phase: GitActionProgress["phase"];
97 readonly iconColor: ReturnType<typeof useThemeColor>;
98}) {
99 switch (props.phase) {
100 case "running":
101 return <ActivityIndicator size="small" />;
102 case "success":
103 return (
104 <View className="h-6 w-6 items-center justify-center rounded-full bg-green-500">
105 <SymbolView name="checkmark" size={12} tintColor="white" type="monochrome" />
106 </View>
107 );
108 case "error":
109 return (
110 <View className="h-6 w-6 items-center justify-center rounded-full bg-red-500">
111 <SymbolView
112 name="exclamationmark.triangle"
113 size={12}
114 tintColor="white"
115 type="monochrome"
116 />
117 </View>
118 );
119 default:
120 return null;
121 }