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-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-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-emerald-500">
208 +{file.insertions}
209 </Text>
210 <Text className="text-xs font-t3-bold text-rose-500">-{file.deletions}</Text>
211 </View>
212 ))}
213 {selectedFiles.length > selectedFilePreview.length ? (
214 <Text className="text-foreground-muted text-xs leading-snug">
215 +{selectedFiles.length - selectedFilePreview.length} more files
216 </Text>
217 ) : null}
218 </View>
219 ) : (
220 <View className="gap-2">
221 {allFiles.map((file) => {
222 const included = !excludedFiles.has(file.path);
223 return (
224 <Pressable
225 key={file.path}
226 className={cn(
227 "px-4 py-3",
228 Platform.OS === "android"
229 ? included
230 ? "rounded-xl bg-subtle"
231 : "rounded-xl"
232 : cn(
233 "rounded-[18px] border",
234 included ? "border-border" : "border-border-subtle",
235 ),
236 )}
237 accessibilityRole="checkbox"
238 accessibilityLabel={file.path}
239 accessibilityState={{ checked: included }}
240 onPress={() => {
241 setExcludedFiles((current) => {
242 const next = new Set(current);
243 if (next.has(file.path)) {
244 next.delete(file.path);
245 } else {
246 next.add(file.path);
247 }
248 return next;
249 });
250 }}
251 >
252 {Platform.OS !== "android" ? (
253 <View
254 className={`absolute inset-0 rounded-[18px] ${included ? "bg-card" : "bg-subtle"}`}
255 />
256 ) : null}
257 <View className="flex-row items-start justify-between gap-3">
258 {Platform.OS === "android" ? (
259 <View
260 className={cn(
261 "mt-0.5 size-5 items-center justify-center rounded-sm",
262 included ? "bg-primary" : "border border-input-border",
263 )}
264 >
265 {included ? (
266 <SymbolView
267 name="checkmark"
268 size={16}
269 tintColorClassName="accent-primary-foreground"
270 type="monochrome"
271 />
272 ) : null}
273 </View>
274 ) : null}
275 <View className="flex-1 gap-1">
276 <Text
277 selectable
278 className={`text-sm font-t3-bold ${included ? "text-foreground" : "text-foreground-muted"}`}
279 >
280 {file.path}
281 </Text>
282 {!included ? (
283 <Text className="text-foreground-muted text-2xs leading-normal">
284 Excluded from this commit
285 </Text>
286 ) : null}
287 </View>
288 <View className="items-end gap-1">
289 <Text className="text-xs font-t3-bold text-emerald-500">
290 +{file.insertions}
291 </Text>
292 <Text className="text-xs font-t3-bold text-rose-500">
293 -{file.deletions}
294 </Text>
295 </View>
296 </View>
297 </Pressable>
298 );
299 })}
300 </View>
301 )}
302 </View>
303
304 <View
305 className={Platform.OS === "android" ? "gap-3 rounded-[20px] bg-card p-4" : "gap-2"}
306 >
307 <Text
308 className={
309 Platform.OS === "android"
310 ? "text-foreground text-base font-t3-medium"
311 : "text-foreground text-sm font-t3-bold"
312 }
313 >
314 Commit message
315 </Text>
316 <TextInput
317 multiline
318 accessibilityLabel="Commit message"
319 value={dialogCommitMessage}
320 onChangeText={setDialogCommitMessage}
321 placeholder="Leave empty to auto-generate"
322 textAlignVertical="top"
323 className={cn(
324 "min-h-[128px] px-4 py-3.5",
325 Platform.OS === "android" ? "rounded-xl bg-sheet-solid" : "rounded-[20px]",
326 )}
327 />
328 </View>
329
330 <View className={Platform.OS === "android" ? "gap-2" : "flex-row gap-3"}>
331 <View className={Platform.OS === "android" ? undefined : "flex-1"}>
332 <SheetActionButton
333 icon="arrow.branch"
334 label="Commit on new branch"
335 disabled={noneSelected || busy}
336 onPress={() => void runCommitAction(true)}
337 />
338 </View>
339 <View className={Platform.OS === "android" ? undefined : "flex-1"}>
340 <SheetActionButton
341 icon="checkmark.circle"
342 label="Commit"
343 tone="primary"
344 disabled={noneSelected || busy}
345 onPress={() => void runCommitAction(false)}
346 />
347 </View>
348 </View>
349 </ScrollView>
350 </MaterialScreenContent>
351 </View>
352 );
353}