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