"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { GitBranch, Check, ChevronDown } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { cn } from "@/lib/utils";
export function BranchSelector({
branches,
currentBranch,
username,
repoName,
basePath = "",
}: {
branches: string[];
currentBranch: string;
username: string;
repoName: string;
basePath?: string;
}) {
const router = useRouter();
const [open, setOpen] = useState(false);
function handleSelect(branch: string) {
setOpen(false);
if (branch === currentBranch) return;
if (basePath) {
router.push(`/${username}/${repoName}/tree/${branch}/${basePath}`);
} else {
router.push(`/${username}/${repoName}/tree/${branch}`);
}
}
if (branches.length === 0) {
return (
);
}
return (
Switch branches
{branches.map((branch) => (
handleSelect(branch)}
className={cn(
"cursor-pointer gap-2",
branch === currentBranch && "bg-accent/10"
)}
>
{branch}
))}
);
}