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="bg-sheet ios:flex-1"
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="android:shrink android:grow-0 ios:flex-1"
96 keyboardShouldPersistTaps="handled"
97 showsVerticalScrollIndicator={false}
98 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
99 contentContainerClassName="android:gap-2 android:p-2 ios:gap-4 ios:px-5 ios:pt-2"
100 contentContainerStyle={
101 Platform.OS === "android"
102 ? { paddingBottom: Math.max(insets.bottom, 18) + 18 }
103 : undefined
104 }
105 >
106 <View className="gap-3 bg-card p-4 android:rounded-[20px] ios:rounded-[22px] ios:border ios:border-border">
107 <View className="android:gap-1 ios:flex-row ios:items-center ios:justify-between ios:gap-3">
108 <Text className="text-foreground-muted text-sm font-medium">Branch</Text>
109 <Text className="text-foreground text-base android:font-t3-medium ios:font-t3-bold">
110 {gitStatus.data?.refName ?? "(detached HEAD)"}
111 </Text>
112 </View>
113 {isDefaultRef ? (
114 <Text className="text-xs leading-normal text-warning-foreground">
115 Warning: this is the default branch.
116 </Text>
117 ) : null}
118 </View>
119
120 <View className="gap-3 bg-card p-4 android:rounded-[20px] ios:rounded-[22px] ios:border ios:border-border">
121 <View className="flex-row items-center justify-between gap-3">
122 <View className="gap-1">
123 <Text className="text-foreground text-base android:font-t3-medium ios:font-t3-bold">
124 Files
125 </Text>
126 <Text className="text-foreground-muted text-xs leading-normal">
127 {selectedFiles.length} selected · +{selectedInsertions} / -{selectedDeletions}
128 </Text>
129 </View>
130 <View className="flex-row items-center gap-2">
131 {!allSelected && isEditingFiles ? (
132 <Pressable
133 className="rounded-full px-3 android:min-h-12 android:justify-center android:active:bg-subtle ios:bg-subtle ios:py-2"
134 onPress={() => setExcludedFiles(new Set())}
135 >
136 <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">
137 Reset
138 </Text>
139 </Pressable>
140 ) : null}
141 <Pressable
142 className="rounded-full px-3 android:min-h-12 android:justify-center android:active:bg-subtle ios:bg-subtle ios:py-2"
143 onPress={() => setIsEditingFiles((current) => !current)}
144 >
145 <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">
146 {isEditingFiles ? "Done" : "Edit"}
147 </Text>
148 </Pressable>
149 </View>
150 </View>
151
152 {allFiles.length === 0 ? (
153 <Text className="text-foreground-secondary text-sm leading-normal">
154 No changed files are available to commit.
155 </Text>
156 ) : !isEditingFiles ? (
157 <View className="gap-2">
158 {selectedFilePreview.map((file) => (
159 <View key={file.path} className="flex-row items-center justify-between gap-3">
160 <Text className="text-foreground flex-1 text-sm font-medium" numberOfLines={1}>
161 {file.path}
162 </Text>
163 <Text className="text-xs font-t3-bold text-adaptive-emerald-700-300">
164 +{file.insertions}
165 </Text>
166 <Text className="text-xs font-t3-bold text-adaptive-rose-700-300">
167 -{file.deletions}
168 </Text>
169 </View>
170 ))}
171 {selectedFiles.length > selectedFilePreview.length ? (
172 <Text className="text-foreground-muted text-xs leading-snug">
173 +{selectedFiles.length - selectedFilePreview.length} more files
174 </Text>
175 ) : null}
176 </View>
177 ) : (
178 <View className="gap-2">
179 {allFiles.map((file) => {
180 const included = !excludedFiles.has(file.path);
181 return (
182 <Pressable
183 key={file.path}
184 className={cn(
185 "px-4 py-3 android:rounded-xl ios:rounded-[18px] ios:border",
186 included
187 ? "android:bg-subtle ios:border-border"
188 : "ios:border-border-subtle",
189 )}
190 accessibilityRole="checkbox"
191 accessibilityLabel={file.path}
192 accessibilityState={{ checked: included }}
193 onPress={() => {
194 setExcludedFiles((current) => {
195 const next = new Set(current);
196 if (next.has(file.path)) {
197 next.delete(file.path);
198 } else {
199 next.add(file.path);
200 }
201 return next;
202 });
203 }}
204 >
205 {Platform.OS !== "android" ? (
206 <View
207 className={`absolute inset-0 rounded-[18px] ${included ? "bg-card" : "bg-subtle"}`}
208 />
209 ) : null}
210 <View className="flex-row items-start justify-between gap-3">
211 {Platform.OS === "android" ? (
212 <View
213 className={cn(
214 "mt-0.5 size-5 items-center justify-center rounded-sm",
215 included ? "bg-primary" : "border border-input-border",
216 )}
217 >
218 {included ? (
219 <SymbolView
220 name="checkmark"
221 size={16}
222 tintColorClassName="accent-primary-foreground"
223 type="monochrome"
224 />
225 ) : null}
226 </View>
227 ) : null}
228 <View className="flex-1 gap-1">
229 <Text
230 selectable
231 className={`text-sm font-t3-bold ${included ? "text-foreground" : "text-foreground-muted"}`}
232 >
233 {file.path}
234 </Text>
235 {!included ? (
236 <Text className="text-foreground-muted text-2xs leading-normal">
237 Excluded from this commit
238 </Text>
239 ) : null}
240 </View>
241 <View className="items-end gap-1">
242 <Text className="text-xs font-t3-bold text-adaptive-emerald-700-300">
243 +{file.insertions}
244 </Text>
245 <Text className="text-xs font-t3-bold text-adaptive-rose-700-300">
246 -{file.deletions}
247 </Text>
248 </View>
249 </View>
250 </Pressable>
251 );
252 })}
253 </View>
254 )}
255 </View>
256
257 <View className="android:gap-3 android:rounded-[20px] android:bg-card android:p-4 ios:gap-2">
258 <Text className="text-foreground android:text-base android:font-t3-medium ios:text-sm ios:font-t3-bold">
259 Commit message
260 </Text>
261 <TextInput
262 multiline
263 accessibilityLabel="Commit message"
264 value={dialogCommitMessage}
265 onChangeText={setDialogCommitMessage}
266 placeholder="Leave empty to auto-generate"
267 textAlignVertical="top"
268 className="min-h-[128px] px-4 py-3.5 android:rounded-xl android:bg-sheet-solid ios:rounded-[20px]"
269 />
270 </View>
271
272 <View className="android:gap-2 ios:flex-row ios:gap-3">
273 <View className="ios:flex-1">
274 <SheetActionButton
275 icon="arrow.branch"
276 label="Commit on new branch"
277 disabled={noneSelected || busy}
278 onPress={() => void runCommitAction(true)}
279 />
280 </View>
281 <View className="ios:flex-1">
282 <SheetActionButton
283 icon="checkmark.circle"
284 label="Commit"
285 tone="primary"
286 disabled={noneSelected || busy}
287 onPress={() => void runCommitAction(false)}
288 />
289 </View>
290 </View>
291 </ScrollView>
292 </MaterialScreenContent>
293 </View>
294 );
295}