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

GitConfirmSheet.tsx

141 lines5.0 KB

1import { resolveDefaultBranchActionDialogCopy } from "@t3tools/client-runtime/state/vcs";
2import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
3import { resolveAutoFeatureBranchName } from "@t3tools/shared/git";
4import * as Arr from "effect/Array";
5import * as Result from "effect/Result";
6import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native";
7import { useCallback, useMemo } from "react";
8import { View } from "react-native";
9​
10import { SheetHeader } from "../../../components/ui/SheetHeader";
11import { FitSheet } from "../../../components/ui/FitSheet";
12import { SheetIntro } from "../../../components/ui/SheetIntro";
13import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions";
14import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state";
15import { useThreadSelection } from "../../../state/use-thread-selection";
16import { SheetActionButton } from "./gitSheetComponents";
17​
18type GitConfirmSheetProps = StaticScreenProps<{
19 readonly environmentId: string;
20 readonly threadId: string;
21 readonly confirmAction?: string;
22 readonly branchName?: string;
23 readonly includesCommit?: string;
24 readonly commitMessage?: string;
25 readonly filePaths?: string;
26}>;
27​
28export function GitConfirmSheet(props: GitConfirmSheetProps) {
29 const navigation = useNavigation();
30 const gitState = useSelectedThreadGitState();
31 const gitActions = useSelectedThreadGitActions();
32 const { canWriteSourceControl, canChangeThreadBranch } = gitActions;
33 const { selectedThreadProject } = useThreadSelection();
34 const terminology = sourceControlClients.get(
35 selectedThreadProject?.repositoryIdentity?.provider,
36 ).changeRequest;
37​
38 const params = props.route.params;
39​
40 const confirmAction = params.confirmAction as
41 | "push"
42 | "create_pr"
43 | "commit_push"
44 | "commit_push_pr"
45 | undefined;
46 const branchName = params.branchName ?? "";
47 const includesCommit = params.includesCommit === "true";
48 const environmentId = params.environmentId ?? "";
49 const threadId = params.threadId ?? "";
50​
51 const copy = useMemo(
52 () =>
53 confirmAction
54 ? resolveDefaultBranchActionDialogCopy({
55 action: confirmAction,
56 branchName,
57 includesCommit,
58 terminology,
59 })
60 : null,
61 [branchName, confirmAction, includesCommit, terminology],
62 );
63​
64 const continuePendingAction = async () => {
65 if (!canWriteSourceControl || !confirmAction) return;
66 navigation.dispatch(StackActions.replace("Thread", { environmentId, threadId }));
67 await gitActions.onRunSelectedThreadGitAction({
68 action: confirmAction,
69 ...(params.commitMessage ? { commitMessage: params.commitMessage } : {}),
70 ...(params.filePaths ? { filePaths: params.filePaths.split(",") } : {}),
71 });
72 };
73​
74 const movePendingActionToFeatureBranch = useCallback(async () => {
75 if (!canChangeThreadBranch || !confirmAction) return;
76 navigation.dispatch(StackActions.replace("Thread", { environmentId, threadId }));
77​
78 if (includesCommit) {
79 await gitActions.onRunSelectedThreadGitAction({
80 action: confirmAction,
81 featureBranch: true,
82 ...(params.commitMessage ? { commitMessage: params.commitMessage } : {}),
83 ...(params.filePaths ? { filePaths: params.filePaths.split(",") } : {}),
84 });
85 return;
86 }
87​
88 const branches =
89 gitState.selectedThreadBranches.length > 0
90 ? gitState.selectedThreadBranches
91 : await gitActions.refreshSelectedThreadBranches();
92 if (branches === null) return;
93 const newBranchName = resolveAutoFeatureBranchName(
94 Arr.filterMap(branches, (branch) =>
95 branch.isRemote ? Result.failVoid : Result.succeed(branch.name),
96 ),
97 );
98 const created = await gitActions.onCreateSelectedThreadBranch(newBranchName);
99 if (created === null) return;
100 await gitActions.onRunSelectedThreadGitAction({ action: confirmAction });
101 }, [
102 canChangeThreadBranch,
103 confirmAction,
104 gitActions,
105 gitState.selectedThreadBranches,
106 includesCommit,
107 params,
108 navigation,
109 environmentId,
110 threadId,
111 ]);
112​
113 return (
114 <FitSheet
115 header={<SheetHeader title="Confirm action" onClose={() => navigation.goBack()} />}
116 showsVerticalScrollIndicator={false}
117 >
118 <SheetIntro
119 eyebrow="Confirm"
120 title={copy?.title ?? "Run action on default branch?"}
121 description={copy?.description ?? "Choose how to continue."}
122 />
123​
124 <View className="gap-3">
125 <SheetActionButton
126 icon="arrow.right.circle"
127 label={copy?.continueLabel ?? "Continue"}
128 disabled={!canWriteSourceControl}
129 onPress={() => void continuePendingAction()}
130 />
131 <SheetActionButton
132 icon="arrow.branch"
133 label="Feature branch & continue"
134 disabled={!canChangeThreadBranch}
135 tone="primary"
136 onPress={() => void movePendingActionToFeatureBranch()}
137 />
138 </View>
139 </FitSheet>
140 );