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

GitBranchesSheet.tsx

244 lines10.6 KB

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 { AppBar } from "../../../components/ui/AppBar";
8import { SheetHeader } from "../../../components/ui/SheetHeader";
9import { ScreenContent } from "../../../components/ui/ScreenContent";
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";
19import { SheetActionButton } from "./gitSheetComponents";
20​
21type GitBranchesSheetProps = StaticScreenProps<{
22 readonly environmentId: string;
23 readonly threadId: string;
24}>;
25​
26export function GitBranchesSheet(_props: GitBranchesSheetProps) {
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 { 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 currentBranchLabel = gitStatus.data?.refName ?? selectedThread?.branch ?? "Detached HEAD";
46 const currentWorktreePath = selectedThreadCwd;
47 const availableBranches = gitState.selectedThreadBranches;
48 const branchesLoading = gitState.selectedThreadBranchesLoading;
49 const busy = gitState.gitOperationLabel !== null;
50​
51 const [newBranchName, setNewBranchName] = useState("");
52 const [worktreeBaseBranchOverride, setWorktreeBaseBranchOverride] = useState<string | null>(null);
53 const worktreeBaseBranch =
54 worktreeBaseBranchOverride ??
55 (currentBranchLabel === "Detached HEAD" ? "main" : currentBranchLabel);
56 const [worktreeBranchName, setWorktreeBranchName] = useState("");
57​
58 const disabledExistingBranchNames: Array<string> = [];
59 for (const branch of availableBranches) {
60 if (branch.worktreePath !== null && branch.worktreePath !== currentWorktreePath) {
61 disabledExistingBranchNames.push(branch.name);
62 }
63 }
64 const disabledExistingBranches = new Set(disabledExistingBranchNames);
65​
66 return (
67 <View
68 collapsable={false}
69 className="bg-sheet ios:flex-1"
70 style={Platform.OS === "android" ? { maxHeight: windowHeight * 0.92 } : undefined}
71 >
72 {Platform.OS === "android" ? (
73 <NativeStackScreenOptions
74 options={{
75 sheetCornerRadius: 28,
76 sheetAllowedDetents: "fitToContents",
77 }}
78 />
79 ) : null}
80 {Platform.OS === "android" ? (
81 <AppBar
82 sheet
83 title="Branches & worktrees"
84 onBack={() => navigation.goBack()}
85 hideBottomBorder
86 />
87 ) : (
88 <SheetHeader
89 presentation="native"
90 title="Branches & worktrees"
91 onClose={() => navigation.goBack()}
92 />
93 )}
94 <ScreenContent fitToContents>
95 <ScrollView
96 nestedScrollEnabled
97 className="android:shrink android:grow-0 ios:flex-1"
98 contentInsetAdjustmentBehavior={Platform.OS === "ios" ? "automatic" : "never"}
99 keyboardShouldPersistTaps="handled"
100 showsVerticalScrollIndicator={false}
101 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
102 contentContainerClassName="android:gap-2 android:p-2 ios:gap-4 ios:px-5 ios:pt-2"
103 contentContainerStyle={
104 Platform.OS === "android"
105 ? { paddingBottom: Math.max(insets.bottom, 18) + 18 }
106 : undefined
107 }
108 >
109 <View className="bg-card android:gap-3 android:rounded-[20px] android:p-4 ios:gap-2 ios:rounded-[18px] ios:border ios:border-border ios:px-4 ios:py-4">
110 <Text className="android:text-foreground android:text-base android:font-t3-medium ios:text-foreground-secondary ios:text-2xs ios:font-t3-bold ios:tracking-[1px] ios:uppercase">
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="android:rounded-xl android:bg-sheet-solid ios:rounded-[18px]"
119 />
120 <SheetActionButton
121 icon="plus"
122 label="Create & checkout"
123 tone="primary"
124 disabled={!canChangeThreadBranch || 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((result) => {
129 if (result === null) return;
130 setNewBranchName("");
131 navigation.goBack();
132 });
133 }}
134 />
135 </View>
136​
137 <View className="bg-card android:gap-3 android:rounded-[20px] android:p-4 ios:gap-2 ios:rounded-[18px] ios:border ios:border-border ios:px-4 ios:py-4">
138 <Text className="android:text-foreground android:text-base android:font-t3-medium ios:text-foreground-secondary ios:text-2xs ios:font-t3-bold ios:tracking-[1px] ios:uppercase">
139 New worktree
140 </Text>
141 {Platform.OS === "android" ? (
142 <Text className="text-foreground-secondary text-sm">Base branch</Text>
143 ) : null}
144 <TextInput
145 value={worktreeBaseBranch}
146 onChangeText={setWorktreeBaseBranchOverride}
147 placeholder="main"
148 accessibilityLabel="Worktree base branch"
149 className="android:rounded-xl android:bg-sheet-solid ios:rounded-[18px]"
150 />
151 {Platform.OS === "android" ? (
152 <Text className="text-foreground-secondary text-sm">New branch</Text>
153 ) : null}
154 <TextInput
155 value={worktreeBranchName}
156 onChangeText={setWorktreeBranchName}
157 placeholder="feature/mobile-thread"
158 accessibilityLabel="Worktree branch name"
159 className="android:rounded-xl android:bg-sheet-solid ios:rounded-[18px]"
160 />
161 <SheetActionButton
162 icon="square.split.2x1"
163 label="Create worktree"
164 tone="primary"
165 disabled={
166 !canChangeThreadBranch ||
167 busy ||
168 worktreeBaseBranch.trim().length === 0 ||
169 worktreeBranchName.trim().length === 0
170 }
171 onPress={() => {
172 const baseBranch = worktreeBaseBranch.trim();
173 const newBranch = worktreeBranchName.trim();
174 if (baseBranch.length === 0 || newBranch.length === 0) return;
175 void gitActions
176 .onCreateSelectedThreadWorktree({ baseBranch, newBranch })
177 .then((result) => {
178 if (result === null) return;
179 setWorktreeBranchName("");
180 navigation.goBack();
181 });
182 }}
183 />
184 </View>
185​
186 <View className="gap-2">
187 <Text className="text-foreground-secondary android:px-4 android:pb-1 android:pt-3 android:text-sm android:font-t3-medium ios:text-2xs ios:font-t3-bold ios:tracking-[1px] ios:uppercase">
188 Existing branches
189 </Text>
190 {branchesLoading ? (
191 <Text className="text-foreground-secondary text-sm font-medium android:px-4">
192 Loading branches...
193 </Text>
194 ) : null}
195 {!branchesLoading && availableBranches.length === 0 ? (
196 <Text className="text-foreground-secondary text-sm font-medium android:px-4">
197 No local branches found.
198 </Text>
199 ) : null}
200 {availableBranches.map((branch) => {
201 const disabled = disabledExistingBranches.has(branch.name);
202 const subtitle = branch.worktreePath
203 ? branch.worktreePath === currentWorktreePath
204 ? "Checked out in this thread"
205 : "Checked out in another worktree"
206 : branch.isDefault
207 ? "Default branch"
208 : "Local branch";
209​
210 return (
211 <Pressable
212 key={branch.name}
213 className={cn(
214 "gap-1 px-4 py-3 disabled:opacity-[0.45] android:rounded-[20px] android:active:bg-subtle ios:rounded-[18px] ios:border",
215 branch.current
216 ? "android:bg-secondary ios:border-subtle-strong"
217 : "android:bg-card ios:border-border",
218 )}
219 accessibilityRole="button"
220 accessibilityState={{ selected: branch.current, disabled: busy || disabled }}
221 disabled={!canChangeThreadBranch || busy || disabled}
222 onPress={() => {
223 void gitActions.onCheckoutSelectedThreadBranch(branch.name).then((result) => {
224 if (result === null) return;
225 navigation.goBack();
226 });
227 }}
228 >
229 {Platform.OS !== "android" ? (
230 <View className="absolute inset-0 rounded-[18px] bg-card" />
231 ) : null}
232 <Text className="text-foreground text-base android:font-t3-medium ios:font-t3-bold">
233 {branch.name}
234 </Text>
235 <Text className="text-foreground-secondary text-xs font-medium">{subtitle}</Text>
236 </Pressable>
237 );
238 })}
239 </View>
240 </ScrollView>
241 </ScreenContent>
242 </View>
243 );