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