Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Update AppHeader org and project selections to use combobox

Joshen Limc22cbc00410c
diff --git a/studio/components/layouts/AppLayout/AppHeader.tsx b/studio/components/layouts/AppLayout/AppHeader.tsx
index f25ffe0dff4..3aa96a11853 100644
--- a/studio/components/layouts/AppLayout/AppHeader.tsx
+++ b/studio/components/layouts/AppLayout/AppHeader.tsx
@@ -5,7 +5,7 @@ import { useSelectedOrganization, useSelectedProject } from 'hooks'
import FeedbackDropdown from '../ProjectLayout/LayoutHeader/FeedbackDropdown'
import HelpPopover from '../ProjectLayout/LayoutHeader/HelpPopover'
import NotificationsPopover from '../ProjectLayout/LayoutHeader/NotificationsPopover'
-import ProjectDropdown from '../ProjectLayout/LayoutHeader/ProjectDropdown'
+import ProjectDropdown from './ProjectDropdown'
import OrganizationDropdown from './OrganizationDropdown'
import SettingsButton from './SettingsButton'
import UserSettingsDropdown from './UserSettingsDropdown'
diff --git a/studio/components/layouts/AppLayout/OrganizationDropdown.tsx b/studio/components/layouts/AppLayout/OrganizationDropdown.tsx
index 80937af89bf..210df04dcb8 100644
--- a/studio/components/layouts/AppLayout/OrganizationDropdown.tsx
+++ b/studio/components/layouts/AppLayout/OrganizationDropdown.tsx
@@ -1,74 +1,106 @@
-import Link from 'next/link'
-import { Badge, Button, Dropdown, IconCode, IconPlus } from 'ui'
+import { useRouter } from 'next/router'
+import { useState } from 'react'
+import {
+ Badge,
+ Button,
+ CommandEmpty_Shadcn_,
+ CommandGroup_Shadcn_,
+ CommandInput_Shadcn_,
+ CommandItem_Shadcn_,
+ CommandList_Shadcn_,
+ Command_Shadcn_,
+ IconCode,
+ IconPlus,
+ Popover,
+ PopoverContent_Shadcn_,
+ PopoverTrigger_Shadcn_,
+ Popover_Shadcn_,
+} from 'ui'
import { useOrganizationsQuery } from 'data/organizations/organizations-query'
-import { useFlag, useSelectedOrganization } from 'hooks'
-import { IS_PLATFORM } from 'lib/constants'
import { useOrgSubscriptionQuery } from 'data/subscriptions/org-subscription-query'
+import { useFlag, useSelectedOrganization } from 'hooks'
import ShimmeringLoader from 'components/ui/ShimmeringLoader'
-import { useRouter } from 'next/router'
+import Link from 'next/link'
const OrganizationDropdown = () => {
const router = useRouter()
- const { data: organizations, isLoading: isLoadingOrganizations } = useOrganizationsQuery()
- const selectedOrganization = useSelectedOrganization()
const orgCreationV2 = useFlag('orgcreationv2')
+ const selectedOrganization = useSelectedOrganization()
+ const { data: organizations, isLoading: isLoadingOrganizations } = useOrganizationsQuery()
const slug = selectedOrganization?.slug
const orgName = selectedOrganization?.name
const { data, isSuccess } = useOrgSubscriptionQuery({ orgSlug: slug })
+ const [open, setOpen] = useState(false)
+
if (isLoadingOrganizations) {
return <ShimmeringLoader className="w-[90px]" />
}
- return IS_PLATFORM ? (
- <Dropdown
- side="bottom"
- align="start"
- overlay={
- <>
- {organizations
- ?.sort((a, b) => a.name.localeCompare(b.name))
- .map((x) => {
- const href = router.pathname.includes('[slug]')
- ? router.pathname.replace('[slug]', x.slug)
- : router.pathname.includes('[ref]/settings')
- ? `/org/${x.slug}/general`
- : `/org/${x.slug}`
- return (
- <Link key={x.slug} href={href}>
- <a>
- <Dropdown.Item>{x.name}</Dropdown.Item>
- </a>
- </Link>
- )
- })}
- <Dropdown.Separator />
- <Link href={orgCreationV2 ? `/new-with-subscription` : `/new`}>
- <a>
- <Dropdown.Item icon={<IconPlus size="tiny" />}>New organization</Dropdown.Item>
- </a>
- </Link>
- </>
- }
- >
- <Button
- type="text"
- iconRight={<IconCode className="text-scale-1100 rotate-90" strokeWidth={2} size={12} />}
- >
- <span className="text-sm">{orgName}</span>
- {isSuccess && (
- <Badge color="slate" className="ml-2">
- {data?.plan.name}
- </Badge>
- )}
- </Button>
- </Dropdown>
- ) : (
- <Button type="text">
- <span className="text-sm">{orgName}</span>
- </Button>
+ return (
+ <div className="flex items-center space-x-2 px-2">
+ <Link href={`/org/${slug}`}>
+ <a className="flex items-center space-x-2">
+ <p className="text-sm">{orgName}</p>
+ {isSuccess && <Badge color="slate">{data?.plan.name}</Badge>}
+ </a>
+ </Link>
+
+ <Popover_Shadcn_ open={open} onOpenChange={setOpen}>
+ <PopoverTrigger_Shadcn_ asChild>
+ <Button
+ type="text"
+ className="px-1"
+ icon={<IconCode className="text-scale-1100 rotate-90" strokeWidth={2} size={12} />}
+ />
+ </PopoverTrigger_Shadcn_>
+ <PopoverContent_Shadcn_ className="p-0" side="bottom" align="start">
+ <Command_Shadcn_>
+ <CommandInput_Shadcn_ placeholder="Find organization..." />
+ <CommandList_Shadcn_>
+ <CommandEmpty_Shadcn_>No results found.</CommandEmpty_Shadcn_>
+ <CommandGroup_Shadcn_>
+ {organizations
+ ?.sort((a, b) => a.name.localeCompare(b.name))
+ .map((org) => {
+ const href = router.pathname.includes('[slug]')
+ ? router.pathname.replace('[slug]', org.slug)
+ : router.pathname.includes('[ref]/settings')
+ ? `/org/${org.slug}/general`
+ : `/org/${org.slug}`
+ return (
+ <CommandItem_Shadcn_
+ key={org.slug}
+ value={org.name}
+ className="cursor-pointer"
+ onSelect={() => {
+ setOpen(false)
+ }}
+ >
+ <Link passHref href={href}>
+ <a className="w-full">{org.name}</a>
+ </Link>
+ </CommandItem_Shadcn_>
+ )
+ })}
+ </CommandGroup_Shadcn_>
+ <CommandGroup_Shadcn_ className="border-t">
+ <CommandItem_Shadcn_ className="cursor-pointer" onSelect={() => setOpen(false)}>
+ <Link passHref href={orgCreationV2 ? `/new-with-subscription` : `/new`}>
+ <a className="flex items-center space-x-2 w-full">
+ <IconPlus size={14} strokeWidth={1.5} />
+ <p>New organization</p>
+ </a>
+ </Link>
+ </CommandItem_Shadcn_>
+ </CommandGroup_Shadcn_>
+ </CommandList_Shadcn_>
+ </Command_Shadcn_>
+ </PopoverContent_Shadcn_>
+ </Popover_Shadcn_>
+ </div>
)
}
diff --git a/studio/components/layouts/AppLayout/OrganizationDropdownV2.tsx b/studio/components/layouts/AppLayout/OrganizationDropdownV2.tsx
deleted file mode 100644
index 005b571fbac..00000000000
--- a/studio/components/layouts/AppLayout/OrganizationDropdownV2.tsx
+++ /dev/null
@@ -1 +0,0 @@
-// [Joshen] TODO use Combobox
diff --git a/studio/components/layouts/AppLayout/ProjectDropdown.tsx b/studio/components/layouts/AppLayout/ProjectDropdown.tsx
new file mode 100644
index 00000000000..c666c6565dd
--- /dev/null
+++ b/studio/components/layouts/AppLayout/ProjectDropdown.tsx
@@ -0,0 +1,165 @@
+import Link from 'next/link'
+import { useRouter } from 'next/router'
+import { ParsedUrlQuery } from 'querystring'
+import { useState } from 'react'
+import {
+ Badge,
+ Button,
+ CommandEmpty_Shadcn_,
+ CommandGroup_Shadcn_,
+ CommandInput_Shadcn_,
+ CommandItem_Shadcn_,
+ CommandList_Shadcn_,
+ Command_Shadcn_,
+ IconCode,
+ IconPlus,
+ PopoverContent_Shadcn_,
+ PopoverTrigger_Shadcn_,
+ Popover_Shadcn_,
+} from 'ui'
+
+import ShimmeringLoader from 'components/ui/ShimmeringLoader'
+import { useProjectsQuery } from 'data/projects/projects-query'
+import { useProjectSubscriptionV2Query } from 'data/subscriptions/project-subscription-v2-query'
+import { useSelectedOrganization, useSelectedProject } from 'hooks'
+import { IS_PLATFORM, PROJECT_STATUS } from 'lib/constants'
+import { Organization, Project } from 'types'
+
+// [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
+ return route
+ .split('/')
+ .slice(0, isStorageBucketRoute ? 5 : 4)
+ .join('/')
+ } else {
+ return route
+ }
+}
+
+const ProjectLink = ({
+ project,
+ organization,
+ setOpen,
+}: {
+ project: Project
+ organization?: Organization
+ setOpen: (value: boolean) => void
+}) => {
+ const router = useRouter()
+ const sanitizedRoute = sanitizeRoute(router.route, router.query)
+ const isOrgBilling = !!organization?.subscription_id
+
+ // [Joshen] Temp while we're interim between v1 and v2 billing
+ let href = sanitizedRoute?.replace('[ref]', project.ref) ?? `/project/${project.ref}`
+ if (href.endsWith('settings/addons') && !isOrgBilling) {
+ href = href.replace('settings/addons', 'settings/billing/subscription')
+ } else if (href.endsWith('settings/billing/subscription') && isOrgBilling) {
+ href = href.replace('settings/billing/subscription', 'settings/addons')
+ } else if (href.endsWith('settings/infrastructure') && !isOrgBilling) {
+ href = href.replace('settings/infrastructure', 'settings/billing/usage')
+ } else if (href.endsWith('settings/billing/usage') && !isOrgBilling) {
+ href = href.replace('settings/billing/usage', 'settings/infrastructure')
+ }
+
+ return (
+ <CommandItem_Shadcn_
+ key={project.ref}
+ value={project.name}
+ className="cursor-pointer"
+ onSelect={() => setOpen(false)}
+ >
+ <Link passHref href={href}>
+ <a className="w-full">{project.name}</a>
+ </Link>
+ </CommandItem_Shadcn_>
+ )
+}
+
+const ProjectDropdown = ({ alt }: { alt?: boolean }) => {
+ const selectedProject = useSelectedProject()
+ const selectedOrganization = useSelectedOrganization()
+ const { data: allProjects, isLoading: isLoadingProjects } = useProjectsQuery()
+
+ const isOrgBilling = !!selectedOrganization?.subscription_id
+ const { data: subscription, isSuccess } = useProjectSubscriptionV2Query(
+ { projectRef: selectedProject?.ref },
+ { enabled: alt && !isOrgBilling }
+ )
+ const projects = allProjects
+ ?.filter((x) => x.status !== PROJECT_STATUS.INACTIVE)
+ .filter((x) => x.organization_id === selectedOrganization?.id)
+ .sort((a, b) => a.name.localeCompare(b.name))
+
+ const [open, setOpen] = useState(false)
+
+ if (isLoadingProjects && alt) {
+ return <ShimmeringLoader className="w-[90px]" />
+ }
+
+ return IS_PLATFORM ? (
+ <div className="flex items-center space-x-2 px-2">
+ <Link href={`/project/${selectedProject?.ref}`}>
+ <a className="flex items-center space-x-2">
+ <p className="text-sm">{selectedProject?.name}</p>
+ {isSuccess && !isOrgBilling && <Badge color="slate">{subscription?.plan.name}</Badge>}
+ </a>
+ </Link>
+
+ <Popover_Shadcn_ open={open} onOpenChange={setOpen}>
+ <PopoverTrigger_Shadcn_ asChild>
+ <Button
+ type="text"
+ className="px-1"
+ icon={<IconCode className="text-scale-1100 rotate-90" strokeWidth={2} size={12} />}
+ />
+ </PopoverTrigger_Shadcn_>
+ <PopoverContent_Shadcn_ className="p-0" side="bottom" align="start">
+ <Command_Shadcn_>
+ <CommandInput_Shadcn_ placeholder="Find project..." />
+ <CommandList_Shadcn_>
+ <CommandEmpty_Shadcn_>No results found.</CommandEmpty_Shadcn_>
+ <CommandGroup_Shadcn_>
+ {projects?.map((project) => (
+ <ProjectLink
+ key={project.ref}
+ project={project}
+ organization={selectedOrganization}
+ setOpen={setOpen}
+ />
+ ))}
+ </CommandGroup_Shadcn_>
+ <CommandGroup_Shadcn_ className="border-t">
+ <CommandItem_Shadcn_ className="cursor-pointer" onSelect={() => setOpen(false)}>
+ <Link passHref href={`/new/${selectedOrganization?.slug}`}>
+ <a className="flex items-center space-x-2 w-full">
+ <IconPlus size={14} strokeWidth={1.5} />
+ <p>New project</p>
+ </a>
+ </Link>
+ </CommandItem_Shadcn_>
+ </CommandGroup_Shadcn_>
+ </CommandList_Shadcn_>
+ </Command_Shadcn_>
+ </PopoverContent_Shadcn_>
+ </Popover_Shadcn_>
+ </div>
+ ) : (
+ <Button type="text">
+ <span className="text-sm">{selectedProject?.name}</span>
+ </Button>
+ )
+}
+
+export default ProjectDropdown