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

GitCommitSheet.tsx

297 lines13.4 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​
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 const [isEditingFiles, setIsEditingFiles] = useState(false);
52​
53 const selectedFiles = allFiles.filter((file) => !excludedFiles.has(file.path));
54 const allSelected = excludedFiles.size === 0;
55 const noneSelected = selectedFiles.length === 0;
56 const selectedInsertions = selectedFiles.reduce((sum, file) => sum + file.insertions, 0);
57 const selectedDeletions = selectedFiles.reduce((sum, file) => sum + file.deletions, 0);
58 const selectedFilePreview = selectedFiles.slice(0, 3);
59​
60 const runCommitAction = async (featureBranch: boolean) => {
61 if (!canWriteSourceControl || (featureBranch && !canChangeThreadBranch)) return;
62 const commitMessage = dialogCommitMessage.trim();
63 navigation.goBack();
64 await gitActions.onRunSelectedThreadGitAction({
65 action: "commit",
66 featureBranch,
67 ...(commitMessage ? { commitMessage } : {}),
68 ...(!allSelected ? { filePaths: selectedFiles.map((file) => file.path) } : {}),
69 });
70 };
71​
72 return (
73 <View
74 collapsable={false}
75 className="bg-sheet ios:flex-1"
76 style={Platform.OS === "android" ? { maxHeight: windowHeight * 0.92 } : undefined}
77 >
78 {Platform.OS === "android" ? (
79 <NativeStackScreenOptions
80 options={{
81 sheetCornerRadius: 28,
82 sheetAllowedDetents: "fitToContents",
83 }}
84 />
85 ) : null}
86 {Platform.OS === "android" ? (
87 <AndroidSheetHeader
88 title="Commit changes"
89 onBack={() => navigation.goBack()}
90 hideBottomBorder
91 />
92 ) : null}
93 <ScreenContent fitToContents>
94 <ScrollView
95 nestedScrollEnabled
96 className="android:shrink android:grow-0 ios:flex-1"
97 keyboardShouldPersistTaps="handled"
98 showsVerticalScrollIndicator={false}
99 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
100 contentContainerClassName="android:gap-2 android:p-2 ios:gap-4 ios:px-5 ios:pt-2"
101 contentContainerStyle={
102 Platform.OS === "android"
103 ? { paddingBottom: Math.max(insets.bottom, 18) + 18 }
104 : undefined
105 }
106 >
107 <View className="gap-3 bg-card p-4 android:rounded-[20px] ios:rounded-[22px] ios:border ios:border-border">
108 <View className="android:gap-1 ios:flex-row ios:items-center ios:justify-between ios:gap-3">
109 <Text className="text-foreground-muted text-sm font-medium">Branch</Text>
110 <Text className="text-foreground text-base android:font-t3-medium ios:font-t3-bold">
111 {gitStatus.data?.refName ?? "(detached HEAD)"}
112 </Text>
113 </View>
114 {isDefaultRef ? (
115 <Text className="text-xs leading-normal text-warning-foreground">
116 Warning: this is the default branch.
117 </Text>
118 ) : null}
119 </View>
120​
121 <View className="gap-3 bg-card p-4 android:rounded-[20px] ios:rounded-[22px] ios:border ios:border-border">
122 <View className="flex-row items-center justify-between gap-3">
123 <View className="gap-1">
124 <Text className="text-foreground text-base android:font-t3-medium ios:font-t3-bold">
125 Files
126 </Text>
127 <Text className="text-foreground-muted text-xs leading-normal">
128 {selectedFiles.length} selected · +{formatDiffCount(selectedInsertions)} / -
129 {formatDiffCount(selectedDeletions)}
130 </Text>
131 </View>
132 <View className="flex-row items-center gap-2">
133 {!allSelected && isEditingFiles ? (
134 <Pressable
135 className="rounded-full px-3 android:min-h-12 android:justify-center android:active:bg-subtle ios:bg-subtle ios:py-2"
136 onPress={() => setExcludedFiles(new Set())}
137 >
138 <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">
139 Reset
140 </Text>
141 </Pressable>
142 ) : null}
143 <Pressable
144 className="rounded-full px-3 android:min-h-12 android:justify-center android:active:bg-subtle ios:bg-subtle ios:py-2"
145 onPress={() => setIsEditingFiles((current) => !current)}
146 >
147 <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">
148 {isEditingFiles ? "Done" : "Edit"}
149 </Text>
150 </Pressable>
151 </View>
152 </View>
153​
154 {allFiles.length === 0 ? (
155 <Text className="text-foreground-secondary text-sm leading-normal">
156 No changed files are available to commit.
157 </Text>
158 ) : !isEditingFiles ? (
159 <View className="gap-2">
160 {selectedFilePreview.map((file) => (
161 <View key={file.path} className="flex-row items-center justify-between gap-3">
162 <Text className="text-foreground flex-1 text-sm font-medium" numberOfLines={1}>
163 {file.path}
164 </Text>
165 <Text className="text-xs font-t3-bold text-adaptive-emerald-700-300">
166 +{formatDiffCount(file.insertions)}
167 </Text>
168 <Text className="text-xs font-t3-bold text-adaptive-rose-700-300">
169 -{formatDiffCount(file.deletions)}
170 </Text>
171 </View>
172 ))}
173 {selectedFiles.length > selectedFilePreview.length ? (
174 <Text className="text-foreground-muted text-xs leading-snug">
175 +{selectedFiles.length - selectedFilePreview.length} more files
176 </Text>
177 ) : null}
178 </View>
179 ) : (
180 <View className="gap-2">
181 {allFiles.map((file) => {
182 const included = !excludedFiles.has(file.path);
183 return (
184 <Pressable
185 key={file.path}
186 className={cn(
187 "px-4 py-3 android:rounded-xl ios:rounded-[18px] ios:border",
188 included
189 ? "android:bg-subtle ios:border-border"
190 : "ios:border-border-subtle",
191 )}
192 accessibilityRole="checkbox"
193 accessibilityLabel={file.path}
194 accessibilityState={{ checked: included }}
195 onPress={() => {
196 setExcludedFiles((current) => {
197 const next = new Set(current);
198 if (next.has(file.path)) {
199 next.delete(file.path);
200 } else {
201 next.add(file.path);
202 }
203 return next;
204 });
205 }}
206 >
207 {Platform.OS !== "android" ? (
208 <View
209 className={`absolute inset-0 rounded-[18px] ${included ? "bg-card" : "bg-subtle"}`}
210 />
211 ) : null}
212 <View className="flex-row items-start justify-between gap-3">
213 {Platform.OS === "android" ? (
214 <View
215 className={cn(
216 "mt-0.5 size-5 items-center justify-center rounded-sm",
217 included ? "bg-primary" : "border border-input-border",
218 )}
219 >
220 {included ? (
221 <SymbolView
222 name="checkmark"
223 size={16}
224 tintColorClassName="accent-primary-foreground"
225 type="monochrome"
226 />
227 ) : null}
228 </View>
229 ) : null}
230 <View className="flex-1 gap-1">
231 <Text
232 selectable
233 className={`text-sm font-t3-bold ${included ? "text-foreground" : "text-foreground-muted"}`}
234 >
235 {file.path}
236 </Text>
237 {!included ? (
238 <Text className="text-foreground-muted text-2xs leading-normal">
239 Excluded from this commit
240 </Text>
241 ) : null}
242 </View>
243 <View className="items-end gap-1">
244 <Text className="text-xs font-t3-bold text-adaptive-emerald-700-300">
245 +{formatDiffCount(file.insertions)}
246 </Text>
247 <Text className="text-xs font-t3-bold text-adaptive-rose-700-300">
248 -{formatDiffCount(file.deletions)}
249 </Text>
250 </View>
251 </View>
252 </Pressable>
253 );
254 })}
255 </View>
256 )}
257 </View>
258​
259 <View className="android:gap-3 android:rounded-[20px] android:bg-card android:p-4 ios:gap-2">
260 <Text className="text-foreground android:text-base android:font-t3-medium ios:text-sm ios:font-t3-bold">
261 Commit message
262 </Text>
263 <TextInput
264 multiline
265 accessibilityLabel="Commit message"
266 value={dialogCommitMessage}
267 onChangeText={setDialogCommitMessage}
268 placeholder="Leave empty to auto-generate"
269 textAlignVertical="top"
270 className="min-h-[128px] px-4 py-3.5 android:rounded-xl android:bg-sheet-solid ios:rounded-[20px]"
271 />
272 </View>
273​
274 <View className="android:gap-2 ios:flex-row ios:gap-3">
275 <View className="ios:flex-1">
276 <SheetActionButton
277 icon="arrow.branch"
278 label="Commit on new branch"
279 disabled={!canChangeThreadBranch || noneSelected || busy}
280 onPress={() => void runCommitAction(true)}
281 />
282 </View>
283 <View className="ios:flex-1">
284 <SheetActionButton
285 icon="checkmark.circle"
286 label="Commit"
287 tone="primary"
288 disabled={!canWriteSourceControl || noneSelected || busy}
289 onPress={() => void runCommitAction(false)}
290 />
291 </View>
292 </View>
293 </ScrollView>
294 </ScreenContent>
295 </View>
296 );