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 { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native";
7import { useCallback, useMemo } from "react";
8import { Platform, ScrollView, useWindowDimensions, View } from "react-native";
9
10import { useSafeAreaInsets } from "react-native-safe-area-context";
11
12import { AndroidSheetHeader } from "../../../components/ui/AndroidScreenHeader";
13import { ScreenContent } from "../../../components/ui/ScreenContent";
14import { NativeStackScreenOptions } from "../../../native/StackHeader";
15import { AppText as Text } from "../../../components/ui/AppText";
16import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions";
17import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state";
18import { useThreadSelection } from "../../../state/use-thread-selection";
19import { SheetActionButton } from "./gitSheetComponents";
20
21type GitConfirmSheetProps = StaticScreenProps<{
22 readonly environmentId: string;
23 readonly threadId: string;
24 readonly confirmAction?: string;
25 readonly branchName?: string;
26 readonly includesCommit?: string;
27 readonly commitMessage?: string;
28 readonly filePaths?: string;
29}>;
30
31export function GitConfirmSheet(props: GitConfirmSheetProps) {
32 const navigation = useNavigation();
33 const insets = useSafeAreaInsets();
34 const { height: windowHeight } = useWindowDimensions();
35 const gitState = useSelectedThreadGitState();
36 const gitActions = useSelectedThreadGitActions();
37 const { canWriteSourceControl, canChangeThreadBranch } = gitActions;
38 const { selectedThreadProject } = useThreadSelection();
39 const terminology = sourceControlClients.get(
40 selectedThreadProject?.repositoryIdentity?.provider,
41 ).changeRequest;
42
43 const params = props.route.params;
44
45 const confirmAction = params.confirmAction as
46 | "push"
47 | "create_pr"
48 | "commit_push"
49 | "commit_push_pr"
50 | undefined;
51 const branchName = params.branchName ?? "";
52 const includesCommit = params.includesCommit === "true";
53 const environmentId = params.environmentId ?? "";
54 const threadId = params.threadId ?? "";
55
56 const copy = useMemo(
57 () =>
58 confirmAction
59 ? resolveDefaultBranchActionDialogCopy({
60 action: confirmAction,
61 branchName,
62 includesCommit,
63 terminology,
64 })
65 : null,
66 [branchName, confirmAction, includesCommit, terminology],
67 );
68
69 const continuePendingAction = useCallback(async () => {
70 if (!canWriteSourceControl || !confirmAction) return;
71 navigation.dispatch(StackActions.replace("Thread", { environmentId, threadId }));
72 await gitActions.onRunSelectedThreadGitAction({
73 action: confirmAction,
74 ...(params.commitMessage ? { commitMessage: params.commitMessage } : {}),
75 ...(params.filePaths ? { filePaths: params.filePaths.split(",") } : {}),
76 });
77 }, [
78 canWriteSourceControl,
79 confirmAction,
80 environmentId,
81 gitActions,
82 params,
83 navigation,
84 threadId,
85 ]);
86
87 const movePendingActionToFeatureBranch = useCallback(async () => {
88 if (!canChangeThreadBranch || !confirmAction) return;
89 navigation.dispatch(StackActions.replace("Thread", { environmentId, threadId }));
90
91 if (includesCommit) {
92 await gitActions.onRunSelectedThreadGitAction({
93 action: confirmAction,
94 featureBranch: true,
95 ...(params.commitMessage ? { commitMessage: params.commitMessage } : {}),
96 ...(params.filePaths ? { filePaths: params.filePaths.split(",") } : {}),
97 });
98 return;
99 }
100
101 const branches =
102 gitState.selectedThreadBranches.length > 0
103 ? gitState.selectedThreadBranches
104 : await gitActions.refreshSelectedThreadBranches();
105 const newBranchName = resolveAutoFeatureBranchName(
106 Arr.filterMap(branches, (branch) =>
107 branch.isRemote ? Result.failVoid : Result.succeed(branch.name),
108 ),
109 );
110 const created = await gitActions.onCreateSelectedThreadBranch(newBranchName);
111 if (created === null) return;
112 await gitActions.onRunSelectedThreadGitAction({ action: confirmAction });
113 }, [
114 canChangeThreadBranch,
115 confirmAction,
116 gitActions,
117 gitState.selectedThreadBranches,
118 includesCommit,
119 params,
120 navigation,
121 environmentId,
122 threadId,
123 ]);
124
125 return (
126 <View
127 collapsable={false}
128 className="bg-sheet ios:flex-1"
129 style={Platform.OS === "android" ? { maxHeight: windowHeight * 0.92 } : undefined}
130 >
131 {Platform.OS === "android" ? (
132 <NativeStackScreenOptions
133 options={{
134 sheetCornerRadius: 28,
135 sheetAllowedDetents: "fitToContents",
136 }}
137 />
138 ) : null}
139 {Platform.OS === "android" ? (
140 <AndroidSheetHeader
141 title="Confirm action"
142 onBack={() => navigation.goBack()}
143 hideBottomBorder
144 />
145 ) : (
146 <View className="min-h-4 pt-2" />
147 )}
148
149 <ScreenContent fitToContents>
150 <ScrollView
151 nestedScrollEnabled
152 className="android:shrink android:grow-0 ios:flex-1"
153 showsVerticalScrollIndicator={false}
154 contentContainerClassName="android:gap-2 android:p-2"
155 contentContainerStyle={
156 Platform.OS === "android"
157 ? { paddingBottom: Math.max(insets.bottom, 18) + 8 }
158 : undefined
159 }
160 >
161 <View className="android:gap-2 android:rounded-[20px] android:bg-card android:p-3 ios:items-center ios:gap-1 ios:px-5 ios:pb-3 ios:pt-4">
162 {Platform.OS !== "android" ? (
163 <Text className="text-xs font-t3-bold tracking-[1px] uppercase text-foreground-muted">
164 Confirm
165 </Text>
166 ) : null}
167 <Text className="android:text-xl android:font-t3-medium ios:text-center ios:text-3xl ios:font-t3-bold">
168 {copy?.title ?? "Run action on default branch?"}
169 </Text>
170 <Text className="text-foreground-secondary leading-normal android:text-base ios:text-center ios:text-sm ios:font-medium">
171 {copy?.description ?? "Choose how to continue."}
172 </Text>
173 </View>
174
175 <View
176 className="android:gap-2 ios:gap-3 ios:px-5 ios:pt-2"
177 style={
178 Platform.OS === "android"
179 ? undefined
180 : { paddingBottom: Math.max(insets.bottom, 18) + 8 }
181 }
182 >
183 <SheetActionButton
184 icon="arrow.right.circle"
185 label={copy?.continueLabel ?? "Continue"}
186 disabled={!canWriteSourceControl}
187 onPress={() => void continuePendingAction()}
188 />
189 <SheetActionButton
190 icon="arrow.branch"
191 label="Feature branch & continue"
192 disabled={!canChangeThreadBranch}
193 tone="primary"
194 onPress={() => void movePendingActionToFeatureBranch()}
195 />
196 </View>
197 </ScrollView>
198 </ScreenContent>
199 </View>
200 );
201}