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