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