Open History to see the last commit
1import { sanitizeFeatureBranchName } from "@t3tools/shared/git";
2import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
3import { useState } from "react";
4import { Platform, Pressable, ScrollView, useWindowDimensions, View } from "react-native";
5import { useSafeAreaInsets } from "react-native-safe-area-context";
6
7import { AndroidSheetHeader } from "../../../components/AndroidScreenHeader";
8import { MaterialScreenContent } from "../../../components/MaterialScreenContent";
9import { NativeStackScreenOptions } from "../../../native/StackHeader";
10import { AppText as Text, AppTextInput as TextInput } from "../../../components/AppText";
11import { cn } from "../../../lib/cn";
12import { useEnvironmentQuery } from "../../../state/query";
13import { useThreadSelection } from "../../../state/use-thread-selection";
14import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions";
15import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state";
16import { useSelectedThreadWorktree } from "../../../state/use-selected-thread-worktree";
17import { vcsEnvironment } from "../../../state/vcs";
18import { SheetActionButton } from "./gitSheetComponents";
19
20type GitBranchesSheetProps = StaticScreenProps<{
21 readonly environmentId: string;
22 readonly threadId: string;
23}>;
24
25export function GitBranchesSheet(_props: GitBranchesSheetProps) {
26 const navigation = useNavigation();
27 const insets = useSafeAreaInsets();
28 const { height: windowHeight } = useWindowDimensions();
29 const { selectedThread } = useThreadSelection();
30 const { selectedThreadCwd, selectedThreadWorktreePath } = useSelectedThreadWorktree();
31 const gitState = useSelectedThreadGitState();
32 const gitActions = useSelectedThreadGitActions();
33
34 const gitStatus = useEnvironmentQuery(
35 selectedThread !== null && selectedThreadCwd !== null
36 ? vcsEnvironment.status({
37 environmentId: selectedThread.environmentId,
38 input: { cwd: selectedThreadCwd },
39 })
40 : null,
41 );
42
43 const currentBranchLabel = gitStatus.data?.refName ?? selectedThread?.branch ?? "Detached HEAD";
44 const currentWorktreePath = selectedThreadWorktreePath;
45 const availableBranches = gitState.selectedThreadBranches;
46 const branchesLoading = gitState.selectedThreadBranchesLoading;
47 const busy = gitState.gitOperationLabel !== null;
48
49 const [newBranchName, setNewBranchName] = useState("");
50 const [worktreeBaseBranch, setWorktreeBaseBranch] = useState(
51 currentBranchLabel === "Detached HEAD" ? "main" : currentBranchLabel,
52 );
53 const [worktreeBranchName, setWorktreeBranchName] = useState("");
54
55 const disabledExistingBranchNames: Array<string> = [];
56 for (const branch of availableBranches) {
57 if (branch.worktreePath !== null && branch.worktreePath !== currentWorktreePath) {
58 disabledExistingBranchNames.push(branch.name);
59 }
60 }
61 const disabledExistingBranches = new Set(disabledExistingBranchNames);
62
63 return (
64 <View
65 collapsable={false}
66 className={Platform.OS === "android" ? "bg-sheet" : "flex-1 bg-sheet"}
67 style={Platform.OS === "android" ? { maxHeight: windowHeight * 0.92 } : undefined}
68 >
69 {Platform.OS === "android" ? (
70 <NativeStackScreenOptions
71 options={{
72 sheetCornerRadius: 28,
73 sheetAllowedDetents: "fitToContents",
74 }}
75 />
76 ) : null}
77 {Platform.OS === "android" ? (
78 <AndroidSheetHeader
79 title="Branches & worktrees"
80 onBack={() => navigation.goBack()}
81 hideBottomBorder
82 />
83 ) : null}
84 <MaterialScreenContent fitToContents>
85 <ScrollView
86 className={Platform.OS === "android" ? "shrink grow-0" : "flex-1"}
87 keyboardShouldPersistTaps="handled"
88 showsVerticalScrollIndicator={false}
89 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
90 contentContainerClassName={Platform.OS === "android" ? "gap-2 p-2" : "gap-4 px-5 pt-2"}
91 contentContainerStyle={
92 Platform.OS === "android"
93 ? { paddingBottom: Math.max(insets.bottom, 18) + 18 }
94 : undefined
95 }
96 >
97 <View
98 className={
99 Platform.OS === "android"
100 ? "gap-3 rounded-[20px] bg-card p-4"
101 : "gap-2 rounded-[18px] border border-border bg-card px-4 py-4"
102 }
103 >
104 <Text
105 className={
106 Platform.OS === "android"
107 ? "text-foreground text-base font-t3-medium"
108 : "text-foreground-secondary text-2xs font-t3-bold tracking-[1px] uppercase"
109 }
110 >
111 New branch
112 </Text>
113 <TextInput
114 value={newBranchName}
115 onChangeText={setNewBranchName}
116 placeholder="feature/mobile-polish"
117 accessibilityLabel="New branch name"
118 className={Platform.OS === "android" ? "rounded-xl bg-sheet-solid" : "rounded-[18px]"}
119 />
120 <SheetActionButton
121 icon="plus"
122 label="Create & checkout"
123 tone="primary"
124 disabled={busy || newBranchName.trim().length === 0}
125 onPress={() => {
126 const branch = sanitizeFeatureBranchName(newBranchName.trim());
127 if (branch.length === 0) return;
128 void gitActions.onCreateSelectedThreadBranch(branch).then(() => {
129 setNewBranchName("");
130 navigation.goBack();
131 });
132 }}
133 />
134 </View>
135
136 <View
137 className={
138 Platform.OS === "android"
139 ? "gap-3 rounded-[20px] bg-card p-4"
140 : "gap-2 rounded-[18px] border border-border bg-card px-4 py-4"
141 }
142 >
143 <Text
144 className={
145 Platform.OS === "android"
146 ? "text-foreground text-base font-t3-medium"
147 : "text-foreground-secondary text-2xs font-t3-bold tracking-[1px] uppercase"
148 }
149 >
150 New worktree
151 </Text>
152 {Platform.OS === "android" ? (
153 <Text className="text-foreground-secondary text-sm">Base branch</Text>
154 ) : null}
155 <TextInput
156 value={worktreeBaseBranch}
157 onChangeText={setWorktreeBaseBranch}
158 placeholder="main"
159 accessibilityLabel="Worktree base branch"
160 className={Platform.OS === "android" ? "rounded-xl bg-sheet-solid" : "rounded-[18px]"}
161 />
162 {Platform.OS === "android" ? (
163 <Text className="text-foreground-secondary text-sm">New branch</Text>
164 ) : null}
165 <TextInput
166 value={worktreeBranchName}
167 onChangeText={setWorktreeBranchName}
168 placeholder="feature/mobile-thread"
169 accessibilityLabel="Worktree branch name"
170 className={Platform.OS === "android" ? "rounded-xl bg-sheet-solid" : "rounded-[18px]"}
171 />
172 <SheetActionButton
173 icon="square.split.2x1"
174 label="Create worktree"
175 tone="primary"
176 disabled={
177 busy ||
178 worktreeBaseBranch.trim().length === 0 ||
179 worktreeBranchName.trim().length === 0
180 }
181 onPress={() => {
182 const baseBranch = worktreeBaseBranch.trim();
183 const newBranch = worktreeBranchName.trim();
184 if (baseBranch.length === 0 || newBranch.length === 0) return;
185 void gitActions
186 .onCreateSelectedThreadWorktree({ baseBranch, newBranch })
187 .then(() => {
188 setWorktreeBranchName("");
189 navigation.goBack();
190 });
191 }}
192 />
193 </View>
194
195 <View className="gap-2">
196 <Text
197 className={
198 Platform.OS === "android"
199 ? "px-4 pb-1 pt-3 text-foreground-secondary text-sm font-t3-medium"
200 : "text-foreground-secondary text-2xs font-t3-bold tracking-[1px] uppercase"
201 }
202 >
203 Existing branches
204 </Text>
205 {branchesLoading ? (
206 <Text
207 className={cn(
208 "text-foreground-secondary text-sm font-medium",
209 Platform.OS === "android" && "px-4",
210 )}
211 >
212 Loading branches...
213 </Text>
214 ) : null}
215 {!branchesLoading && availableBranches.length === 0 ? (
216 <Text
217 className={cn(
218 "text-foreground-secondary text-sm font-medium",
219 Platform.OS === "android" && "px-4",
220 )}
221 >
222 No local branches found.
223 </Text>
224 ) : null}
225 {availableBranches.map((branch) => {
226 const disabled = disabledExistingBranches.has(branch.name);
227 const subtitle = branch.worktreePath
228 ? branch.worktreePath === currentWorktreePath
229 ? "Checked out in this thread"
230 : "Checked out in another worktree"
231 : branch.isDefault
232 ? "Default branch"
233 : "Local branch";
234
235 return (
236 <Pressable
237 key={branch.name}
238 className={cn(
239 "gap-1 px-4 py-3 disabled:opacity-[0.45]",
240 Platform.OS === "android"
241 ? cn(
242 "rounded-[20px] active:bg-subtle",
243 branch.current ? "bg-secondary" : "bg-card",
244 )
245 : cn(
246 "rounded-[18px] border",
247 branch.current ? "border-subtle-strong" : "border-border",
248 ),
249 )}
250 accessibilityRole="button"
251 accessibilityState={{ selected: branch.current, disabled: busy || disabled }}
252 disabled={busy || disabled}
253 onPress={() => {
254 void gitActions.onCheckoutSelectedThreadBranch(branch.name).then(() => {
255 navigation.goBack();
256 });
257 }}
258 >
259 {Platform.OS !== "android" ? (
260 <View className="absolute inset-0 rounded-[18px] bg-card" />
261 ) : null}
262 <Text
263 className={cn(
264 "text-foreground text-base",
265 Platform.OS === "android" ? "font-t3-medium" : "font-t3-bold",
266 )}
267 >
268 {branch.name}
269 </Text>
270 <Text className="text-foreground-secondary text-xs font-medium">{subtitle}</Text>
271 </Pressable>
272 );
273 })}
274 </View>
275 </ScrollView>
276 </MaterialScreenContent>
277 </View>
278 );
279}