Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GitConfirmSheet.tsx

198 lines6.9 KB

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={Platform.OS === "android" ? "bg-sheet" : "flex-1 bg-sheet"}
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={Platform.OS === "android" ? "shrink grow-0" : "flex-1"}
134 showsVerticalScrollIndicator={false}
135 contentContainerClassName={Platform.OS === "android" ? "gap-2 p-2" : undefined}
136 contentContainerStyle={
137 Platform.OS === "android"
138 ? { paddingBottom: Math.max(insets.bottom, 18) + 8 }
139 : undefined
140 }
141 >
142 <View
143 className={
144 Platform.OS === "android"
145 ? "gap-2 rounded-[20px] bg-card p-3"
146 : "items-center gap-1 px-5 pb-3 pt-4"
147 }
148 >
149 {Platform.OS !== "android" ? (
150 <Text className="text-xs font-t3-bold tracking-[1px] uppercase text-foreground-muted">
151 Confirm
152 </Text>
153 ) : null}
154 <Text
155 className={
156 Platform.OS === "android"
157 ? "text-xl font-t3-medium"
158 : "text-center text-3xl font-t3-bold"
159 }
160 >
161 {copy?.title ?? "Run action on default branch?"}
162 </Text>
163 <Text
164 className={
165 Platform.OS === "android"
166 ? "text-foreground-secondary text-base leading-normal"
167 : "text-center text-foreground-secondary text-sm font-medium leading-normal"
168 }
169 >
170 {copy?.description ?? "Choose how to continue."}
171 </Text>
172 </View>
173​
174 <View
175 className={Platform.OS === "android" ? "gap-2" : "gap-3 px-5 pt-2"}
176 style={
177 Platform.OS === "android"
178 ? undefined
179 : { paddingBottom: Math.max(insets.bottom, 18) + 8 }
180 }
181 >
182 <SheetActionButton
183 icon="arrow.right.circle"
184 label={copy?.continueLabel ?? "Continue"}
185 onPress={() => void continuePendingAction()}
186 />
187 <SheetActionButton
188 icon="arrow.branch"
189 label="Feature branch & continue"
190 tone="primary"
191 onPress={() => void movePendingActionToFeatureBranch()}
192 />
193 </View>
194 </ScrollView>
195 </MaterialScreenContent>
196 </View>
197 );