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

GitCommitSheet.tsx

233 lines9.8 KB

1import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
2import { useCallback, useState } from "react";
3import { Platform, Pressable, ScrollView, View } from "react-native";
4import { useSafeAreaInsets } from "react-native-safe-area-context";
5​
6import { AndroidSheetHeader } from "../../../components/AndroidScreenHeader";
7import { AppText as Text, AppTextInput as TextInput } from "../../../components/AppText";
8import { cn } from "../../../lib/cn";
9import { useEnvironmentQuery } from "../../../state/query";
10import { useThreadSelection } from "../../../state/use-thread-selection";
11import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions";
12import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state";
13import { useSelectedThreadWorktree } from "../../../state/use-selected-thread-worktree";
14import { vcsEnvironment } from "../../../state/vcs";
15import { SheetActionButton } from "./gitSheetComponents";
16​
17type GitCommitSheetProps = StaticScreenProps<{
18 readonly environmentId: string;
19 readonly threadId: string;
20}>;
21​
22export function GitCommitSheet(_props: GitCommitSheetProps) {
23 const navigation = useNavigation();
24 const insets = useSafeAreaInsets();
25 const { selectedThread } = useThreadSelection();
26 const { selectedThreadCwd } = useSelectedThreadWorktree();
27 const gitState = useSelectedThreadGitState();
28 const gitActions = useSelectedThreadGitActions();
29​
30 const gitStatus = useEnvironmentQuery(
31 selectedThread !== null && selectedThreadCwd !== null
32 ? vcsEnvironment.status({
33 environmentId: selectedThread.environmentId,
34 input: { cwd: selectedThreadCwd },
35 })
36 : null,
37 );
38​
39 const busy = gitState.gitOperationLabel !== null;
40 const isDefaultRef = gitStatus.data?.isDefaultRef ?? false;
41 const allFiles = gitStatus.data?.workingTree?.files ?? [];
42​
43 const [dialogCommitMessage, setDialogCommitMessage] = useState("");
44 const [excludedFiles, setExcludedFiles] = useState<ReadonlySet<string>>(new Set());
45 const [isEditingFiles, setIsEditingFiles] = useState(false);
46​
47 const selectedFiles = allFiles.filter((file) => !excludedFiles.has(file.path));
48 const allSelected = excludedFiles.size === 0;
49 const noneSelected = selectedFiles.length === 0;
50 const selectedInsertions = selectedFiles.reduce((sum, file) => sum + file.insertions, 0);
51 const selectedDeletions = selectedFiles.reduce((sum, file) => sum + file.deletions, 0);
52 const selectedFilePreview = selectedFiles.slice(0, 3);
53​
54 const runCommitAction = useCallback(
55 async (featureBranch: boolean) => {
56 const commitMessage = dialogCommitMessage.trim();
57 navigation.goBack();
58 await gitActions.onRunSelectedThreadGitAction({
59 action: "commit",
60 featureBranch,
61 ...(commitMessage ? { commitMessage } : {}),
62 ...(!allSelected ? { filePaths: selectedFiles.map((file) => file.path) } : {}),
63 });
64 },
65 [allSelected, dialogCommitMessage, gitActions, navigation, selectedFiles],
66 );
67​
68 return (
69 <View collapsable={false} className="flex-1 bg-sheet">
70 {Platform.OS === "android" ? (
71 <AndroidSheetHeader title="Commit changes" onBack={() => navigation.goBack()} />
72 ) : null}
73 <ScrollView
74 className="flex-1"
75 keyboardShouldPersistTaps="handled"
76 showsVerticalScrollIndicator={false}
77 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
78 contentContainerClassName="gap-4 px-5 pt-2"
79 >
80 <View className="gap-3 rounded-[22px] border border-border bg-card px-4 py-4">
81 <View className="flex-row items-center justify-between gap-3">
82 <Text className="text-foreground-muted text-sm font-medium">Branch</Text>
83 <Text className="text-foreground text-base font-t3-bold">
84 {gitStatus.data?.refName ?? "(detached HEAD)"}
85 </Text>
86 </View>
87 {isDefaultRef ? (
88 <Text className="text-xs leading-normal text-warning-foreground">
89 Warning: this is the default branch.
90 </Text>
91 ) : null}
92 </View>
93​
94 <View className="gap-3 rounded-[22px] border border-border bg-card px-4 py-4">
95 <View className="flex-row items-center justify-between gap-3">
96 <View className="gap-1">
97 <Text className="text-foreground text-base font-t3-bold">Files</Text>
98 <Text className="text-foreground-muted text-xs leading-normal">
99 {selectedFiles.length} selected · +{selectedInsertions} / -{selectedDeletions}
100 </Text>
101 </View>
102 <View className="flex-row items-center gap-2">
103 {!allSelected && isEditingFiles ? (
104 <Pressable
105 className="bg-subtle rounded-full px-3 py-2"
106 onPress={() => setExcludedFiles(new Set())}
107 >
108 <Text className="text-foreground text-2xs font-t3-bold uppercase">Reset</Text>
109 </Pressable>
110 ) : null}
111 <Pressable
112 className="bg-subtle rounded-full px-3 py-2"
113 onPress={() => setIsEditingFiles((current) => !current)}
114 >
115 <Text className="text-foreground text-2xs font-t3-bold uppercase">
116 {isEditingFiles ? "Done" : "Edit"}
117 </Text>
118 </Pressable>
119 </View>
120 </View>
121​
122 {allFiles.length === 0 ? (
123 <Text className="text-foreground-secondary text-sm leading-normal">
124 No changed files are available to commit.
125 </Text>
126 ) : !isEditingFiles ? (
127 <View className="gap-2">
128 {selectedFilePreview.map((file) => (
129 <View key={file.path} className="flex-row items-center justify-between gap-3">
130 <Text className="text-foreground flex-1 text-sm font-medium" numberOfLines={1}>
131 {file.path}
132 </Text>
133 <Text className="text-xs font-t3-bold text-emerald-500">+{file.insertions}</Text>
134 <Text className="text-xs font-t3-bold text-rose-500">-{file.deletions}</Text>
135 </View>
136 ))}
137 {selectedFiles.length > selectedFilePreview.length ? (
138 <Text className="text-foreground-muted text-xs leading-snug">
139 +{selectedFiles.length - selectedFilePreview.length} more files
140 </Text>
141 ) : null}
142 </View>
143 ) : (
144 <View className="gap-2">
145 {allFiles.map((file) => {
146 const included = !excludedFiles.has(file.path);
147 return (
148 <Pressable
149 key={file.path}
150 className={cn(
151 "rounded-[18px] border px-4 py-3",
152 included ? "border-border" : "border-border-subtle",
153 )}
154 onPress={() => {
155 setExcludedFiles((current) => {
156 const next = new Set(current);
157 if (next.has(file.path)) {
158 next.delete(file.path);
159 } else {
160 next.add(file.path);
161 }
162 return next;
163 });
164 }}
165 >
166 <View
167 className={`absolute inset-0 rounded-[18px] ${included ? "bg-card" : "bg-subtle"}`}
168 />
169 <View className="flex-row items-start justify-between gap-3">
170 <View className="flex-1 gap-1">
171 <Text
172 selectable
173 className={`text-sm font-t3-bold ${included ? "text-foreground" : "text-foreground-muted"}`}
174 >
175 {file.path}
176 </Text>
177 {!included ? (
178 <Text className="text-foreground-muted text-2xs leading-normal">
179 Excluded from this commit
180 </Text>
181 ) : null}
182 </View>
183 <View className="items-end gap-1">
184 <Text className="text-xs font-t3-bold text-emerald-500">
185 +{file.insertions}
186 </Text>
187 <Text className="text-xs font-t3-bold text-rose-500">
188 -{file.deletions}
189 </Text>
190 </View>
191 </View>
192 </Pressable>
193 );
194 })}
195 </View>
196 )}
197 </View>
198​
199 <View className="gap-2">
200 <Text className="text-foreground text-sm font-t3-bold">Commit message</Text>
201 <TextInput
202 multiline
203 value={dialogCommitMessage}
204 onChangeText={setDialogCommitMessage}
205 placeholder="Leave empty to auto-generate"
206 textAlignVertical="top"
207 className="min-h-[128px] rounded-[20px] px-4 py-3.5"
208 />
209 </View>
210​
211 <View className="flex-row gap-3">
212 <View className="flex-1">
213 <SheetActionButton
214 icon="arrow.branch"
215 label="Commit on new branch"
216 disabled={noneSelected || busy}
217 onPress={() => void runCommitAction(true)}
218 />
219 </View>
220 <View className="flex-1">
221 <SheetActionButton
222 icon="checkmark.circle"
223 label="Commit"
224 tone="primary"
225 disabled={noneSelected || busy}
226 onPress={() => void runCommitAction(false)}
227 />
228 </View>
229 </View>
230 </ScrollView>
231 </View>
232 );