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

GitBranchesSheet.tsx

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