Skip to content

Commit 1e88778

Browse files
authored
Merge pull request #41 from beNative/codex/add-switch-to-main-branch-button-cvk914
Add main branch switch button to repository cards
2 parents 3463fca + 2950e27 commit 1e88778

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

components/RepositoryCard.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { TerminalIcon } from './icons/TerminalIcon';
1919
import { StatusIndicator } from './StatusIndicator';
2020
import { ChevronDownIcon } from './icons/ChevronDownIcon';
2121
import { MagnifyingGlassIcon } from './icons/MagnifyingGlassIcon';
22+
import { HomeIcon } from './icons/HomeIcon';
2223
import { ClockIcon } from './icons/ClockIcon';
2324
import { useTooltip } from '../hooks/useTooltip';
2425
import { TooltipContext } from '../contexts/TooltipContext';
@@ -89,6 +90,11 @@ const BranchSwitcher: React.FC<{
8990
const remoteBranches = branchInfo?.remote ?? [];
9091
const currentBranch = branchInfo?.current ?? '';
9192

93+
const remoteMainBranch = remoteBranches.find(branch => branch.split('/').slice(-1)[0] === 'main');
94+
const hasLocalMainBranch = localBranches.includes('main');
95+
const mainBranchTarget = hasLocalMainBranch ? 'main' : remoteMainBranch;
96+
const showMainBranchButton = Boolean(mainBranchTarget && currentBranch !== 'main');
97+
9298
const remoteBranchesToOffer = remoteBranches.filter(rBranch => {
9399
const localEquivalent = rBranch.split('/').slice(1).join('/');
94100
return !localBranches.includes(localEquivalent);
@@ -105,6 +111,13 @@ const BranchSwitcher: React.FC<{
105111
const branchSearchTooltip = useTooltip(
106112
isRefreshingBranches ? 'Refreshing branches…' : 'Search all branches'
107113
);
114+
const mainBranchTooltip = useTooltip(
115+
mainBranchTarget
116+
? hasLocalMainBranch
117+
? 'Switch to the main branch'
118+
: 'Track and switch to the remote main branch'
119+
: 'Main branch not available'
120+
);
108121

109122
useEffect(() => {
110123
if (isOpen && buttonRef.current) {
@@ -157,6 +170,12 @@ const BranchSwitcher: React.FC<{
157170
onClose();
158171
};
159172

173+
const handleSwitchToMain = () => {
174+
if (!mainBranchTarget) return;
175+
onSwitchBranch(repoId, mainBranchTarget);
176+
onClose();
177+
};
178+
160179
const openModal = async () => {
161180
setIsRefreshingBranches(true);
162181
try {
@@ -245,6 +264,17 @@ const BranchSwitcher: React.FC<{
245264
<MagnifyingGlassIcon className="h-4 w-4" />
246265
)}
247266
</button>
267+
{showMainBranchButton && (
268+
<button
269+
{...mainBranchTooltip}
270+
type="button"
271+
onClick={handleSwitchToMain}
272+
className="flex-shrink-0 p-1.5 rounded-md text-gray-500 hover:text-green-600 dark:text-gray-300 dark:hover:text-green-400 hover:bg-green-50 dark:hover:bg-green-900/40 focus:outline-none focus:ring-2 focus:ring-green-500"
273+
aria-label="Switch to main branch"
274+
>
275+
<HomeIcon className="h-4 w-4" />
276+
</button>
277+
)}
248278

249279
{isOpen && hasOptions && createPortal(DropdownContent, document.body)}
250280
<BranchSelectionModal

0 commit comments

Comments
 (0)