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 { NativeStackScreenOptions } from "../../../native/StackHeader";
12import { AppText as Text, AppTextInput as TextInput } from "../../../components/ui/AppText";
13import { cn } from "../../../lib/cn";
14import { useEnvironmentQuery } from "../../../state/query";
15import { useThreadSelection } from "../../../state/use-thread-selection";
16import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions";
17import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state";
18import { useSelectedThreadWorktree } from "../../../state/use-selected-thread-worktree";
19import { vcsEnvironment } from "../../../state/vcs";
20
21type GitCommitSheetProps = StaticScreenProps<{
22 readonly environmentId: string;
23 readonly threadId: string;
24}>;
25
26export function GitCommitSheet(_props: GitCommitSheetProps) {
27 const navigation = useNavigation();
28 const insets = useSafeAreaInsets();
29 const { height: windowHeight } = useWindowDimensions();
30 const { selectedThread } = useThreadSelection();
31 const { selectedThreadCwd } = useSelectedThreadWorktree();
32 const gitState = useSelectedThreadGitState();
33 const gitActions = useSelectedThreadGitActions();
34 const { canWriteSourceControl, canChangeThreadBranch } = gitActions;
35
36 const gitStatus = useEnvironmentQuery(
37 selectedThread !== null && selectedThreadCwd !== null
38 ? vcsEnvironment.status({
39 environmentId: selectedThread.environmentId,
40 input: { cwd: selectedThreadCwd },
41 })
42 : null,
43 );
44
45 const busy = gitState.gitOperationLabel !== null;
46 const isDefaultRef = gitStatus.data?.isDefaultRef ?? false;
47 const allFiles = gitStatus.data?.workingTree?.files ?? [];
48
49 const [dialogCommitMessage, setDialogCommitMessage] = useState("");
50 const [excludedFiles, setExcludedFiles] = useState<ReadonlySet<string>>(new Set());
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
58 const runCommitAction = async (featureBranch: boolean) => {
59 if (!canWriteSourceControl || (featureBranch && !canChangeThreadBranch)) return;
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
70 const android = Platform.OS === "android";
71 const header = (
72 <SheetHeader
73 presentation={android ? "inline" : "native"}
74 title="Commit changes"
75 subtitle={gitStatus.data?.refName ?? "(detached HEAD)"}
76 onCancel={() => navigation.goBack()}
77 accessibilityLabel="Cancel commit"
78 />
79 );
80
81 return (
82 // Material sheet drags follow the first nested-scrolling native view, and the inline
83 // header's Compose button is one, so Android renders content first and reverses the column.
84 <View
85 collapsable={false}
86 className="bg-sheet ios:flex-1 android:flex-col-reverse"
87 style={android ? { maxHeight: windowHeight * 0.92 } : undefined}
88 >
89 {android ? (
90 <NativeStackScreenOptions
91 options={{ sheetCornerRadius: 28, sheetAllowedDetents: "fitToContents" }}
92 />
93 ) : null}
94 {android ? null : header}
95 <ScreenContent fitToContents>
96 <ScrollView
97 nestedScrollEnabled
98 contentInsetAdjustmentBehavior="automatic"
99 className="android:shrink android:grow-0 ios:flex-1"
100 keyboardShouldPersistTaps="handled"
101 keyboardDismissMode="on-drag"
102 automaticallyAdjustKeyboardInsets
103 contentContainerClassName="px-5 pt-4 pb-6 gap-5"
104 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
105 >
106 {isDefaultRef ? (
107 <Text className="text-sm text-warning-foreground">
108 You’re on the default branch. You can create a new branch when committing.
109 </Text>
110 ) : null}
111 <View className="gap-2">
112 <Text className="text-sm font-t3-medium text-foreground">Commit message</Text>
113 <TextInput
114 multiline
115 accessibilityLabel="Commit message"
116 value={dialogCommitMessage}
117 onChangeText={setDialogCommitMessage}
118 placeholder="Describe your changes…"
119 textAlignVertical="top"
120 className="min-h-24 rounded-xl bg-subtle px-4 py-3 text-base"
121 />
122 <Text className="text-xs text-foreground-muted">
123 Leave empty to generate a message automatically.
124 </Text>
125 </View>
126 <View>
127 <View className="flex-row items-center justify-between gap-2 pb-2">
128 <View className="flex-1 gap-1">
129 <Text className="text-base font-t3-bold text-foreground">Changed files</Text>
130 <Text className="text-xs text-foreground-muted">
131 {selectedFiles.length} of {allFiles.length} selected · +
132 {formatDiffCount(selectedInsertions)} −{formatDiffCount(selectedDeletions)}
133 </Text>
134 </View>
135 {allFiles.length > 0 ? (
136 <Button
137 label={allSelected ? "Deselect all" : "Select all"}
138 tone="text"
139 onPress={() =>
140 setExcludedFiles(
141 allSelected ? new Set(allFiles.map((file) => file.path)) : new Set(),
142 )
143 }
144 />
145 ) : null}
146 </View>
147 {allFiles.length === 0 ? (
148 <Text className="py-4 text-sm text-foreground-muted">
149 No changed files to commit.
150 </Text>
151 ) : (
152 allFiles.map((file) => {
153 const included = !excludedFiles.has(file.path);
154 const slash = file.path.lastIndexOf("/");
155 const name = file.path.slice(slash + 1);
156 const directory = slash >= 0 ? file.path.slice(0, slash) : null;
157 return (
158 <Pressable
159 key={file.path}
160 className="min-h-16 flex-row items-center gap-3 border-t border-border-subtle py-3"
161 accessibilityRole="checkbox"
162 accessibilityLabel={file.path}
163 accessibilityState={{ checked: included }}
164 onPress={() =>
165 setExcludedFiles((current) => {
166 const next = new Set(current);
167 if (next.has(file.path)) next.delete(file.path);
168 else next.add(file.path);
169 return next;
170 })
171 }
172 >
173 <View
174 className={cn(
175 "size-5 items-center justify-center rounded-full",
176 included ? "bg-primary" : "border border-input-border",
177 )}
178 >
179 {included ? (
180 <SymbolView
181 name="checkmark"
182 size={13}
183 tintColorClassName="accent-primary-foreground"
184 />
185 ) : null}
186 </View>
187 <View className="flex-1 gap-1">
188 <Text
189 numberOfLines={1}
190 ellipsizeMode="middle"
191 className="text-sm font-t3-medium text-foreground"
192 >
193 {name}
194 </Text>
195 {directory ? (
196 <Text
197 numberOfLines={1}
198 ellipsizeMode="middle"
199 className="text-xs text-foreground-muted"
200 >
201 {directory}
202 </Text>
203 ) : null}
204 </View>
205 <View className="flex-row gap-2">
206 <Text className="text-xs text-adaptive-emerald-700-300">
207 +{formatDiffCount(file.insertions)}
208 </Text>
209 <Text className="text-xs text-adaptive-rose-700-300">
210 −{formatDiffCount(file.deletions)}
211 </Text>
212 </View>
213 </Pressable>
214 );
215 })
216 )}
217 </View>
218 <View className="gap-2">
219 <Button
220 label="Commit changes"
221 icon="checkmark"
222 tone="primary"
223 fullWidth
224 disabled={!canWriteSourceControl || noneSelected || busy}
225 onPress={() => void runCommitAction(false)}
226 />
227 <Button
228 label="Commit on a new branch"
229 icon="arrow.branch"
230 tone="text"
231 fullWidth
232 disabled={!canWriteSourceControl || !canChangeThreadBranch || noneSelected || busy}
233 onPress={() => void runCommitAction(true)}
234 />
235 </View>
236 </ScrollView>
237 </ScreenContent>
238 {android ? header : null}
239 </View>
240 );
241}