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

GitBranchesSheet.tsx

176 lines6.7 KB

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