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, View } from "react-native";
5import { useSafeAreaInsets } from "react-native-safe-area-context";
6
7import { AndroidSheetHeader } from "../../../components/AndroidScreenHeader";
8import { AppText as Text, AppTextInput as TextInput } from "../../../components/AppText";
9import { cn } from "../../../lib/cn";
10import { useEnvironmentQuery } from "../../../state/query";
11import { useThreadSelection } from "../../../state/use-thread-selection";
12import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions";
13import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state";
14import { useSelectedThreadWorktree } from "../../../state/use-selected-thread-worktree";
15import { vcsEnvironment } from "../../../state/vcs";
16import { SheetActionButton } from "./gitSheetComponents";
17
18type GitBranchesSheetProps = StaticScreenProps<{
19 readonly environmentId: string;
20 readonly threadId: string;
21}>;
22
23export function GitBranchesSheet(_props: GitBranchesSheetProps) {
24 const navigation = useNavigation();
25 const insets = useSafeAreaInsets();
26 const { selectedThread } = useThreadSelection();
27 const { selectedThreadCwd, selectedThreadWorktreePath } = useSelectedThreadWorktree();
28 const gitState = useSelectedThreadGitState();
29 const gitActions = useSelectedThreadGitActions();
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 <View collapsable={false} className="flex-1 bg-sheet">
62 {Platform.OS === "android" ? (
63 <AndroidSheetHeader title="Branches & worktrees" onBack={() => navigation.goBack()} />
64 ) : null}
65 <ScrollView
66 className="flex-1"
67 showsVerticalScrollIndicator={false}
68 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
69 contentContainerClassName="gap-4 px-5 pt-2"
70 >
71 <View className="gap-2 rounded-[18px] border border-border bg-card px-4 py-4">
72 <Text className="text-foreground-secondary text-2xs font-t3-bold tracking-[1px] uppercase">
73 New branch
74 </Text>
75 <TextInput
76 value={newBranchName}
77 onChangeText={setNewBranchName}
78 placeholder="feature/mobile-polish"
79 className="rounded-[18px]"
80 />
81 <SheetActionButton
82 icon="plus"
83 label="Create & checkout"
84 tone="primary"
85 disabled={busy || newBranchName.trim().length === 0}
86 onPress={() => {
87 const branch = sanitizeFeatureBranchName(newBranchName.trim());
88 if (branch.length === 0) return;
89 void gitActions.onCreateSelectedThreadBranch(branch).then(() => {
90 setNewBranchName("");
91 navigation.goBack();
92 });
93 }}
94 />
95 </View>
96
97 <View className="gap-2 rounded-[18px] border border-border bg-card px-4 py-4">
98 <Text className="text-foreground-secondary text-2xs font-t3-bold tracking-[1px] uppercase">
99 New worktree
100 </Text>
101 <TextInput
102 value={worktreeBaseBranch}
103 onChangeText={setWorktreeBaseBranch}
104 placeholder="main"
105 className="rounded-[18px]"
106 />
107 <TextInput
108 value={worktreeBranchName}
109 onChangeText={setWorktreeBranchName}
110 placeholder="feature/mobile-thread"
111 className="rounded-[18px]"
112 />
113 <SheetActionButton
114 icon="square.split.2x1"
115 label="Create worktree"
116 tone="primary"
117 disabled={
118 busy ||
119 worktreeBaseBranch.trim().length === 0 ||
120 worktreeBranchName.trim().length === 0
121 }
122 onPress={() => {
123 const baseBranch = worktreeBaseBranch.trim();
124 const newBranch = worktreeBranchName.trim();
125 if (baseBranch.length === 0 || newBranch.length === 0) return;
126 void gitActions.onCreateSelectedThreadWorktree({ baseBranch, newBranch }).then(() => {
127 setWorktreeBranchName("");
128 navigation.goBack();
129 });
130 }}
131 />
132 </View>
133
134 <View className="gap-2">
135 <Text className="text-foreground-secondary text-2xs font-t3-bold tracking-[1px] uppercase">
136 Existing branches
137 </Text>
138 {branchesLoading ? (
139 <Text className="text-foreground-secondary text-sm font-medium">
140 Loading branches...
141 </Text>
142 ) : null}
143 {!branchesLoading && availableBranches.length === 0 ? (
144 <Text className="text-foreground-secondary text-sm font-medium">
145 No local branches found.
146 </Text>
147 ) : null}
148 {availableBranches.map((branch) => {
149 const disabled = disabledExistingBranches.has(branch.name);
150 const subtitle = branch.worktreePath
151 ? branch.worktreePath === currentWorktreePath
152 ? "Checked out in this thread"
153 : "Checked out in another worktree"
154 : branch.isDefault
155 ? "Default branch"
156 : "Local branch";
157
158 return (
159 <Pressable
160 key={branch.name}
161 className={cn(
162 "gap-1 rounded-[18px] border px-4 py-3 disabled:opacity-[0.45]",
163 branch.current ? "border-subtle-strong" : "border-border",
164 )}
165 disabled={busy || disabled}
166 onPress={() => {
167 void gitActions.onCheckoutSelectedThreadBranch(branch.name).then(() => {
168 navigation.goBack();
169 });
170 }}
171 >
172 <View className="absolute inset-0 rounded-[18px] bg-card" />
173 <Text className="text-foreground text-base font-t3-bold">{branch.name}</Text>
174 <Text className="text-foreground-secondary text-xs font-medium">{subtitle}</Text>
175 </Pressable>
176 );
177 })}
178 </View>
179 </ScrollView>
180 </View>
181 );
182}