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 ShimmeringLoader from 'components/ui/ShimmeringLoader' import { ProjectInfo, useProjectsQuery } from 'data/projects/projects-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 type { Organization } from 'types' import { 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' // [Fran] the idea is to let users change projects without losing the current page, // but at the same time we need to redirect correctly between urls that might be // unique to a project e.g. '/project/projectRef/editor/tableId' // Right now, I'm gonna assume that any router query after the projectId, // is a unique project id/marker so we'll redirect the user to the // highest common route with just projectRef in the router queries. 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 } } const ProjectLink = ({ project, setOpen, }: { project: ProjectInfo organization?: Organization setOpen: (value: boolean) => void }) => { const router = useRouter() const { ref } = useParams() const sanitizedRoute = sanitizeRoute(router.route, router.query) // [Joshen] Temp while we're interim between v1 and v2 billing let href = sanitizedRoute?.replace('[ref]', project.ref) ?? `/project/${project.ref}` return ( { router.push(href) setOpen(false) }} onClick={() => setOpen(false)} > {project.name} {project.ref === ref && } ) } export const ProjectDropdown = () => { const router = useRouter() const { ref } = useParams() const { data: project } = useSelectedProjectQuery() const { data, isLoading: isLoadingProjects } = useProjectsQuery() const { data: selectedOrganization } = useSelectedOrganizationQuery() const projectCreationEnabled = useIsFeatureEnabled('projects:create') const isBranch = project?.parentRef !== project?.ref const projects = (data?.projects ?? []) .filter((x) => x.organization_id === selectedOrganization?.id) .sort((a, b) => a.name.localeCompare(b.name)) const selectedProject = isBranch ? projects?.find((p) => p.ref === project?.parentRef) : projects?.find((p) => p.ref === ref) const [open, setOpen] = useState(false) if (isLoadingProjects || !selectedProject) { return } return IS_PLATFORM ? ( <> {selectedProject?.name} ) }