import { AlertCircle, Check, ChevronsUpDown, ListTree, MessageCircle, Plus, Shield, } from 'lucide-react' import Link from 'next/link' import { useRouter } from 'next/router' import { useState } from 'react' import { useParams } from 'common' import { Branch, useBranchesQuery } from 'data/branches/branches-query' import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject' import { useAppStateSnapshot } from 'state/app-state' import { Badge, Button, CommandEmpty_Shadcn_, CommandGroup_Shadcn_, CommandInput_Shadcn_, CommandItem_Shadcn_, CommandList_Shadcn_, CommandSeparator_Shadcn_, Command_Shadcn_, PopoverContent_Shadcn_, PopoverTrigger_Shadcn_, Popover_Shadcn_, ScrollArea, cn, } from 'ui' import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader' import { sanitizeRoute } from './ProjectDropdown' import { partition } from 'lodash' const BranchLink = ({ branch, isSelected, setOpen, }: { branch: Branch isSelected: boolean setOpen: (value: boolean) => void }) => { const router = useRouter() const sanitizedRoute = sanitizeRoute(router.route, router.query) const href = sanitizedRoute?.replace('[ref]', branch.project_ref) ?? `/project/${branch.project_ref}` return ( { setOpen(false) router.push(href) }} onClick={() => { setOpen(false) }} >

{branch.is_default && } {branch.name}

{isSelected && }
) } export const BranchDropdown = () => { const router = useRouter() const { ref } = useParams() const snap = useAppStateSnapshot() const { data: projectDetails } = useSelectedProjectQuery() const [open, setOpen] = useState(false) const projectRef = projectDetails?.parent_project_ref || ref const { data: branches, isPending: isLoading, isError, isSuccess, } = useBranchesQuery({ projectRef }, { enabled: Boolean(projectDetails) }) const isBranchingEnabled = projectDetails?.is_branch_enabled === true const selectedBranch = branches?.find((branch) => branch.project_ref === ref) const defaultMainBranch = { id: 'main', name: 'main', project_ref: projectRef ?? ref ?? '', is_default: true, } as unknown as Branch const mainBranch = branches?.find((branch) => branch.is_default) const restOfBranches = branches ?.filter((branch) => !branch.is_default) ?.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()) const sortedBranches = branches && branches.length > 0 ? mainBranch ? [mainBranch].concat(restOfBranches ?? []) : restOfBranches ?? [] : [defaultMainBranch] const branchList = isBranchingEnabled ? sortedBranches ?? [] : [defaultMainBranch] const BRANCHING_GITHUB_DISCUSSION_LINK = 'https://github.com/orgs/supabase/discussions/18937' return ( <> {isLoading && } {isError && (

Failed to load branches

)} {isSuccess && ( <> {isBranchingEnabled ? selectedBranch?.name : 'main'} {isBranchingEnabled ? ( selectedBranch?.is_default ? ( Production ) : selectedBranch?.persistent ? ( Persistent ) : ( Preview ) ) : ( Production )}