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 = 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
79 const movePendingActionToFeatureBranch = useCallback(async () => {
80 if (!canChangeThreadBranch || !confirmAction) return;
81 navigation.dispatch(StackActions.replace("Thread", { environmentId, threadId }));
82
83 if (includesCommit) {
84 await gitActions.onRunSelectedThreadGitAction({
85 action: confirmAction,
86 featureBranch: true,
87 ...(params.commitMessage ? { commitMessage: params.commitMessage } : {}),
88 ...(params.filePaths ? { filePaths: params.filePaths.split(",") } : {}),
89 });
90 return;
91 }
92
93 const branches =
94 gitState.selectedThreadBranches.length > 0
95 ? gitState.selectedThreadBranches
96 : await gitActions.refreshSelectedThreadBranches();
97 const newBranchName = resolveAutoFeatureBranchName(
98 Arr.filterMap(branches, (branch) =>
99 branch.isRemote ? Result.failVoid : Result.succeed(branch.name),
100 ),
101 );
102 const created = await gitActions.onCreateSelectedThreadBranch(newBranchName);
103 if (created === null) return;
104 await gitActions.onRunSelectedThreadGitAction({ action: confirmAction });
105 }, [
106 canChangeThreadBranch,
107 confirmAction,
108 gitActions,
109 gitState.selectedThreadBranches,
110 includesCommit,
111 params,
112 navigation,
113 environmentId,
114 threadId,
115 ]);
116
117 return (
118 <View
119 collapsable={false}
120 className="bg-sheet ios:flex-1"
121 style={Platform.OS === "android" ? { maxHeight: windowHeight * 0.92 } : undefined}
122 >
123 {Platform.OS === "android" ? (
124 <NativeStackScreenOptions
125 options={{
126 sheetCornerRadius: 28,
127 sheetAllowedDetents: "fitToContents",
128 }}
129 />
130 ) : null}
131 {Platform.OS === "android" ? (
132 <AndroidSheetHeader
133 title="Confirm action"
134 onBack={() => navigation.goBack()}
135 hideBottomBorder
136 />
137 ) : (
138 <View className="min-h-4 pt-2" />
139 )}
140
141 <ScreenContent fitToContents>
142 <ScrollView
143 nestedScrollEnabled
144 className="android:shrink android:grow-0 ios:flex-1"
145 showsVerticalScrollIndicator={false}
146 contentContainerClassName="android:gap-2 android:p-2"
147 contentContainerStyle={
148 Platform.OS === "android"
149 ? { paddingBottom: Math.max(insets.bottom, 18) + 8 }
150 : undefined
151 }
152 >
153 <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">
154 {Platform.OS !== "android" ? (
155 <Text className="text-xs font-t3-bold tracking-[1px] uppercase text-foreground-muted">
156 Confirm
157 </Text>
158 ) : null}
159 <Text className="android:text-xl android:font-t3-medium ios:text-center ios:text-3xl ios:font-t3-bold">
160 {copy?.title ?? "Run action on default branch?"}
161 </Text>
162 <Text className="text-foreground-secondary leading-normal android:text-base ios:text-center ios:text-sm ios:font-medium">
163 {copy?.description ?? "Choose how to continue."}
164 </Text>
165 </View>
166
167 <View
168 className="android:gap-2 ios:gap-3 ios:px-5 ios:pt-2"
169 style={
170 Platform.OS === "android"
171 ? undefined
172 : { paddingBottom: Math.max(insets.bottom, 18) + 8 }
173 }
174 >
175 <SheetActionButton
176 icon="arrow.right.circle"
177 label={copy?.continueLabel ?? "Continue"}
178 disabled={!canWriteSourceControl}
179 onPress={() => void continuePendingAction()}
180 />
181 <SheetActionButton
182 icon="arrow.branch"
183 label="Feature branch & continue"
184 disabled={!canChangeThreadBranch}
185 tone="primary"
186 onPress={() => void movePendingActionToFeatureBranch()}
187 />
188 </View>
189 </ScrollView>
190 </ScreenContent>
191 </View>
192 );
193}