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

GitBranchesSheet.tsx

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