Open History to see the last commit
1import { useRouter } from "expo-router";
2import { useCallback, useState } from "react";
3import { Pressable, ScrollView, View, useColorScheme } from "react-native";
4import { useSafeAreaInsets } from "react-native-safe-area-context";
5import { useThemeColor } from "../../../lib/useThemeColor";
6
7import { AppText as Text, AppTextInput as TextInput } from "../../../components/AppText";
8import { useEnvironmentQuery } from "../../../state/query";
9import { useThreadSelection } from "../../../state/use-thread-selection";
10import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions";
11import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state";
12import { useSelectedThreadWorktree } from "../../../state/use-selected-thread-worktree";
13import { vcsEnvironment } from "../../../state/vcs";
14import { SheetActionButton } from "./gitSheetComponents";
15
16export function GitCommitSheet() {
17 const router = useRouter();
18 const insets = useSafeAreaInsets();
19 const isDarkMode = useColorScheme() === "dark";
20 const { selectedThread } = useThreadSelection();
21 const { selectedThreadCwd } = useSelectedThreadWorktree();
22 const gitState = useSelectedThreadGitState();
23 const gitActions = useSelectedThreadGitActions();
24
25 const borderColor = useThemeColor("--color-border");
26 const borderSubtleColor = useThemeColor("--color-border-subtle");
27 const inputBorderColor = useThemeColor("--color-input-border");
28 const inputBg = useThemeColor("--color-input");
29 const foregroundColor = useThemeColor("--color-foreground");
30
31 const gitStatus = useEnvironmentQuery(
32 selectedThread !== null && selectedThreadCwd !== null
33 ? vcsEnvironment.status({
34 environmentId: selectedThread.environmentId,
35 input: { cwd: selectedThreadCwd },
36 })
37 : null,
38 );
39
40 const busy = gitState.gitOperationLabel !== null;
41 const isDefaultRef = gitStatus.data?.isDefaultRef ?? false;
42 const allFiles = gitStatus.data?.workingTree?.files ?? [];
43
44 const [dialogCommitMessage, setDialogCommitMessage] = useState("");
45 const [excludedFiles, setExcludedFiles] = useState<ReadonlySet<string>>(new Set());
46 const [isEditingFiles, setIsEditingFiles] = useState(false);
47
48 const selectedFiles = allFiles.filter((file) => !excludedFiles.has(file.path));
49 const allSelected = excludedFiles.size === 0;
50 const noneSelected = selectedFiles.length === 0;
51 const selectedInsertions = selectedFiles.reduce((sum, file) => sum + file.insertions, 0);
52 const selectedDeletions = selectedFiles.reduce((sum, file) => sum + file.deletions, 0);
53 const selectedFilePreview = selectedFiles.slice(0, 3);
54
55 const runCommitAction = useCallback(
56 async (featureBranch: boolean) => {
57 const commitMessage = dialogCommitMessage.trim();
58 router.dismiss();
59 await gitActions.onRunSelectedThreadGitAction({
60 action: "commit",
61 featureBranch,
62 ...(commitMessage ? { commitMessage } : {}),
63 ...(!allSelected ? { filePaths: selectedFiles.map((file) => file.path) } : {}),
64 });
65 },
66 [allSelected, dialogCommitMessage, gitActions, router, selectedFiles],
67 );
68
69 return (
70 <ScrollView
71 keyboardShouldPersistTaps="handled"
72 showsVerticalScrollIndicator={false}
73 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
74 contentContainerStyle={{
75 paddingHorizontal: 20,
76 paddingTop: 8,
77 gap: 16,
78 }}
79 >
80 <View className="gap-3 rounded-[22px] border border-border bg-card px-4 py-4">
81 <View className="flex-row items-center justify-between gap-3">
82 <Text className="text-foreground-muted text-sm font-medium">Branch</Text>
83 <Text className="text-foreground text-base font-t3-bold">
84 {gitStatus.data?.refName ?? "(detached HEAD)"}
85 </Text>
86 </View>
87 {isDefaultRef ? (
88 <Text
89 className="text-xs leading-[18px]"
90 style={{ color: isDarkMode ? "#fbbf24" : "#b45309" }}
91 >
92 Warning: this is the default branch.
93 </Text>
94 ) : null}
95 </View>
96
97 <View className="gap-3 rounded-[22px] border border-border bg-card px-4 py-4">
98 <View className="flex-row items-center justify-between gap-3">
99 <View className="gap-1">
100 <Text className="text-foreground text-base font-t3-bold">Files</Text>
101 <Text className="text-foreground-muted text-xs leading-[18px]">
102 {selectedFiles.length} selected · +{selectedInsertions} / -{selectedDeletions}
103 </Text>
104 </View>
105 <View className="flex-row items-center gap-2">
106 {!allSelected && isEditingFiles ? (
107 <Pressable
108 className="bg-subtle rounded-full px-3 py-2"
109 onPress={() => setExcludedFiles(new Set())}
110 >
111 <Text className="text-foreground text-2xs font-t3-bold uppercase">Reset</Text>
112 </Pressable>
113 ) : null}
114 <Pressable
115 className="bg-subtle rounded-full px-3 py-2"
116 onPress={() => setIsEditingFiles((current) => !current)}
117 >
118 <Text className="text-foreground text-2xs font-t3-bold uppercase">
119 {isEditingFiles ? "Done" : "Edit"}
120 </Text>
121 </Pressable>
122 </View>
123 </View>
124
125 {allFiles.length === 0 ? (
126 <Text className="text-foreground-secondary text-sm leading-[19px]">
127 No changed files are available to commit.
128 </Text>
129 ) : !isEditingFiles ? (
130 <View className="gap-2">
131 {selectedFilePreview.map((file) => (
132 <View key={file.path} className="flex-row items-center justify-between gap-3">
133 <Text className="text-foreground flex-1 text-sm font-medium" numberOfLines={1}>
134 {file.path}
135 </Text>
136 <Text className="text-xs font-t3-bold" style={{ color: "#10b981" }}>
137 +{file.insertions}
138 </Text>
139 <Text className="text-xs font-t3-bold" style={{ color: "#f43f5e" }}>
140 -{file.deletions}
141 </Text>
142 </View>
143 ))}
144 {selectedFiles.length > selectedFilePreview.length ? (
145 <Text className="text-foreground-muted text-xs leading-[17px]">
146 +{selectedFiles.length - selectedFilePreview.length} more files
147 </Text>
148 ) : null}
149 </View>
150 ) : (
151 <View className="gap-2">
152 {allFiles.map((file) => {
153 const included = !excludedFiles.has(file.path);
154 return (
155 <Pressable
156 key={file.path}
157 className="rounded-[18px] border px-4 py-3"
158 style={{
159 borderColor: included ? borderColor : borderSubtleColor,
160 }}
161 onPress={() => {
162 setExcludedFiles((current) => {
163 const next = new Set(current);
164 if (next.has(file.path)) {
165 next.delete(file.path);
166 } else {
167 next.add(file.path);
168 }
169 return next;
170 });
171 }}
172 >
173 <View
174 className={`absolute inset-0 rounded-[18px] ${included ? "bg-card" : "bg-subtle"}`}
175 />
176 <View className="flex-row items-start justify-between gap-3">
177 <View className="flex-1 gap-1">
178 <Text
179 selectable
180 className={`text-sm font-t3-bold ${included ? "text-foreground" : "text-foreground-muted"}`}
181 >
182 {file.path}
183 </Text>
184 {!included ? (
185 <Text className="text-foreground-muted text-2xs leading-[16px]">
186 Excluded from this commit
187 </Text>
188 ) : null}
189 </View>
190 <View className="items-end gap-1">
191 <Text className="text-xs font-t3-bold" style={{ color: "#10b981" }}>
192 +{file.insertions}
193 </Text>
194 <Text className="text-xs font-t3-bold" style={{ color: "#f43f5e" }}>
195 -{file.deletions}
196 </Text>
197 </View>
198 </View>
199 </Pressable>
200 );
201 })}
202 </View>
203 )}
204 </View>
205
206 <View className="gap-2">
207 <Text className="text-foreground text-sm font-t3-bold">Commit message</Text>
208 <TextInput
209 multiline
210 value={dialogCommitMessage}
211 onChangeText={setDialogCommitMessage}
212 placeholder="Leave empty to auto-generate"
213 textAlignVertical="top"
214 className="min-h-[128px] rounded-[20px] px-4 py-3.5 font-sans text-base"
215 style={{
216 minHeight: 128,
217 borderWidth: 1,
218 borderColor: inputBorderColor,
219 backgroundColor: inputBg,
220 color: foregroundColor,
221 }}
222 />
223 </View>
224
225 <View className="flex-row gap-3">
226 <View className="flex-1">
227 <SheetActionButton
228 icon="arrow.branch"
229 label="Commit on new branch"
230 disabled={noneSelected || busy}
231 onPress={() => void runCommitAction(true)}
232 />
233 </View>
234 <View className="flex-1">
235 <SheetActionButton
236 icon="checkmark.circle"
237 label="Commit"
238 tone="primary"
239 disabled={noneSelected || busy}
240 onPress={() => void runCommitAction(false)}
241 />
242 </View>
243 </View>
244 </ScrollView>
245 );
246}