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

GitConfirmSheet.tsx

194 lines7.1 KB

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