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

GitCommitSheet.tsx

307 lines13.6 KB

1import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
2import { useCallback, useState } from "react";
3import { Platform, Pressable, ScrollView, useWindowDimensions, View } from "react-native";
4import { formatDiffCount } from "@t3tools/client-runtime/diff-count";
5import { useSafeAreaInsets } from "react-native-safe-area-context";
6​
7import { AndroidSheetHeader } from "../../../components/AndroidScreenHeader";
8import { SymbolView } from "../../../components/AppSymbol";
9import { MaterialScreenContent } from "../../../components/MaterialScreenContent";
10import { NativeStackScreenOptions } from "../../../native/StackHeader";
11import { AppText as Text, AppTextInput as TextInput } from "../../../components/AppText";
12import { cn } from "../../../lib/cn";
13import { useEnvironmentQuery } from "../../../state/query";
14import { useThreadSelection } from "../../../state/use-thread-selection";
15import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions";
16import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state";
17import { useSelectedThreadWorktree } from "../../../state/use-selected-thread-worktree";
18import { vcsEnvironment } from "../../../state/vcs";
19import { SheetActionButton } from "./gitSheetComponents";
20​
21type GitCommitSheetProps = StaticScreenProps<{
22 readonly environmentId: string;
23 readonly threadId: string;
24}>;
25​
26export function GitCommitSheet(_props: GitCommitSheetProps) {
27 const navigation = useNavigation();
28 const insets = useSafeAreaInsets();
29 const { height: windowHeight } = useWindowDimensions();
30 const { selectedThread } = useThreadSelection();
31 const { selectedThreadCwd } = useSelectedThreadWorktree();
32 const gitState = useSelectedThreadGitState();
33 const gitActions = useSelectedThreadGitActions();
34 const { canWriteSourceControl, canChangeThreadBranch } = gitActions;
35​
36 const gitStatus = useEnvironmentQuery(
37 selectedThread !== null && selectedThreadCwd !== null
38 ? vcsEnvironment.status({
39 environmentId: selectedThread.environmentId,
40 input: { cwd: selectedThreadCwd },
41 })
42 : null,
43 );
44​
45 const busy = gitState.gitOperationLabel !== null;
46 const isDefaultRef = gitStatus.data?.isDefaultRef ?? false;
47 const allFiles = gitStatus.data?.workingTree?.files ?? [];
48​
49 const [dialogCommitMessage, setDialogCommitMessage] = useState("");
50 const [excludedFiles, setExcludedFiles] = useState<ReadonlySet<string>>(new Set());
51 const [isEditingFiles, setIsEditingFiles] = useState(false);
52​
53 const selectedFiles = allFiles.filter((file) => !excludedFiles.has(file.path));
54 const allSelected = excludedFiles.size === 0;
55 const noneSelected = selectedFiles.length === 0;
56 const selectedInsertions = selectedFiles.reduce((sum, file) => sum + file.insertions, 0);
57 const selectedDeletions = selectedFiles.reduce((sum, file) => sum + file.deletions, 0);
58 const selectedFilePreview = selectedFiles.slice(0, 3);
59​
60 const runCommitAction = useCallback(
61 async (featureBranch: boolean) => {
62 if (!canWriteSourceControl || (featureBranch && !canChangeThreadBranch)) return;
63 const commitMessage = dialogCommitMessage.trim();
64 navigation.goBack();
65 await gitActions.onRunSelectedThreadGitAction({
66 action: "commit",
67 featureBranch,
68 ...(commitMessage ? { commitMessage } : {}),
69 ...(!allSelected ? { filePaths: selectedFiles.map((file) => file.path) } : {}),
70 });
71 },
72 [
73 allSelected,
74 canWriteSourceControl,
75 canChangeThreadBranch,
76 dialogCommitMessage,
77 gitActions,
78 navigation,
79 selectedFiles,
80 ],
81 );
82​
83 return (
84 <View
85 collapsable={false}
86 className="bg-sheet ios:flex-1"
87 style={Platform.OS === "android" ? { maxHeight: windowHeight * 0.92 } : undefined}
88 >
89 {Platform.OS === "android" ? (
90 <NativeStackScreenOptions
91 options={{
92 sheetCornerRadius: 28,
93 sheetAllowedDetents: "fitToContents",
94 }}
95 />
96 ) : null}
97 {Platform.OS === "android" ? (
98 <AndroidSheetHeader
99 title="Commit changes"
100 onBack={() => navigation.goBack()}
101 hideBottomBorder
102 />
103 ) : null}
104 <MaterialScreenContent fitToContents>
105 <ScrollView
106 className="android:shrink android:grow-0 ios:flex-1"
107 keyboardShouldPersistTaps="handled"
108 showsVerticalScrollIndicator={false}
109 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
110 contentContainerClassName="android:gap-2 android:p-2 ios:gap-4 ios:px-5 ios:pt-2"
111 contentContainerStyle={
112 Platform.OS === "android"
113 ? { paddingBottom: Math.max(insets.bottom, 18) + 18 }
114 : undefined
115 }
116 >
117 <View className="gap-3 bg-card p-4 android:rounded-[20px] ios:rounded-[22px] ios:border ios:border-border">
118 <View className="android:gap-1 ios:flex-row ios:items-center ios:justify-between ios:gap-3">
119 <Text className="text-foreground-muted text-sm font-medium">Branch</Text>
120 <Text className="text-foreground text-base android:font-t3-medium ios:font-t3-bold">
121 {gitStatus.data?.refName ?? "(detached HEAD)"}
122 </Text>
123 </View>
124 {isDefaultRef ? (
125 <Text className="text-xs leading-normal text-warning-foreground">
126 Warning: this is the default branch.
127 </Text>
128 ) : null}
129 </View>
130​
131 <View className="gap-3 bg-card p-4 android:rounded-[20px] ios:rounded-[22px] ios:border ios:border-border">
132 <View className="flex-row items-center justify-between gap-3">
133 <View className="gap-1">
134 <Text className="text-foreground text-base android:font-t3-medium ios:font-t3-bold">
135 Files
136 </Text>
137 <Text className="text-foreground-muted text-xs leading-normal">
138 {selectedFiles.length} selected · +{formatDiffCount(selectedInsertions)} / -
139 {formatDiffCount(selectedDeletions)}
140 </Text>
141 </View>
142 <View className="flex-row items-center gap-2">
143 {!allSelected && isEditingFiles ? (
144 <Pressable
145 className="rounded-full px-3 android:min-h-12 android:justify-center android:active:bg-subtle ios:bg-subtle ios:py-2"
146 onPress={() => setExcludedFiles(new Set())}
147 >
148 <Text className="android:text-primary-text android:text-sm android:font-t3-medium ios:text-foreground ios:text-2xs ios:font-t3-bold ios:uppercase">
149 Reset
150 </Text>
151 </Pressable>
152 ) : null}
153 <Pressable
154 className="rounded-full px-3 android:min-h-12 android:justify-center android:active:bg-subtle ios:bg-subtle ios:py-2"
155 onPress={() => setIsEditingFiles((current) => !current)}
156 >
157 <Text className="android:text-primary-text android:text-sm android:font-t3-medium ios:text-foreground ios:text-2xs ios:font-t3-bold ios:uppercase">
158 {isEditingFiles ? "Done" : "Edit"}
159 </Text>
160 </Pressable>
161 </View>
162 </View>
163​
164 {allFiles.length === 0 ? (
165 <Text className="text-foreground-secondary text-sm leading-normal">
166 No changed files are available to commit.
167 </Text>
168 ) : !isEditingFiles ? (
169 <View className="gap-2">
170 {selectedFilePreview.map((file) => (
171 <View key={file.path} className="flex-row items-center justify-between gap-3">
172 <Text className="text-foreground flex-1 text-sm font-medium" numberOfLines={1}>
173 {file.path}
174 </Text>
175 <Text className="text-xs font-t3-bold text-adaptive-emerald-700-300">
176 +{formatDiffCount(file.insertions)}
177 </Text>
178 <Text className="text-xs font-t3-bold text-adaptive-rose-700-300">
179 -{formatDiffCount(file.deletions)}
180 </Text>
181 </View>
182 ))}
183 {selectedFiles.length > selectedFilePreview.length ? (
184 <Text className="text-foreground-muted text-xs leading-snug">
185 +{selectedFiles.length - selectedFilePreview.length} more files
186 </Text>
187 ) : null}
188 </View>
189 ) : (
190 <View className="gap-2">
191 {allFiles.map((file) => {
192 const included = !excludedFiles.has(file.path);
193 return (
194 <Pressable
195 key={file.path}
196 className={cn(
197 "px-4 py-3 android:rounded-xl ios:rounded-[18px] ios:border",
198 included
199 ? "android:bg-subtle ios:border-border"
200 : "ios:border-border-subtle",
201 )}
202 accessibilityRole="checkbox"
203 accessibilityLabel={file.path}
204 accessibilityState={{ checked: included }}
205 onPress={() => {
206 setExcludedFiles((current) => {
207 const next = new Set(current);
208 if (next.has(file.path)) {
209 next.delete(file.path);
210 } else {
211 next.add(file.path);
212 }
213 return next;
214 });
215 }}
216 >
217 {Platform.OS !== "android" ? (
218 <View
219 className={`absolute inset-0 rounded-[18px] ${included ? "bg-card" : "bg-subtle"}`}
220 />
221 ) : null}
222 <View className="flex-row items-start justify-between gap-3">
223 {Platform.OS === "android" ? (
224 <View
225 className={cn(
226 "mt-0.5 size-5 items-center justify-center rounded-sm",
227 included ? "bg-primary" : "border border-input-border",
228 )}
229 >
230 {included ? (
231 <SymbolView
232 name="checkmark"
233 size={16}
234 tintColorClassName="accent-primary-foreground"
235 type="monochrome"
236 />
237 ) : null}
238 </View>
239 ) : null}
240 <View className="flex-1 gap-1">
241 <Text
242 selectable
243 className={`text-sm font-t3-bold ${included ? "text-foreground" : "text-foreground-muted"}`}
244 >
245 {file.path}
246 </Text>
247 {!included ? (
248 <Text className="text-foreground-muted text-2xs leading-normal">
249 Excluded from this commit
250 </Text>
251 ) : null}
252 </View>
253 <View className="items-end gap-1">
254 <Text className="text-xs font-t3-bold text-adaptive-emerald-700-300">
255 +{formatDiffCount(file.insertions)}
256 </Text>
257 <Text className="text-xs font-t3-bold text-adaptive-rose-700-300">
258 -{formatDiffCount(file.deletions)}
259 </Text>
260 </View>
261 </View>
262 </Pressable>
263 );
264 })}
265 </View>
266 )}
267 </View>
268​
269 <View className="android:gap-3 android:rounded-[20px] android:bg-card android:p-4 ios:gap-2">
270 <Text className="text-foreground android:text-base android:font-t3-medium ios:text-sm ios:font-t3-bold">
271 Commit message
272 </Text>
273 <TextInput
274 multiline
275 accessibilityLabel="Commit message"
276 value={dialogCommitMessage}
277 onChangeText={setDialogCommitMessage}
278 placeholder="Leave empty to auto-generate"
279 textAlignVertical="top"
280 className="min-h-[128px] px-4 py-3.5 android:rounded-xl android:bg-sheet-solid ios:rounded-[20px]"
281 />
282 </View>
283​
284 <View className="android:gap-2 ios:flex-row ios:gap-3">
285 <View className="ios:flex-1">
286 <SheetActionButton
287 icon="arrow.branch"
288 label="Commit on new branch"
289 disabled={!canChangeThreadBranch || noneSelected || busy}
290 onPress={() => void runCommitAction(true)}
291 />
292 </View>
293 <View className="ios:flex-1">
294 <SheetActionButton
295 icon="checkmark.circle"
296 label="Commit"
297 tone="primary"
298 disabled={!canWriteSourceControl || noneSelected || busy}
299 onPress={() => void runCommitAction(false)}
300 />
301 </View>
302 </View>
303 </ScrollView>
304 </MaterialScreenContent>
305 </View>
306 );