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

GitBranchesSheet.tsx

243 lines10.7 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 { AndroidSheetHeader } from "../../../components/ui/AndroidScreenHeader";
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 <AndroidSheetHeader
82 title="Branches & worktrees"
83 onBack={() => navigation.goBack()}
84 hideBottomBorder
85 />
86 ) : (
87 <SheetHeader
88 presentation="native"
89 title="Branches & worktrees"
90 onClose={() => navigation.goBack()}
91 />
92 )}
93 <ScreenContent fitToContents>
94 <ScrollView
95 nestedScrollEnabled
96 className="android:shrink android:grow-0 ios:flex-1"
97 contentInsetAdjustmentBehavior={Platform.OS === "ios" ? "automatic" : "never"}
98 keyboardShouldPersistTaps="handled"
99 showsVerticalScrollIndicator={false}
100 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
101 contentContainerClassName="android:gap-2 android:p-2 ios:gap-4 ios:px-5 ios:pt-2"
102 contentContainerStyle={
103 Platform.OS === "android"
104 ? { paddingBottom: Math.max(insets.bottom, 18) + 18 }
105 : undefined
106 }
107 >
108 <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">
109 <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">
110 New branch
111 </Text>
112 <TextInput
113 value={newBranchName}
114 onChangeText={setNewBranchName}
115 placeholder="feature/mobile-polish"
116 accessibilityLabel="New branch name"
117 className="android:rounded-xl android:bg-sheet-solid ios:rounded-[18px]"
118 />
119 <SheetActionButton
120 icon="plus"
121 label="Create & checkout"
122 tone="primary"
123 disabled={!canChangeThreadBranch || busy || newBranchName.trim().length === 0}
124 onPress={() => {
125 const branch = sanitizeFeatureBranchName(newBranchName.trim());
126 if (branch.length === 0) return;
127 void gitActions.onCreateSelectedThreadBranch(branch).then((result) => {
128 if (result === null) return;
129 setNewBranchName("");
130 navigation.goBack();
131 });
132 }}
133 />
134 </View>
135​
136 <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">
137 <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">
138 New worktree
139 </Text>
140 {Platform.OS === "android" ? (
141 <Text className="text-foreground-secondary text-sm">Base branch</Text>
142 ) : null}
143 <TextInput
144 value={worktreeBaseBranch}
145 onChangeText={setWorktreeBaseBranchOverride}
146 placeholder="main"
147 accessibilityLabel="Worktree base branch"
148 className="android:rounded-xl android:bg-sheet-solid ios:rounded-[18px]"
149 />
150 {Platform.OS === "android" ? (
151 <Text className="text-foreground-secondary text-sm">New branch</Text>
152 ) : null}
153 <TextInput
154 value={worktreeBranchName}
155 onChangeText={setWorktreeBranchName}
156 placeholder="feature/mobile-thread"
157 accessibilityLabel="Worktree branch name"
158 className="android:rounded-xl android:bg-sheet-solid ios:rounded-[18px]"
159 />
160 <SheetActionButton
161 icon="square.split.2x1"
162 label="Create worktree"
163 tone="primary"
164 disabled={
165 !canChangeThreadBranch ||
166 busy ||
167 worktreeBaseBranch.trim().length === 0 ||
168 worktreeBranchName.trim().length === 0
169 }
170 onPress={() => {
171 const baseBranch = worktreeBaseBranch.trim();
172 const newBranch = worktreeBranchName.trim();
173 if (baseBranch.length === 0 || newBranch.length === 0) return;
174 void gitActions
175 .onCreateSelectedThreadWorktree({ baseBranch, newBranch })
176 .then((result) => {
177 if (result === null) return;
178 setWorktreeBranchName("");
179 navigation.goBack();
180 });
181 }}
182 />
183 </View>
184​
185 <View className="gap-2">
186 <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">
187 Existing branches
188 </Text>
189 {branchesLoading ? (
190 <Text className="text-foreground-secondary text-sm font-medium android:px-4">
191 Loading branches...
192 </Text>
193 ) : null}
194 {!branchesLoading && availableBranches.length === 0 ? (
195 <Text className="text-foreground-secondary text-sm font-medium android:px-4">
196 No local branches found.
197 </Text>
198 ) : null}
199 {availableBranches.map((branch) => {
200 const disabled = disabledExistingBranches.has(branch.name);
201 const subtitle = branch.worktreePath
202 ? branch.worktreePath === currentWorktreePath
203 ? "Checked out in this thread"
204 : "Checked out in another worktree"
205 : branch.isDefault
206 ? "Default branch"
207 : "Local branch";
208​
209 return (
210 <Pressable
211 key={branch.name}
212 className={cn(
213 "gap-1 px-4 py-3 disabled:opacity-[0.45] android:rounded-[20px] android:active:bg-subtle ios:rounded-[18px] ios:border",
214 branch.current
215 ? "android:bg-secondary ios:border-subtle-strong"
216 : "android:bg-card ios:border-border",
217 )}
218 accessibilityRole="button"
219 accessibilityState={{ selected: branch.current, disabled: busy || disabled }}
220 disabled={!canChangeThreadBranch || busy || disabled}
221 onPress={() => {
222 void gitActions.onCheckoutSelectedThreadBranch(branch.name).then((result) => {
223 if (result === null) return;
224 navigation.goBack();
225 });
226 }}
227 >
228 {Platform.OS !== "android" ? (
229 <View className="absolute inset-0 rounded-[18px] bg-card" />
230 ) : null}
231 <Text className="text-foreground text-base android:font-t3-medium ios:font-t3-bold">
232 {branch.name}
233 </Text>
234 <Text className="text-foreground-secondary text-xs font-medium">{subtitle}</Text>
235 </Pressable>
236 );
237 })}
238 </View>
239 </ScrollView>
240 </ScreenContent>
241 </View>
242 );