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

GitActionProgressOverlay.tsx

123 lines3.8 KB

1import * as Haptics from "expo-haptics";
2import { SymbolView } from "../../components/AppSymbol";
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 className="absolute inset-x-3 z-[100]"
51 style={{ top: insets.top + 48 }}
52 pointerEvents="box-none"
53 >
54 <Pressable onPress={handlePress}>
55 <OverlayContent progress={progress} />
56 </Pressable>
57 </Animated.View>
58 );
60​
61function OverlayContent(props: { readonly progress: GitActionProgress }) {
62 const { progress } = props;
63 const iconColor = useThemeColor("--color-icon");
64​
65 const bgClass =
66 progress.phase === "error"
67 ? "bg-red-50 dark:bg-red-950/80 border-red-200 dark:border-red-800"
68 : "bg-card border-border";
69​
70 return (
71 <View
72 className={`flex-row items-center gap-2.5 rounded-2xl border px-3.5 py-3 shadow-lg shadow-black/10 ${bgClass}`}
73 >
74 <OverlayIcon phase={progress.phase} iconColor={iconColor} />
75​
76 <View className="flex-1 gap-0.5">
77 {progress.label ? (
78 <Text className="text-sm font-t3-bold text-foreground" numberOfLines={1}>
79 {progress.label}
80 </Text>
81 ) : null}
82 {progress.description ? (
83 <Text className="text-2xs text-foreground-muted" numberOfLines={1}>
84 {progress.description}
85 </Text>
86 ) : null}
87 </View>
88​
89 {progress.prUrl ? (
90 <SymbolView name="arrow.up.right" size={13} tintColor={iconColor} type="monochrome" />
91 ) : null}
92 </View>
93 );
95​
96function OverlayIcon(props: {
97 readonly phase: GitActionProgress["phase"];
98 readonly iconColor: ReturnType<typeof useThemeColor>;
99}) {
100 switch (props.phase) {
101 case "running":
102 return <ActivityIndicator size="small" />;
103 case "success":
104 return (
105 <View className="h-6 w-6 items-center justify-center rounded-full bg-green-500">
106 <SymbolView name="checkmark" size={12} tintColor="white" type="monochrome" />
107 </View>
108 );
109 case "error":
110 return (
111 <View className="h-6 w-6 items-center justify-center rounded-full bg-red-500">
112 <SymbolView
113 name="exclamationmark.triangle"
114 size={12}
115 tintColor="white"
116 type="monochrome"
117 />
118 </View>
119 );
120 default:
121 return null;
122 }