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

GitCommitSheet.tsx

236 lines9.1 KB

1import { FitSheet } from "../../../components/ui/FitSheet";
2import { SheetHeader } from "../../../components/ui/SheetHeader";
3import { Button } from "../../../components/ui/Button";
4import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
5import { T, useGT, Var } from "gt-react-native";
6import { useState } from "react";
7import { Pressable, View } from "react-native";
8import { formatDiffCount } from "@t3tools/client-runtime/diff-count";
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 {
15 gitRepositoryTargetFromRoute,
16 type GitRepositoryRouteParams,
17} from "../../../state/thread-git-target";
18import { useThreadSelection } from "../../../state/use-thread-selection";
19import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions";
20import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state";
21import { useSelectedThreadWorktree } from "../../../state/use-selected-thread-worktree";
22import { vcsEnvironment } from "../../../state/vcs";
23​
24type GitCommitSheetProps = StaticScreenProps<
25 {
26 readonly environmentId: string;
27 readonly threadId: string;
28 } & GitRepositoryRouteParams
29>;
30​
31export function GitCommitSheet(props: GitCommitSheetProps) {
32 const repository = gitRepositoryTargetFromRoute(props.route.params);
33 const gt = useGT();
34 const navigation = useNavigation();
35 const { selectedThread } = useThreadSelection();
36 const threadWorktree = useSelectedThreadWorktree();
37 const selectedThreadCwd = repository?.cwd ?? threadWorktree.selectedThreadCwd;
38 const gitState = useSelectedThreadGitState(repository);
39 const gitActions = useSelectedThreadGitActions(repository);
40 const { canWriteSourceControl, canCreateFeatureBranch } = gitActions;
41​
42 const gitStatus = useEnvironmentQuery(
43 selectedThread !== null && selectedThreadCwd !== null
44 ? vcsEnvironment.status({
45 environmentId: selectedThread.environmentId,
46 input: { cwd: selectedThreadCwd },
47 })
48 : null,
49 );
50​
51 const busy = gitState.gitOperationLabel !== null;
52 const isDefaultRef = gitStatus.data?.isDefaultRef ?? false;
53 const allFiles = gitStatus.data?.workingTree?.files ?? [];
54​
55 const [dialogCommitMessage, setDialogCommitMessage] = useState("");
56 const [excludedFiles, setExcludedFiles] = useState<ReadonlySet<string>>(new Set());
57​
58 const selectedFiles = allFiles.filter((file) => !excludedFiles.has(file.path));
59 const allSelected = excludedFiles.size === 0;
60 const noneSelected = selectedFiles.length === 0;
61 const selectedInsertions = selectedFiles.reduce((sum, file) => sum + file.insertions, 0);
62 const selectedDeletions = selectedFiles.reduce((sum, file) => sum + file.deletions, 0);
63​
64 const runCommitAction = async (featureBranch: boolean) => {
65 if (!canWriteSourceControl || (featureBranch && !canCreateFeatureBranch)) return;
66 const commitMessage = dialogCommitMessage.trim();
67 navigation.goBack();
68 await gitActions.onRunSelectedThreadGitAction({
69 action: "commit",
70 featureBranch,
71 ...(commitMessage ? { commitMessage } : {}),
72 ...(!allSelected ? { filePaths: selectedFiles.map((file) => file.path) } : {}),
73 });
74 };
75​
76 return (
77 <FitSheet
78 header={
79 <SheetHeader
80 title={gt("Commit changes")}
81 subtitle={[repository?.name, gitStatus.data?.refName ?? gt("(detached HEAD)")]
82 .filter(Boolean)
83 .join(" · ")}
84 onCancel={() => navigation.goBack()}
85 accessibilityLabel={gt("Cancel commit")}
86 />
87 }
88 keyboardShouldPersistTaps="handled"
89 keyboardDismissMode="on-drag"
90 automaticallyAdjustKeyboardInsets
91 contentContainerClassName="px-5 pt-4 gap-5"
92 >
93 {isDefaultRef ? (
94 <Text className="text-sm text-warning-foreground">
95 <T>You’re on the default branch. You can create a new branch when committing.</T>
96 </Text>
97 ) : null}
98 <View className="gap-2">
99 <Text className="text-sm font-t3-medium text-foreground">
100 <T>Commit message</T>
101 </Text>
102 <TextInput
103 multiline
104 accessibilityLabel={gt("Commit message")}
105 value={dialogCommitMessage}
106 onChangeText={setDialogCommitMessage}
107 placeholder={gt("Describe your changes…")}
108 textAlignVertical="top"
109 className="min-h-24 rounded-xl bg-subtle px-4 py-3 text-base"
110 />
111 <Text className="text-xs text-foreground-muted">
112 <T>Leave empty to generate a message automatically.</T>
113 </Text>
114 </View>
115 <View>
116 <View className="flex-row items-center justify-between gap-2 pb-2">
117 <View className="flex-1 gap-1">
118 <Text className="text-base font-t3-bold text-foreground">
119 <T>Changed files</T>
120 </Text>
121 <Text className="text-xs text-foreground-muted">
122 <T>
123 <Var>{selectedFiles.length}</Var> of <Var>{allFiles.length}</Var> selected · +
124 <Var>{formatDiffCount(selectedInsertions)}</Var> −
125 <Var>{formatDiffCount(selectedDeletions)}</Var>
126 </T>
127 </Text>
128 </View>
129 {allFiles.length > 0 ? (
130 <Button
131 label={
132 allSelected
133 ? gt("Deselect all")
134 : gt("Select all", { $context: "selects every changed file to commit" })
135 }
136 tone="text"
137 onPress={() =>
138 setExcludedFiles(
139 allSelected ? new Set(allFiles.map((file) => file.path)) : new Set(),
140 )
141 }
142 />
143 ) : null}
144 </View>
145 {allFiles.length === 0 ? (
146 <Text className="py-4 text-sm text-foreground-muted">
147 <T>No changed files to commit.</T>
148 </Text>
149 ) : (
150 allFiles.map((file) => {
151 const included = !excludedFiles.has(file.path);
152 const slash = file.path.lastIndexOf("/");
153 const name = file.path.slice(slash + 1);
154 const directory = slash >= 0 ? file.path.slice(0, slash) : null;
155 return (
156 <Pressable
157 key={file.path}
158 className="min-h-16 flex-row items-center gap-3 border-t border-border-subtle py-3"
159 accessibilityRole="checkbox"
160 accessibilityLabel={file.path}
161 accessibilityState={{ checked: included }}
162 onPress={() =>
163 setExcludedFiles((current) => {
164 const next = new Set(current);
165 if (next.has(file.path)) next.delete(file.path);
166 else next.add(file.path);
167 return next;
168 })
169 }
170 >
171 <View
172 className={cn(
173 "size-5 items-center justify-center rounded-full",
174 included ? "bg-primary" : "border border-input-border",
175 )}
176 >
177 {included ? (
178 <SymbolView
179 name="checkmark"
180 size={13}
181 tintColorClassName="accent-primary-foreground"
182 />
183 ) : null}
184 </View>
185 <View className="flex-1 gap-1">
186 <Text
187 numberOfLines={1}
188 ellipsizeMode="middle"
189 className="text-sm font-t3-medium text-foreground"
190 >
191 {name}
192 </Text>
193 {directory ? (
194 <Text
195 numberOfLines={1}
196 ellipsizeMode="middle"
197 className="text-xs text-foreground-muted"
198 >
199 {directory}
200 </Text>
201 ) : null}
202 </View>
203 <View className="flex-row gap-2">
204 <Text className="text-xs text-adaptive-emerald-700-300">
205 +{formatDiffCount(file.insertions)}
206 </Text>
207 <Text className="text-xs text-adaptive-rose-700-300">
208 −{formatDiffCount(file.deletions)}
209 </Text>
210 </View>
211 </Pressable>
212 );
213 })
214 )}
215 </View>
216 <View className="gap-2">
217 <Button
218 label={gt("Commit changes")}
219 icon="checkmark"
220 tone="primary"
221 fullWidth
222 disabled={!canWriteSourceControl || noneSelected || busy}
223 onPress={() => void runCommitAction(false)}
224 />
225 <Button
226 label={gt("Commit on a new branch")}
227 icon="arrow.branch"
228 tone="text"
229 fullWidth
230 disabled={!canWriteSourceControl || !canCreateFeatureBranch || noneSelected || busy}
231 onPress={() => void runCommitAction(true)}
232 />
233 </View>
234 </FitSheet>
235 );