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