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

GitConfirmSheet.tsx

165 lines6.1 KB

1import {
2 resolveDefaultBranchActionDialogCopy,
3 type DefaultBranchConfirmableAction,
4} from "@t3tools/client-runtime/state/vcs";
5import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
6import { resolveAutoFeatureBranchName } from "@t3tools/shared/git";
7import * as Arr from "effect/Array";
8import * as Result from "effect/Result";
9import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
10import { useGT, useMessages } from "gt-react-native";
11import { useCallback, useMemo } from "react";
12import { View } from "react-native";
13​
14import { SheetHeader } from "../../../components/ui/SheetHeader";
15import { FitSheet } from "../../../components/ui/FitSheet";
16import { SheetIntro } from "../../../components/ui/SheetIntro";
17import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions";
18import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state";
19import { useSelectedThreadWorktree } from "../../../state/use-selected-thread-worktree";
20import { useThreadSelection } from "../../../state/use-thread-selection";
21import { useEnvironmentQuery } from "../../../state/query";
22import {
23 gitRepositoryTargetFromRoute,
24 type GitRepositoryRouteParams,
25} from "../../../state/thread-git-target";
26import { vcsEnvironment } from "../../../state/vcs";
27import { useReturnToThread } from "../thread-inspector-navigation";
28import { SheetActionButton } from "./gitSheetComponents";
29​
30type GitConfirmSheetProps = StaticScreenProps<
31 {
32 readonly environmentId: string;
33 readonly threadId: string;
34 readonly confirmAction?: string;
35 } & GitRepositoryRouteParams
36>;
37​
38const CONFIRMABLE_ACTIONS: ReadonlyArray<DefaultBranchConfirmableAction> = [
39 "push",
40 "create_pr",
41 "commit_push",
42 "commit_push_pr",
43];
44​
45export function GitConfirmSheet(props: GitConfirmSheetProps) {
46 const gt = useGT();
47 const messages = useMessages();
48 const navigation = useNavigation();
49 const returnToThread = useReturnToThread();
50 const repository = gitRepositoryTargetFromRoute(props.route.params);
51 const gitState = useSelectedThreadGitState(repository);
52 const gitActions = useSelectedThreadGitActions(repository);
53 const { canWriteSourceControl, canCreateFeatureBranch } = gitActions;
54 const { selectedThread, selectedThreadProject } = useThreadSelection();
55 const threadWorktree = useSelectedThreadWorktree();
56 const selectedThreadCwd = repository?.cwd ?? threadWorktree.selectedThreadCwd;
57 // The Git controls that opened this sheet already hold the checkout's status subscription.
58 const gitStatus = useEnvironmentQuery(
59 selectedThread !== null && selectedThreadCwd !== null
60 ? vcsEnvironment.status({
61 environmentId: selectedThread.environmentId,
62 input: { cwd: selectedThreadCwd },
63 })
64 : null,
65 );
66 const terminology = sourceControlClients.get(
67 repository === undefined
68 ? selectedThreadProject?.repositoryIdentity?.provider
69 : gitStatus.data?.sourceControlProvider?.kind,
70 ).changeRequest;
71​
72 const params = props.route.params;
73​
74 // The route only names the action; what the sheet says about it comes from the checkout.
75 const confirmAction = CONFIRMABLE_ACTIONS.find((action) => action === params.confirmAction);
76 const branchName = gitStatus.data?.refName ?? null;
77 const includesCommit = confirmAction === "commit_push" || confirmAction === "commit_push_pr";
78 const environmentId = params.environmentId ?? "";
79 const threadId = params.threadId ?? "";
80​
81 const copy = useMemo(
82 () =>
83 confirmAction && branchName
84 ? resolveDefaultBranchActionDialogCopy(
85 {
86 action: confirmAction,
87 branchName,
88 includesCommit,
89 terminology,
90 },
91 messages,
92 )
93 : null,
94 [branchName, confirmAction, includesCommit, messages, terminology],
95 );
96​
97 const continuePendingAction = async () => {
98 if (!canWriteSourceControl || !confirmAction || !copy) return;
99 returnToThread({ environmentId, threadId });
100 await gitActions.onRunSelectedThreadGitAction({ action: confirmAction });
101 };
102​
103 const movePendingActionToFeatureBranch = useCallback(async () => {
104 if (!canCreateFeatureBranch || !confirmAction || !copy) return;
105 returnToThread({ environmentId, threadId });
106​
107 if (includesCommit) {
108 await gitActions.onRunSelectedThreadGitAction({ action: confirmAction, featureBranch: true });
109 return;
110 }
111​
112 const branches =
113 gitState.selectedThreadBranches.length > 0
114 ? gitState.selectedThreadBranches
115 : await gitActions.refreshSelectedThreadBranches();
116 if (branches === null) return;
117 const newBranchName = resolveAutoFeatureBranchName(
118 Arr.filterMap(branches, (branch) =>
119 branch.isRemote ? Result.failVoid : Result.succeed(branch.name),
120 ),
121 );
122 const created = await gitActions.onCreateSelectedThreadBranch(newBranchName);
123 if (created === null) return;
124 await gitActions.onRunSelectedThreadGitAction({ action: confirmAction });
125 }, [
126 canCreateFeatureBranch,
127 confirmAction,
128 copy,
129 gitActions,
130 gitState.selectedThreadBranches,
131 includesCommit,
132 returnToThread,
133 environmentId,
134 threadId,
135 ]);
136​
137 return (
138 <FitSheet
139 header={<SheetHeader title={gt("Confirm action")} onClose={() => navigation.goBack()} />}
140 showsVerticalScrollIndicator={false}
141 >
142 <SheetIntro
143 eyebrow={gt("Confirm", { $context: "eyebrow label above a confirmation title" })}
144 title={copy?.title ?? gt("Run action on default branch?")}
145 description={copy?.description ?? gt("Choose how to continue.")}
146 />
147​
148 <View className="gap-3">
149 <SheetActionButton
150 icon="arrow.right.circle"
151 label={copy?.continueLabel ?? gt("Continue")}
152 disabled={!canWriteSourceControl || !copy}
153 onPress={() => void continuePendingAction()}
154 />
155 <SheetActionButton
156 icon="arrow.branch"
157 label={gt("Feature branch & continue")}
158 disabled={!canCreateFeatureBranch || !copy}
159 tone="primary"
160 onPress={() => void movePendingActionToFeatureBranch()}
161 />
162 </View>
163 </FitSheet>
164 );