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