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

GitCommitSheet.tsx

233 lines9.8 KB

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 return (
71 <View
72 collapsable={false}
73 className="bg-sheet ios:flex-1"
74 style={Platform.OS === "android" ? { maxHeight: windowHeight * 0.92 } : undefined}
75 >
76 {Platform.OS === "android" ? (
77 <NativeStackScreenOptions
78 options={{ sheetCornerRadius: 28, sheetAllowedDetents: "fitToContents" }}
79 />
80 ) : null}
81 <SheetHeader
82 presentation={Platform.OS === "android" ? "inline" : "native"}
83 title="Commit changes"
84 subtitle={gitStatus.data?.refName ?? "(detached HEAD)"}
85 onCancel={() => navigation.goBack()}
86 accessibilityLabel="Cancel commit"
87 />
88 <ScreenContent fitToContents>
89 <ScrollView
90 nestedScrollEnabled
91 contentInsetAdjustmentBehavior="automatic"
92 className="android:shrink android:grow-0 ios:flex-1"
93 keyboardShouldPersistTaps="handled"
94 keyboardDismissMode="on-drag"
95 automaticallyAdjustKeyboardInsets
96 contentContainerClassName="px-5 pt-4 pb-6 gap-5"
97 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
98 >
99 {isDefaultRef ? (
100 <Text className="text-sm text-warning-foreground">
101 You’re on the default branch. You can create a new branch when committing.
102 </Text>
103 ) : null}
104 <View className="gap-2">
105 <Text className="text-sm font-t3-medium text-foreground">Commit message</Text>
106 <TextInput
107 multiline
108 accessibilityLabel="Commit message"
109 value={dialogCommitMessage}
110 onChangeText={setDialogCommitMessage}
111 placeholder="Describe your changes…"
112 textAlignVertical="top"
113 className="min-h-24 rounded-xl bg-subtle px-4 py-3 text-base"
114 />
115 <Text className="text-xs text-foreground-muted">
116 Leave empty to generate a message automatically.
117 </Text>
118 </View>
119 <View>
120 <View className="flex-row items-center justify-between gap-2 pb-2">
121 <View className="flex-1 gap-1">
122 <Text className="text-base font-t3-bold text-foreground">Changed files</Text>
123 <Text className="text-xs text-foreground-muted">
124 {selectedFiles.length} of {allFiles.length} selected · +
125 {formatDiffCount(selectedInsertions)} −{formatDiffCount(selectedDeletions)}
126 </Text>
127 </View>
128 {allFiles.length > 0 ? (
129 <Button
130 label={allSelected ? "Deselect all" : "Select all"}
131 tone="text"
132 onPress={() =>
133 setExcludedFiles(
134 allSelected ? new Set(allFiles.map((file) => file.path)) : new Set(),
135 )
136 }
137 />
138 ) : null}
139 </View>
140 {allFiles.length === 0 ? (
141 <Text className="py-4 text-sm text-foreground-muted">
142 No changed files to commit.
143 </Text>
144 ) : (
145 allFiles.map((file) => {
146 const included = !excludedFiles.has(file.path);
147 const slash = file.path.lastIndexOf("/");
148 const name = file.path.slice(slash + 1);
149 const directory = slash >= 0 ? file.path.slice(0, slash) : null;
150 return (
151 <Pressable
152 key={file.path}
153 className="min-h-16 flex-row items-center gap-3 border-t border-border-subtle py-3"
154 accessibilityRole="checkbox"
155 accessibilityLabel={file.path}
156 accessibilityState={{ checked: included }}
157 onPress={() =>
158 setExcludedFiles((current) => {
159 const next = new Set(current);
160 if (next.has(file.path)) next.delete(file.path);
161 else next.add(file.path);
162 return next;
163 })
164 }
165 >
166 <View
167 className={cn(
168 "size-5 items-center justify-center rounded-full",
169 included ? "bg-primary" : "border border-input-border",
170 )}
171 >
172 {included ? (
173 <SymbolView
174 name="checkmark"
175 size={13}
176 tintColorClassName="accent-primary-foreground"
177 />
178 ) : null}
179 </View>
180 <View className="flex-1 gap-1">
181 <Text
182 numberOfLines={1}
183 ellipsizeMode="middle"
184 className="text-sm font-t3-medium text-foreground"
185 >
186 {name}
187 </Text>
188 {directory ? (
189 <Text
190 numberOfLines={1}
191 ellipsizeMode="middle"
192 className="text-xs text-foreground-muted"
193 >
194 {directory}
195 </Text>
196 ) : null}
197 </View>
198 <View className="flex-row gap-2">
199 <Text className="text-xs text-adaptive-emerald-700-300">
200 +{formatDiffCount(file.insertions)}
201 </Text>
202 <Text className="text-xs text-adaptive-rose-700-300">
203 −{formatDiffCount(file.deletions)}
204 </Text>
205 </View>
206 </Pressable>
207 );
208 })
209 )}
210 </View>
211 <View className="gap-2">
212 <Button
213 label="Commit changes"
214 icon="checkmark"
215 tone="primary"
216 fullWidth
217 disabled={!canWriteSourceControl || noneSelected || busy}
218 onPress={() => void runCommitAction(false)}
219 />
220 <Button
221 label="Commit on a new branch"
222 icon="arrow.branch"
223 tone="text"
224 fullWidth
225 disabled={!canWriteSourceControl || !canChangeThreadBranch || noneSelected || busy}
226 onPress={() => void runCommitAction(true)}
227 />
228 </View>
229 </ScrollView>
230 </ScreenContent>
231 </View>
232 );