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

GitConfirmSheet.tsx

139 lines4.9 KB

1import { resolveDefaultBranchActionDialogCopy } from "@t3tools/client-runtime/state/vcs";
2import { resolveAutoFeatureBranchName } from "@t3tools/shared/git";
3import * as Arr from "effect/Array";
4import * as Result from "effect/Result";
5import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native";
6import { useCallback, useMemo } from "react";
7import { Platform, View } from "react-native";
8​
9import { useSafeAreaInsets } from "react-native-safe-area-context";
10​
11import { AndroidSheetHeader } from "../../../components/AndroidScreenHeader";
12import { AppText as Text } from "../../../components/AppText";
13import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions";
14import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state";
15import { SheetActionButton } from "./gitSheetComponents";
16​
17type GitConfirmSheetProps = StaticScreenProps<{
18 readonly environmentId: string;
19 readonly threadId: string;
20 readonly confirmAction?: string;
21 readonly branchName?: string;
22 readonly includesCommit?: string;
23 readonly commitMessage?: string;
24 readonly filePaths?: string;
25}>;
26​
27export function GitConfirmSheet(props: GitConfirmSheetProps) {
28 const navigation = useNavigation();
29 const insets = useSafeAreaInsets();
30 const gitState = useSelectedThreadGitState();
31 const gitActions = useSelectedThreadGitActions();
32​
33 const params = props.route.params;
34​
35 const confirmAction = params.confirmAction as
36 | "push"
37 | "create_pr"
38 | "commit_push"
39 | "commit_push_pr"
40 | undefined;
41 const branchName = params.branchName ?? "";
42 const includesCommit = params.includesCommit === "true";
43 const environmentId = params.environmentId ?? "";
44 const threadId = params.threadId ?? "";
45​
46 const copy = useMemo(
47 () =>
48 confirmAction
49 ? resolveDefaultBranchActionDialogCopy({
50 action: confirmAction,
51 branchName,
52 includesCommit,
53 })
54 : null,
55 [branchName, confirmAction, includesCommit],
56 );
57​
58 const continuePendingAction = useCallback(async () => {
59 if (!confirmAction) return;
60 navigation.dispatch(StackActions.replace("Thread", { environmentId, threadId }));
61 await gitActions.onRunSelectedThreadGitAction({
62 action: confirmAction,
63 ...(params.commitMessage ? { commitMessage: params.commitMessage } : {}),
64 ...(params.filePaths ? { filePaths: params.filePaths.split(",") } : {}),
65 });
66 }, [confirmAction, environmentId, gitActions, params, navigation, threadId]);
67​
68 const movePendingActionToFeatureBranch = useCallback(async () => {
69 if (!confirmAction) return;
70 navigation.dispatch(StackActions.replace("Thread", { environmentId, threadId }));
71​
72 if (includesCommit) {
73 await gitActions.onRunSelectedThreadGitAction({
74 action: confirmAction,
75 featureBranch: true,
76 ...(params.commitMessage ? { commitMessage: params.commitMessage } : {}),
77 ...(params.filePaths ? { filePaths: params.filePaths.split(",") } : {}),
78 });
79 return;
80 }
81​
82 const branches =
83 gitState.selectedThreadBranches.length > 0
84 ? gitState.selectedThreadBranches
85 : await gitActions.refreshSelectedThreadBranches();
86 const newBranchName = resolveAutoFeatureBranchName(
87 Arr.filterMap(branches, (branch) =>
88 branch.isRemote ? Result.failVoid : Result.succeed(branch.name),
89 ),
90 );
91 await gitActions.onCreateSelectedThreadBranch(newBranchName);
92 await gitActions.onRunSelectedThreadGitAction({ action: confirmAction });
93 }, [
94 confirmAction,
95 gitActions,
96 gitState.selectedThreadBranches,
97 includesCommit,
98 params,
99 navigation,
100 environmentId,
101 threadId,
102 ]);
103​
104 return (
105 <View collapsable={false} className="flex-1 bg-sheet">
106 {Platform.OS === "android" ? (
107 <AndroidSheetHeader title="Confirm action" onBack={() => navigation.goBack()} />
108 ) : (
109 <View className="min-h-4 pt-2" />
110 )}
111​
112 <View className="items-center gap-1 px-5 pb-3 pt-4">
113 <Text className="text-xs font-t3-bold tracking-[1px] uppercase text-foreground-muted">
114 Confirm
115 </Text>
116 <Text className="text-center text-3xl font-t3-bold">
117 {copy?.title ?? "Run action on default branch?"}
118 </Text>
119 <Text className="text-center text-foreground-secondary text-sm font-medium leading-normal">
120 {copy?.description ?? "Choose how to continue."}
121 </Text>
122 </View>
123​
124 <View className="gap-3 px-5 pt-2" style={{ paddingBottom: Math.max(insets.bottom, 18) + 8 }}>
125 <SheetActionButton
126 icon="arrow.right.circle"
127 label={copy?.continueLabel ?? "Continue"}
128 onPress={() => void continuePendingAction()}
129 />
130 <SheetActionButton
131 icon="arrow.branch"
132 label="Feature branch & continue"
133 tone="primary"
134 onPress={() => void movePendingActionToFeatureBranch()}
135 />
136 </View>
137 </View>
138 );