Skip to content

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

Add redirect on /projects if nav layout v2 is on

Joshen Limf1d1249a4f46
diff --git a/studio/components/interfaces/App/RouteValidationWrapper.tsx b/studio/components/interfaces/App/RouteValidationWrapper.tsx
index 9b43d4c5a8d..182de527f6b 100644
--- a/studio/components/interfaces/App/RouteValidationWrapper.tsx
+++ b/studio/components/interfaces/App/RouteValidationWrapper.tsx
@@ -4,9 +4,10 @@ import { PropsWithChildren, useEffect } from 'react'
import { useOrganizationsQuery } from 'data/organizations/organizations-query'
import { useProjectsQuery } from 'data/projects/projects-query'
-import { useStore } from 'hooks'
+import { useFlag, useStore } from 'hooks'
import useLatest from 'hooks/misc/useLatest'
import { useParams } from 'common'
+import { LOCAL_STORAGE_KEYS } from 'lib/constants'
// Ideally these could all be within a _middleware when we use Next 12
const RouteValidationWrapper = ({ children }: PropsWithChildren<{}>) => {
@@ -80,7 +81,9 @@ const RouteValidationWrapper = ({ children }: PropsWithChildren<{}>) => {
// Save organization slug to local storage
const organizations = organizationsRef.current ?? []
const organization = organizations.find((org) => org.slug === slug)
- if (organization) localStorage.setItem('supabase-organization', organization.slug)
+ if (organization) {
+ localStorage.setItem(LOCAL_STORAGE_KEYS.RECENTLY_VISITED_ORGANIZATION, organization.slug)
+ }
}
}, [slug, orgsInitialized])
@@ -91,7 +94,9 @@ const RouteValidationWrapper = ({ children }: PropsWithChildren<{}>) => {
const project = projects.find((project) => project.ref === ref)
const organizationId = project?.organization_id
const organization = organizations?.find((organization) => organization.id === organizationId)
- if (organization) localStorage.setItem('supabase-organization', organization.slug)
+ if (organization) {
+ localStorage.setItem(LOCAL_STORAGE_KEYS.RECENTLY_VISITED_ORGANIZATION, organization.slug)
+ }
}
}, [ref, projectsInitialized])
diff --git a/studio/hooks/misc/useSelectedOrganization.ts b/studio/hooks/misc/useSelectedOrganization.ts
index 282e9daf081..e10acce43e6 100644
--- a/studio/hooks/misc/useSelectedOrganization.ts
+++ b/studio/hooks/misc/useSelectedOrganization.ts
@@ -3,13 +3,16 @@ import { useOrganizationsQuery } from 'data/organizations/organizations-query'
import { useMemo } from 'react'
import { useSelectedProject } from './useSelectedProject'
+import { LOCAL_STORAGE_KEYS } from 'lib/constants'
export function useSelectedOrganization({ enabled = true } = {}) {
const { slug } = useParams()
const { data } = useOrganizationsQuery({ enabled })
const selectedProject = useSelectedProject()
const localStorageSlug =
- typeof window !== 'undefined' ? localStorage.getItem('supabase-organization') : undefined
+ typeof window !== 'undefined'
+ ? localStorage.getItem(LOCAL_STORAGE_KEYS.RECENTLY_VISITED_ORGANIZATION)
+ : undefined
return useMemo(() => {
return data?.find((org) => {
diff --git a/studio/lib/constants/index.ts b/studio/lib/constants/index.ts
index 24743c4904f..3e3b66f24b2 100644
--- a/studio/lib/constants/index.ts
+++ b/studio/lib/constants/index.ts
@@ -85,3 +85,7 @@ export const STRIPE_PUBLIC_KEY =
process.env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY || 'pk_test_XVwg5IZH3I9Gti98hZw6KRzd00v5858heG'
export const USAGE_APPROACHING_THRESHOLD = 0.75
+
+export const LOCAL_STORAGE_KEYS = {
+ RECENTLY_VISITED_ORGANIZATION: 'supabase-organization',
+}
diff --git a/studio/pages/projects.tsx b/studio/pages/projects.tsx
index 8fe0df5780d..e36beadb3d8 100644
--- a/studio/pages/projects.tsx
+++ b/studio/pages/projects.tsx
@@ -5,11 +5,15 @@ import AlertError from 'components/ui/AlertError'
import Connecting from 'components/ui/Loading/Loading'
import { useOrganizationsQuery } from 'data/organizations/organizations-query'
import { useAutoProjectsPrefetch } from 'data/projects/projects-query'
-import { IS_PLATFORM } from 'lib/constants'
+import { useFlag } from 'hooks'
+import { IS_PLATFORM, LOCAL_STORAGE_KEYS } from 'lib/constants'
import { useProfile } from 'lib/profile'
+import { useRouter } from 'next/router'
+import { useEffect } from 'react'
import { NextPageWithLayout } from 'types'
const ProjectsPage: NextPageWithLayout = () => {
+ const router = useRouter()
const {
data: organizations,
isLoading: isOrganizationLoading,
@@ -19,8 +23,19 @@ const ProjectsPage: NextPageWithLayout = () => {
useAutoProjectsPrefetch()
const { isLoading: isProfileLoading } = useProfile()
-
const isLoading = isOrganizationLoading || isProfileLoading
+ const navLayoutV2 = useFlag('navigationLayoutV2')
+
+ useEffect(() => {
+ if (navLayoutV2 && isSuccess && typeof window !== 'undefined') {
+ const localStorageSlug =
+ typeof window !== 'undefined'
+ ? localStorage.getItem(LOCAL_STORAGE_KEYS.RECENTLY_VISITED_ORGANIZATION)
+ : undefined
+ if (localStorageSlug !== undefined) router.push(`/org/${localStorageSlug}`)
+ else router.push(`/org/${organizations[0].slug}`)
+ }
+ }, [navLayoutV2, isSuccess, typeof window])
return (
<>