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

GitCommitSheet.tsx

228 lines9.3 KB

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