Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GitCommitSheet.tsx

311 lines14.0 KB

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