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

GitConfirmSheet.tsx

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