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

GitBranchesSheet.tsx

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