import { Box, Check, ChevronsUpDown, Plus } from 'lucide-react'
import Link from 'next/link'
import { useRouter } from 'next/router'
import { ParsedUrlQuery } from 'querystring'
import { useState } from 'react'
import { useParams } from 'common'
import { OrganizationProjectSelector } from 'components/ui/OrganizationProjectSelector'
import { useProjectDetailQuery } from 'data/projects/project-detail-query'
import { useIsFeatureEnabled } from 'hooks/misc/useIsFeatureEnabled'
import { useSelectedOrganizationQuery } from 'hooks/misc/useSelectedOrganization'
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
import { IS_PLATFORM } from 'lib/constants'
import { Badge, Button, CommandGroup_Shadcn_, CommandItem_Shadcn_, cn } from 'ui'
import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
export const sanitizeRoute = (route: string, routerQueries: ParsedUrlQuery) => {
const queryArray = Object.entries(routerQueries)
if (queryArray.length > 1) {
// [Joshen] Ideally we shouldn't use hard coded numbers, but temp workaround
// for storage bucket route since its longer
const isStorageBucketRoute = 'bucketId' in routerQueries
const isSecurityAdvisorRoute = 'preset' in routerQueries
return route
.split('/')
.slice(0, isStorageBucketRoute || isSecurityAdvisorRoute ? 5 : 4)
.join('/')
} else {
return route
}
}
export const ProjectDropdown = () => {
const router = useRouter()
const { ref } = useParams()
const { data: project, isPending: isLoadingProject } = useSelectedProjectQuery()
const { data: selectedOrganization } = useSelectedOrganizationQuery()
const isBranch = project?.parentRef !== project?.ref
const { data: parentProject, isPending: isLoadingParentProject } = useProjectDetailQuery(
{ ref: project?.parent_project_ref },
{ enabled: isBranch }
)
const selectedProject = parentProject ?? project
const projectCreationEnabled = useIsFeatureEnabled('projects:create')
const [open, setOpen] = useState(false)
if (isLoadingProject || (isBranch && isLoadingParentProject) || !selectedProject) {
return
}
return IS_PLATFORM ? (
<>
{selectedProject?.name}
{
const sanitizedRoute = sanitizeRoute(router.route, router.query)
const href = sanitizedRoute?.replace('[ref]', project.ref) ?? `/project/${project.ref}`
router.push(href)
}}
renderTrigger={() => (
}
/>
)}
renderRow={(project) => {
// [Joshen] Temp while we're interim between v1 and v2 billing
const sanitizedRoute = sanitizeRoute(router.route, router.query)
const href = sanitizedRoute?.replace('[ref]', project.ref) ?? `/project/${project.ref}`
const isSelected = project.ref === ref
const isPaused = project.status === 'INACTIVE'
return (
{project.name}
{isPaused && Paused}
{isSelected && }
)
}}
renderActions={() => {
return (
projectCreationEnabled && (
{
setOpen(false)
router.push(`/new/${selectedOrganization?.slug}`)
}}
onClick={() => setOpen(false)}
>
{
setOpen(false)
}}
className="w-full flex items-center gap-2"
>
New project
)
)
}}
/>
>
) : (
)
}