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

GitActionProgressOverlay.tsx

86 lines3.2 KB

1import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
2import { tryOpenExternalUrl } from "../../lib/openExternalUrl";
3import * as Haptics from "expo-haptics";
4import { HeaderHeightContext } from "@react-navigation/elements";
5import { useContext, useEffect, useRef } from "react";
6import { Platform } from "react-native";
7import Animated, { FadeIn, FadeOut, ReduceMotion } from "react-native-reanimated";
8import { useSafeAreaInsets } from "react-native-safe-area-context";
9import { Toast } from "../../components/ui/Toast";
10import { useToolbarLayout } from "../../components/ui/useToolbarLayout";
11import { MOBILE_MOTION } from "../../lib/designTokens";
12import { APP_BAR_HEIGHT } from "../../lib/layoutMetrics";
13import type { GitActionProgress } from "../../state/use-vcs-action-state";
14​
15export function GitActionProgressOverlay(props: {
16 readonly progress: GitActionProgress;
17 readonly onDismiss: () => void;
18 readonly onOpenPullRequest?: (url: string) => Promise<boolean>;
19}) {
20 const { progress, onDismiss } = props;
21 const openPullRequest = props.onOpenPullRequest;
22 const insets = useSafeAreaInsets();
23 // Keyboard-copy feedback sits outside the screen's native header context.
24 const headerHeight = useContext(HeaderHeightContext) ?? insets.top + APP_BAR_HEIGHT;
25 const toolbar = useToolbarLayout();
26 const top =
27 Platform.OS === "android"
28 ? toolbar.paddingTop + toolbar.height + toolbar.paddingBottom + 8
29 : headerHeight + 8;
30 const prevPhaseRef = useRef(progress.phase);
31​
32 useEffect(() => {
33 const prev = prevPhaseRef.current;
34 prevPhaseRef.current = progress.phase;
35​
36 if (prev === "running" && progress.phase === "success") {
37 Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
38 } else if (prev === "running" && progress.phase === "error") {
39 Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error);
40 }
41 }, [progress.phase]);
42​
43 const handlePress = () => {
44 if (progress.prUrl) {
45 void (openPullRequest
46 ? openPullRequest(progress.prUrl)
47 : tryOpenExternalUrl(progress.prUrl, "pull-request"));
48 return;
49 }
50 };
51​
52 if (progress.phase === "idle") {
53 return null;
54 }
55 const prUrl = progress.prUrl;
56 // A finished action reports only the URL it created.
57 const openLabel = prUrl
58 ? `Open ${(sourceControlClients.findByChangeRequestUrl(prUrl) ?? sourceControlClients.get(undefined)).changeRequest.shortLabel}`
59 : null;
60​
61 return (
62 <Animated.View
63 entering={FadeIn.duration(MOBILE_MOTION.enter).reduceMotion(ReduceMotion.System)}
64 exiting={FadeOut.duration(MOBILE_MOTION.exit).reduceMotion(ReduceMotion.System)}
65 className="absolute inset-x-3 z-[100]"
66 style={{ top }}
67 pointerEvents="box-none"
68 onAccessibilityEscape={onDismiss}
69 >
70 <Toast
71 singleLine
72 message={progress.label ?? "Source control"}
73 description={progress.description ?? undefined}
74 tone={
75 progress.phase === "error"
76 ? "danger"
77 : progress.phase === "success"
78 ? "success"
79 : "default"
80 }
81 busy={progress.phase === "running"}
82 action={openLabel ? { label: openLabel, onPress: handlePress } : undefined}
83 />
84 </Animated.View>
85 );