Open History to see the last commit
1import { SheetHeader } from "../../../components/ui/SheetHeader";
2import { Button } from "../../../components/ui/Button";
3import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
4import { useState } from "react";
5import { Platform, Pressable, ScrollView, useWindowDimensions, View } from "react-native";
6import { formatDiffCount } from "@t3tools/client-runtime/diff-count";
7import { useSafeAreaInsets } from "react-native-safe-area-context";
8
9import { SymbolView } from "../../../components/ui/AppSymbol";
10import { NativeStackScreenOptions } from "../../../native/StackHeader";
11import { AppText as Text, AppTextInput as TextInput } from "../../../components/ui/AppText";
12import { cn } from "../../../lib/cn";
13import { useEnvironmentQuery } from "../../../state/query";
14import { useThreadSelection } from "../../../state/use-thread-selection";
15import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions";
16import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state";
17import { useSelectedThreadWorktree } from "../../../state/use-selected-thread-worktree";
18import { vcsEnvironment } from "../../../state/vcs";
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
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
57 const runCommitAction = async (featureBranch: boolean) => {
58 if (!canWriteSourceControl || (featureBranch && !canChangeThreadBranch)) return;
59 const commitMessage = dialogCommitMessage.trim();
60 navigation.goBack();
61 await gitActions.onRunSelectedThreadGitAction({
62 action: "commit",
63 featureBranch,
64 ...(commitMessage ? { commitMessage } : {}),
65 ...(!allSelected ? { filePaths: selectedFiles.map((file) => file.path) } : {}),
66 });
67 };
68
69 return (
70 <View
71 collapsable={false}
72 className="bg-sheet ios:flex-1"
73 style={Platform.OS === "android" ? { maxHeight: windowHeight * 0.92 } : undefined}
74 >
75 {Platform.OS === "android" ? (
76 <NativeStackScreenOptions
77 options={{ sheetCornerRadius: 28, sheetAllowedDetents: "fitToContents" }}
78 />
79 ) : null}
80 <SheetHeader
81 presentation="inline"
82 title="Commit changes"
83 subtitle={gitStatus.data?.refName ?? "(detached HEAD)"}
84 onCancel={() => navigation.goBack()}
85 accessibilityLabel="Cancel commit"
86 />
87 <ScrollView
88 nestedScrollEnabled
89 className="android:shrink android:grow-0 ios:flex-1"
90 keyboardShouldPersistTaps="handled"
91 keyboardDismissMode="on-drag"
92 automaticallyAdjustKeyboardInsets
93 contentContainerClassName="px-5 pt-4 pb-6 gap-5"
94 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
95 >
96 {isDefaultRef ? (
97 <Text className="text-sm text-warning-foreground">
98 You’re on the default branch. You can create a new branch when committing.
99 </Text>
100 ) : null}
101 <View className="gap-2">
102 <Text className="text-sm font-t3-medium text-foreground">Commit message</Text>
103 <TextInput
104 multiline
105 accessibilityLabel="Commit message"
106 value={dialogCommitMessage}
107 onChangeText={setDialogCommitMessage}
108 placeholder="Describe your changes…"
109 textAlignVertical="top"
110 className="min-h-24 rounded-xl bg-subtle px-4 py-3 text-base"
111 />
112 <Text className="text-xs text-foreground-muted">
113 Leave empty to generate a message automatically.
114 </Text>
115 </View>
116 <View>
117 <View className="flex-row items-center justify-between gap-2 pb-2">
118 <View className="flex-1 gap-1">
119 <Text className="text-base font-t3-bold text-foreground">Changed files</Text>
120 <Text className="text-xs text-foreground-muted">
121 {selectedFiles.length} of {allFiles.length} selected · +
122 {formatDiffCount(selectedInsertions)} −{formatDiffCount(selectedDeletions)}
123 </Text>
124 </View>
125 {allFiles.length > 0 ? (
126 <Button
127 label={allSelected ? "Deselect all" : "Select all"}
128 tone="text"
129 onPress={() =>
130 setExcludedFiles(
131 allSelected ? new Set(allFiles.map((file) => file.path)) : new Set(),
132 )
133 }
134 />
135 ) : null}
136 </View>
137 {allFiles.length === 0 ? (
138 <Text className="py-4 text-sm text-foreground-muted">No changed files to commit.</Text>
139 ) : (
140 allFiles.map((file) => {
141 const included = !excludedFiles.has(file.path);
142 const slash = file.path.lastIndexOf("/");
143 const name = file.path.slice(slash + 1);
144 const directory = slash >= 0 ? file.path.slice(0, slash) : null;
145 return (
146 <Pressable
147 key={file.path}
148 className="min-h-16 flex-row items-center gap-3 border-t border-border-subtle py-3"
149 accessibilityRole="checkbox"
150 accessibilityLabel={file.path}
151 accessibilityState={{ checked: included }}
152 onPress={() =>
153 setExcludedFiles((current) => {
154 const next = new Set(current);
155 if (next.has(file.path)) next.delete(file.path);
156 else next.add(file.path);
157 return next;
158 })
159 }
160 >
161 <View
162 className={cn(
163 "size-5 items-center justify-center rounded-full",
164 included ? "bg-primary" : "border border-input-border",
165 )}
166 >
167 {included ? (
168 <SymbolView
169 name="checkmark"
170 size={13}
171 tintColorClassName="accent-primary-foreground"
172 />
173 ) : null}
174 </View>
175 <View className="flex-1 gap-1">
176 <Text
177 numberOfLines={1}
178 ellipsizeMode="middle"
179 className="text-sm font-t3-medium text-foreground"
180 >
181 {name}
182 </Text>
183 {directory ? (
184 <Text
185 numberOfLines={1}
186 ellipsizeMode="middle"
187 className="text-xs text-foreground-muted"
188 >
189 {directory}
190 </Text>
191 ) : null}
192 </View>
193 <View className="flex-row gap-2">
194 <Text className="text-xs text-adaptive-emerald-700-300">
195 +{formatDiffCount(file.insertions)}
196 </Text>
197 <Text className="text-xs text-adaptive-rose-700-300">
198 −{formatDiffCount(file.deletions)}
199 </Text>
200 </View>
201 </Pressable>
202 );
203 })
204 )}
205 </View>
206 <View className="gap-2">
207 <Button
208 label="Commit changes"
209 icon="checkmark"
210 tone="primary"
211 fullWidth
212 disabled={!canWriteSourceControl || noneSelected || busy}
213 onPress={() => void runCommitAction(false)}
214 />
215 <Button
216 label="Commit on a new branch"
217 icon="arrow.branch"
218 tone="text"
219 fullWidth
220 disabled={!canWriteSourceControl || !canChangeThreadBranch || noneSelected || busy}
221 onPress={() => void runCommitAction(true)}
222 />
223 </View>
224 </ScrollView>
225 </View>
226 );
227}