The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 174 lines 6.59 KB
Raw
1 import { sanitizeFeatureBranchName } from "@t3tools/shared/git";
2 import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
3 import { useGT } from "gt-react-native";
4 import { useState } from "react";
5
6 import { SheetHeader } from "../../../components/ui/SheetHeader";
7 import { FitSheet } from "../../../components/ui/FitSheet";
8 import { FormField, FormSection } from "../../../components/ui/FormSection";
9 import { ListChoiceRow } from "../../../components/ui/ListChoiceRow";
10 import { ListSection } from "../../../components/ui/ListSection";
11 import { useEnvironmentQuery } from "../../../state/query";
12 import { useThreadSelection } from "../../../state/use-thread-selection";
13 import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions";
14 import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state";
15 import { useSelectedThreadWorktree } from "../../../state/use-selected-thread-worktree";
16 import { vcsEnvironment } from "../../../state/vcs";
17 import { SheetActionButton } from "./gitSheetComponents";
18
19 type GitBranchesSheetProps = StaticScreenProps<{
20 readonly environmentId: string;
21 readonly threadId: string;
22 }>;
23
24 export function GitBranchesSheet(_props: GitBranchesSheetProps) {
25 const gt = useGT();
26 const navigation = useNavigation();
27 const { selectedThread } = useThreadSelection();
28 const { selectedThreadCwd } = useSelectedThreadWorktree();
29 const gitState = useSelectedThreadGitState();
30 const gitActions = useSelectedThreadGitActions();
31 const { canChangeThreadBranch } = gitActions;
32
33 const gitStatus = useEnvironmentQuery(
34 selectedThread !== null && selectedThreadCwd !== null
35 ? vcsEnvironment.branch({
36 environmentId: selectedThread.environmentId,
37 input: { cwd: selectedThreadCwd },
38 })
39 : null,
40 );
41
42 const currentBranch = gitStatus.data?.refName ?? selectedThread?.branch ?? null;
43 const currentWorktreePath = selectedThreadCwd;
44 const availableBranches = gitState.selectedThreadBranches;
45 const branchesLoading = gitState.selectedThreadBranchesLoading;
46 const busy = gitState.gitOperationLabel !== null;
47
48 const [newBranchName, setNewBranchName] = useState("");
49 const [worktreeBaseBranchOverride, setWorktreeBaseBranchOverride] = useState<string | null>(null);
50 const worktreeBaseBranch =
51 worktreeBaseBranchOverride ?? (currentBranch === null ? "main" : currentBranch);
52 const [worktreeBranchName, setWorktreeBranchName] = useState("");
53
54 const disabledExistingBranchNames: Array<string> = [];
55 for (const branch of availableBranches) {
56 if (branch.worktreePath !== null && branch.worktreePath !== currentWorktreePath) {
57 disabledExistingBranchNames.push(branch.name);
58 }
59 }
60 const disabledExistingBranches = new Set(disabledExistingBranchNames);
61
62 return (
63 <FitSheet
64 header={
65 <SheetHeader title={gt("Branches & worktrees")} onClose={() => navigation.goBack()} />
66 }
67 keyboardShouldPersistTaps="handled"
68 showsVerticalScrollIndicator={false}
69 >
70 <FormSection title={gt("New branch")}>
71 <FormField
72 value={newBranchName}
73 onChangeText={setNewBranchName}
74 placeholder="feature/mobile-polish"
75 accessibilityLabel={gt("New branch name")}
76 />
77 <SheetActionButton
78 icon="plus"
79 label={gt("Create & checkout")}
80 tone="primary"
81 disabled={!canChangeThreadBranch || busy || newBranchName.trim().length === 0}
82 onPress={() => {
83 const branch = sanitizeFeatureBranchName(newBranchName.trim());
84 if (branch.length === 0) return;
85 void gitActions.onCreateSelectedThreadBranch(branch).then((result) => {
86 if (result === null || !navigation.isFocused()) return;
87 setNewBranchName("");
88 navigation.goBack();
89 });
90 }}
91 />
92 </FormSection>
93
94 <FormSection title={gt("New worktree")}>
95 <FormField
96 label={gt("Base branch")}
97 value={worktreeBaseBranch}
98 onChangeText={setWorktreeBaseBranchOverride}
99 placeholder="main"
100 accessibilityLabel={gt("Worktree base branch")}
101 />
102 <FormField
103 label={gt("New branch")}
104 value={worktreeBranchName}
105 onChangeText={setWorktreeBranchName}
106 placeholder="feature/mobile-thread"
107 accessibilityLabel={gt("Worktree branch name")}
108 />
109 <SheetActionButton
110 icon="square.split.2x1"
111 label={gt("Create worktree")}
112 tone="primary"
113 disabled={
114 !canChangeThreadBranch ||
115 busy ||
116 worktreeBaseBranch.trim().length === 0 ||
117 worktreeBranchName.trim().length === 0
118 }
119 onPress={() => {
120 const baseBranch = worktreeBaseBranch.trim();
121 const newBranch = worktreeBranchName.trim();
122 if (baseBranch.length === 0 || newBranch.length === 0) return;
123 void gitActions
124 .onCreateSelectedThreadWorktree({ baseBranch, newBranch })
125 .then((result) => {
126 if (result === null || !navigation.isFocused()) return;
127 setWorktreeBranchName("");
128 navigation.goBack();
129 });
130 }}
131 />
132 </FormSection>
133
134 <ListSection
135 title={gt("Existing branches")}
136 footer={
137 branchesLoading
138 ? gt("Loading branches...")
139 : availableBranches.length === 0
140 ? gt("No local branches found.")
141 : undefined
142 }
143 >
144 {availableBranches.map((branch, index) => {
145 const disabled = disabledExistingBranches.has(branch.name);
146 const subtitle = branch.worktreePath
147 ? branch.worktreePath === currentWorktreePath
148 ? gt("Checked out in this thread")
149 : gt("Checked out in another worktree")
150 : branch.isDefault
151 ? gt("Default branch")
152 : gt("Local branch");
153
154 return (
155 <ListChoiceRow
156 key={branch.name}
157 label={branch.name}
158 description={subtitle}
159 selected={branch.current}
160 separated={index > 0}
161 disabled={!canChangeThreadBranch || busy || disabled}
162 onPress={() => {
163 void gitActions.onCheckoutSelectedThreadBranch(branch.name).then((result) => {
164 if (result === null || !navigation.isFocused()) return;
165 navigation.goBack();
166 });
167 }}
168 />
169 );
170 })}
171 </ListSection>
172 </FitSheet>
173 );
174 }