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

GitBranchesSheet.tsx

204 lines7.7 KB

1import { sanitizeFeatureBranchName } from "@t3tools/shared/git";
2import { useRouter } from "expo-router";
3import { useState } from "react";
4import { Pressable, ScrollView, View } from "react-native";
5import { useSafeAreaInsets } from "react-native-safe-area-context";
6import { useThemeColor } from "../../../lib/useThemeColor";
7​
8import { AppText as Text, AppTextInput as TextInput } from "../../../components/AppText";
9import { useEnvironmentQuery } from "../../../state/query";
10import { useThreadSelection } from "../../../state/use-thread-selection";
11import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions";
12import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state";
13import { useSelectedThreadWorktree } from "../../../state/use-selected-thread-worktree";
14import { vcsEnvironment } from "../../../state/vcs";
15import { SheetActionButton } from "./gitSheetComponents";
16​
17export function GitBranchesSheet() {
18 const router = useRouter();
19 const insets = useSafeAreaInsets();
20 const { selectedThread } = useThreadSelection();
21 const { selectedThreadCwd, selectedThreadWorktreePath } = useSelectedThreadWorktree();
22 const gitState = useSelectedThreadGitState();
23 const gitActions = useSelectedThreadGitActions();
24​
25 const borderColor = useThemeColor("--color-border");
26 const inputBorderColor = useThemeColor("--color-input-border");
27 const inputBg = useThemeColor("--color-input");
28 const foregroundColor = useThemeColor("--color-foreground");
29 const subtleStrongColor = useThemeColor("--color-subtle-strong");
30​
31 const gitStatus = useEnvironmentQuery(
32 selectedThread !== null && selectedThreadCwd !== null
33 ? vcsEnvironment.status({
34 environmentId: selectedThread.environmentId,
35 input: { cwd: selectedThreadCwd },
36 })
37 : null,
38 );
39​
40 const currentBranchLabel = gitStatus.data?.refName ?? selectedThread?.branch ?? "Detached HEAD";
41 const currentWorktreePath = selectedThreadWorktreePath;
42 const availableBranches = gitState.selectedThreadBranches;
43 const branchesLoading = gitState.selectedThreadBranchesLoading;
44 const busy = gitState.gitOperationLabel !== null;
45​
46 const [newBranchName, setNewBranchName] = useState("");
47 const [worktreeBaseBranch, setWorktreeBaseBranch] = useState(
48 currentBranchLabel === "Detached HEAD" ? "main" : currentBranchLabel,
49 );
50 const [worktreeBranchName, setWorktreeBranchName] = useState("");
51​
52 const disabledExistingBranchNames: Array<string> = [];
53 for (const branch of availableBranches) {
54 if (branch.worktreePath !== null && branch.worktreePath !== currentWorktreePath) {
55 disabledExistingBranchNames.push(branch.name);
56 }
57 }
58 const disabledExistingBranches = new Set(disabledExistingBranchNames);
59​
60 return (
61 <ScrollView
62 showsVerticalScrollIndicator={false}
63 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
64 contentContainerStyle={{
65 paddingHorizontal: 20,
66 paddingTop: 8,
67 gap: 16,
68 }}
69 >
70 <View className="gap-2 rounded-[18px] border border-border bg-card px-4 py-4">
71 <Text
72 className="text-foreground-secondary text-2xs font-t3-bold uppercase"
73 style={{ letterSpacing: 1 }}
74 >
75 New branch
76 </Text>
77 <TextInput
78 value={newBranchName}
79 onChangeText={setNewBranchName}
80 placeholder="feature/mobile-polish"
81 className="rounded-[18px] px-3.5 py-3 font-sans text-base"
82 style={{
83 borderWidth: 1,
84 borderColor: inputBorderColor,
85 backgroundColor: inputBg,
86 color: foregroundColor,
87 }}
88 />
89 <SheetActionButton
90 icon="plus"
91 label="Create & checkout"
92 tone="primary"
93 disabled={busy || newBranchName.trim().length === 0}
94 onPress={() => {
95 const branch = sanitizeFeatureBranchName(newBranchName.trim());
96 if (branch.length === 0) return;
97 void gitActions.onCreateSelectedThreadBranch(branch).then(() => {
98 setNewBranchName("");
99 router.dismiss();
100 });
101 }}
102 />
103 </View>
104​
105 <View className="gap-2 rounded-[18px] border border-border bg-card px-4 py-4">
106 <Text
107 className="text-foreground-secondary text-2xs font-t3-bold uppercase"
108 style={{ letterSpacing: 1 }}
109 >
110 New worktree
111 </Text>
112 <TextInput
113 value={worktreeBaseBranch}
114 onChangeText={setWorktreeBaseBranch}
115 placeholder="main"
116 className="rounded-[18px] px-3.5 py-3 font-sans text-base"
117 style={{
118 borderWidth: 1,
119 borderColor: inputBorderColor,
120 backgroundColor: inputBg,
121 color: foregroundColor,
122 }}
123 />
124 <TextInput
125 value={worktreeBranchName}
126 onChangeText={setWorktreeBranchName}
127 placeholder="feature/mobile-thread"
128 className="rounded-[18px] px-3.5 py-3 font-sans text-base"
129 style={{
130 borderWidth: 1,
131 borderColor: inputBorderColor,
132 backgroundColor: inputBg,
133 color: foregroundColor,
134 }}
135 />
136 <SheetActionButton
137 icon="square.split.2x1"
138 label="Create worktree"
139 tone="primary"
140 disabled={
141 busy || worktreeBaseBranch.trim().length === 0 || worktreeBranchName.trim().length === 0
142 }
143 onPress={() => {
144 const baseBranch = worktreeBaseBranch.trim();
145 const newBranch = worktreeBranchName.trim();
146 if (baseBranch.length === 0 || newBranch.length === 0) return;
147 void gitActions.onCreateSelectedThreadWorktree({ baseBranch, newBranch }).then(() => {
148 setWorktreeBranchName("");
149 router.dismiss();
150 });
151 }}
152 />
153 </View>
154​
155 <View className="gap-2">
156 <Text
157 className="text-foreground-secondary text-2xs font-t3-bold uppercase"
158 style={{ letterSpacing: 1 }}
159 >
160 Existing branches
161 </Text>
162 {branchesLoading ? (
163 <Text className="text-foreground-secondary text-sm font-medium">Loading branches...</Text>
164 ) : null}
165 {!branchesLoading && availableBranches.length === 0 ? (
166 <Text className="text-foreground-secondary text-sm font-medium">
167 No local branches found.
168 </Text>
169 ) : null}
170 {availableBranches.map((branch) => {
171 const disabled = disabledExistingBranches.has(branch.name);
172 const subtitle = branch.worktreePath
173 ? branch.worktreePath === currentWorktreePath
174 ? "Checked out in this thread"
175 : "Checked out in another worktree"
176 : branch.isDefault
177 ? "Default branch"
178 : "Local branch";
179​
180 return (
181 <Pressable
182 key={branch.name}
183 className="gap-1 rounded-[18px] border px-4 py-3"
184 disabled={busy || disabled}
185 style={{
186 borderColor: branch.current ? subtleStrongColor : borderColor,
187 opacity: busy || disabled ? 0.45 : 1,
188 }}
189 onPress={() => {
190 void gitActions.onCheckoutSelectedThreadBranch(branch.name).then(() => {
191 router.dismiss();
192 });
193 }}
194 >
195 <View className="absolute inset-0 rounded-[18px] bg-card" />
196 <Text className="text-foreground text-base font-t3-bold">{branch.name}</Text>
197 <Text className="text-foreground-secondary text-xs font-medium">{subtitle}</Text>
198 </Pressable>
199 );
200 })}
201 </View>
202 </ScrollView>
203 );