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 { SheetHeader } from "../../../components/ui/SheetHeader";
13import { ScreenContent } from "../../../components/ui/ScreenContent";
14import { AppText as Text } from "../../../components/ui/AppText";
15import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions";
16import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state";
17import { useThreadSelection } from "../../../state/use-thread-selection";
18import { SheetActionButton } from "./gitSheetComponents";
19
20type GitConfirmSheetProps = StaticScreenProps<{
21 readonly environmentId: string;
22 readonly threadId: string;
23 readonly confirmAction?: string;
24 readonly branchName?: string;
25 readonly includesCommit?: string;
26 readonly commitMessage?: string;
27 readonly filePaths?: string;
28}>;
29
30export function GitConfirmSheet(props: GitConfirmSheetProps) {
31 const navigation = useNavigation();
32 const insets = useSafeAreaInsets();
33 const { height: windowHeight } = useWindowDimensions();
34 const gitState = useSelectedThreadGitState();
35 const gitActions = useSelectedThreadGitActions();
36 const { canWriteSourceControl, canChangeThreadBranch } = gitActions;
37 const { selectedThreadProject } = useThreadSelection();
38 const terminology = sourceControlClients.get(
39 selectedThreadProject?.repositoryIdentity?.provider,
40 ).changeRequest;
41
42 const params = props.route.params;
43
44 const confirmAction = params.confirmAction as
45 | "push"
46 | "create_pr"
47 | "commit_push"
48 | "commit_push_pr"
49 | undefined;
50 const branchName = params.branchName ?? "";
51 const includesCommit = params.includesCommit === "true";
52 const environmentId = params.environmentId ?? "";
53 const threadId = params.threadId ?? "";
54
55 const copy = useMemo(
56 () =>
57 confirmAction
58 ? resolveDefaultBranchActionDialogCopy({
59 action: confirmAction,
60 branchName,
61 includesCommit,
62 terminology,
63 })
64 : null,
65 [branchName, confirmAction, includesCommit, terminology],
66 );
67
68 const continuePendingAction = async () => {
69 if (!canWriteSourceControl || !confirmAction) return;
70 navigation.dispatch(StackActions.replace("Thread", { environmentId, threadId }));
71 await gitActions.onRunSelectedThreadGitAction({
72 action: confirmAction,
73 ...(params.commitMessage ? { commitMessage: params.commitMessage } : {}),
74 ...(params.filePaths ? { filePaths: params.filePaths.split(",") } : {}),
75 });
76 };
77
78 const movePendingActionToFeatureBranch = useCallback(async () => {
79 if (!canChangeThreadBranch || !confirmAction) return;
80 navigation.dispatch(StackActions.replace("Thread", { environmentId, threadId }));
81
82 if (includesCommit) {
83 await gitActions.onRunSelectedThreadGitAction({
84 action: confirmAction,
85 featureBranch: true,
86 ...(params.commitMessage ? { commitMessage: params.commitMessage } : {}),
87 ...(params.filePaths ? { filePaths: params.filePaths.split(",") } : {}),
88 });
89 return;
90 }
91
92 const branches =
93 gitState.selectedThreadBranches.length > 0
94 ? gitState.selectedThreadBranches
95 : await gitActions.refreshSelectedThreadBranches();
96 if (branches === null) return;
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 android:flex-col-reverse"
121 style={Platform.OS === "android" ? { maxHeight: windowHeight * 0.92 } : undefined}
122 >
123 <ScreenContent fitToContents>
124 <ScrollView
125 nestedScrollEnabled
126 className="android:shrink android:grow-0 ios:flex-1"
127 showsVerticalScrollIndicator={false}
128 contentContainerClassName="android:gap-2 android:p-2"
129 contentContainerStyle={
130 Platform.OS === "android"
131 ? { paddingBottom: Math.max(insets.bottom, 18) + 8 }
132 : undefined
133 }
134 >
135 <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">
136 {Platform.OS !== "android" ? (
137 <Text className="text-xs font-t3-bold tracking-[1px] uppercase text-foreground-muted">
138 Confirm
139 </Text>
140 ) : null}
141 <Text className="android:text-xl android:font-t3-medium ios:text-center ios:text-3xl ios:font-t3-bold">
142 {copy?.title ?? "Run action on default branch?"}
143 </Text>
144 <Text className="text-foreground-secondary leading-normal android:text-base ios:text-center ios:text-sm ios:font-medium">
145 {copy?.description ?? "Choose how to continue."}
146 </Text>
147 </View>
148
149 <View
150 className="android:gap-2 ios:gap-3 ios:px-5 ios:pt-2"
151 style={
152 Platform.OS === "android"
153 ? undefined
154 : { paddingBottom: Math.max(insets.bottom, 18) + 8 }
155 }
156 >
157 <SheetActionButton
158 icon="arrow.right.circle"
159 label={copy?.continueLabel ?? "Continue"}
160 disabled={!canWriteSourceControl}
161 onPress={() => void continuePendingAction()}
162 />
163 <SheetActionButton
164 icon="arrow.branch"
165 label="Feature branch & continue"
166 disabled={!canChangeThreadBranch}
167 tone="primary"
168 onPress={() => void movePendingActionToFeatureBranch()}
169 />
170 </View>
171 </ScrollView>
172 </ScreenContent>
173 <SheetHeader title="Confirm action" onClose={() => navigation.goBack()} />
174 </View>
175 );
176}