Skip to content

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

Port over changes on updates to calling the profile endpoint

Joshen Limd94a77d7ea5a
diff --git a/studio/components/layouts/SQLEditorLayout/SQLEditorMenuOld.tsx b/studio/components/layouts/SQLEditorLayout/SQLEditorMenuOld.tsx
index 0a245a0fbdc..f5fe467d903 100644
--- a/studio/components/layouts/SQLEditorLayout/SQLEditorMenuOld.tsx
+++ b/studio/components/layouts/SQLEditorLayout/SQLEditorMenuOld.tsx
@@ -19,7 +19,7 @@ import {
} from '@supabase/ui'
import { IS_PLATFORM } from 'lib/constants'
-import { useProfile } from 'hooks'
+import { useStore } from 'hooks'
import { useProjectContentStore } from 'stores/projectContentStore'
import { useSqlStore, TAB_TYPES } from 'localStores/sqlEditor/SqlEditorStore'
@@ -110,7 +110,9 @@ const SideBarContent = observer(() => {
const router = useRouter()
const { ref } = router.query
- const { profile: user } = useProfile()
+ const { ui } = useStore()
+ const { profile: user } = ui
+
const sqlEditorStore: any = useSqlStore()
const contentStore: any = useProjectContentStore(ref)
@@ -125,7 +127,7 @@ const SideBarContent = observer(() => {
const snippet = await createSqlSnippet({ router })
// reload the local sqlEditorStore
- await sqlEditorStore.loadRemotePersistentData(contentStore, user.id)
+ await sqlEditorStore.loadRemotePersistentData(contentStore, user?.id)
// select tab with new snippet
if (snippet) sqlEditorStore.selectTab(snippet.id)
diff --git a/studio/components/to-be-cleaned/SqlEditor/TabSqlQuery.js b/studio/components/to-be-cleaned/SqlEditor/TabSqlQuery.js
index 4b8ec67ef33..60159b9cc25 100644
--- a/studio/components/to-be-cleaned/SqlEditor/TabSqlQuery.js
+++ b/studio/components/to-be-cleaned/SqlEditor/TabSqlQuery.js
@@ -23,7 +23,7 @@ import toast from 'react-hot-toast'
import { SQL_SNIPPET_SCHEMA_VERSION } from './SqlEditor.constants'
import Telemetry from 'lib/telemetry'
-import { useProfile, useStore } from 'hooks'
+import { useStore } from 'hooks'
import { debounce } from 'lodash'
import { useWindowDimensions, useKeyboardShortcuts } from 'hooks'
@@ -355,12 +355,14 @@ const FavoriteButton = observer(() => {
const router = useRouter()
const { ref } = router.query
- const { profile: user } = useProfile()
- const [loading, setLoading] = useState(false)
+ const { ui } = useStore()
+ const { profile: user } = ui
const sqlEditorStore = useSqlStore()
const contentStore = useProjectContentStore(ref)
+ const [loading, setLoading] = useState(false)
+
const id = sqlEditorStore.activeTab.id
/*
diff --git a/studio/components/to-be-cleaned/SqlEditor/TabWelcome.js b/studio/components/to-be-cleaned/SqlEditor/TabWelcome.js
index 4c88b56544d..d9725650dfd 100644
--- a/studio/components/to-be-cleaned/SqlEditor/TabWelcome.js
+++ b/studio/components/to-be-cleaned/SqlEditor/TabWelcome.js
@@ -8,7 +8,7 @@ import { useSqlStore } from 'localStores/sqlEditor/SqlEditorStore'
import { useProjectContentStore } from 'stores/projectContentStore'
import Telemetry from 'lib/telemetry'
-import { useProfile } from 'hooks'
+import { useStore } from 'hooks'
import { partition } from 'lodash'
import { createSqlSnippet } from './SqlEditor.utils'
@@ -18,7 +18,9 @@ const TabWelcome = observer(() => {
const router = useRouter()
const { ref } = router.query
- const { profile: user } = useProfile()
+ const { ui } = useStore()
+ const { profile: user } = ui
+
const sqlEditorStore = useSqlStore()
const contentStore = useProjectContentStore(ref)
diff --git a/studio/hooks/misc/withAuth.tsx b/studio/hooks/misc/withAuth.tsx
index 13de197ee24..5869a76a52e 100644
--- a/studio/hooks/misc/withAuth.tsx
+++ b/studio/hooks/misc/withAuth.tsx
@@ -23,9 +23,13 @@ export function withAuth(
const rootStore = useStore()
const [isConnecting, setConnecting] = useState(true)
- const { profile, isLoading, isError } = useProfile()
const { ref, slug } = router.query
const { app, ui } = rootStore
+
+ const returning =
+ app.projects.isInitialized && app.organizations.isInitialized ? 'minimal' : undefined
+ const { profile, isLoading } = useProfile(returning)
+
const isRedirecting = checkRedirectTo(isLoading, router, profile, redirectTo, redirectIfFound)
// We might probably be able to bring this into a _middleware with next 12
@@ -40,14 +44,14 @@ export function withAuth(
useEffect(() => {
// this should run before redirecting
if (!isLoading) {
- if (!isError && profile) {
+ if (!profile) {
+ ui.setProfile(undefined)
+ } else if (returning !== 'minimal') {
const { organizations, ...userProfile } = profile
const projects: Project[] = flatten(organizations?.map((org: any) => org.projects))
app.organizations.initialDataArray(organizations)
app.projects.initialDataArray(projects)
ui.setProfile(userProfile)
- } else {
- ui.setProfile(undefined)
}
}
@@ -55,7 +59,7 @@ export function withAuth(
if (isRedirecting) {
router.push(redirectTo)
}
- }, [isLoading, isRedirecting, isError, profile])
+ }, [isLoading, isRedirecting, profile])
useEffect(() => {
if (!isLoading && router.isReady) {
diff --git a/studio/hooks/queries/useProfile.ts b/studio/hooks/queries/useProfile.ts
index 8a06951a8a2..941e1dfe6ce 100644
--- a/studio/hooks/queries/useProfile.ts
+++ b/studio/hooks/queries/useProfile.ts
@@ -3,8 +3,14 @@ import { get } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import { User } from 'types'
-export function useProfile() {
- const url = `${API_URL}/profile`
+export function useProfile(returning?: 'minimal') {
+ let url = `${API_URL}/profile`
+
+ if (returning) {
+ const query = new URLSearchParams({ returning }).toString()
+ url = `${url}?${query}`
+ }
+
const { data, error } = useSWR<any>(url, get, {
loadingTimeout: 10000,
})
diff --git a/studio/pages/project/[ref]/sql/index.tsx b/studio/pages/project/[ref]/sql/index.tsx
index f26aeebd5f6..a1c7c2ad9d9 100644
--- a/studio/pages/project/[ref]/sql/index.tsx
+++ b/studio/pages/project/[ref]/sql/index.tsx
@@ -16,15 +16,16 @@ import getPgsqlSignatureHelpProvider from 'components/to-be-cleaned/SqlEditor/Pg
import { useProjectContentStore } from 'stores/projectContentStore'
-import { useStore, withAuth, useProfile } from 'hooks'
+import { useStore, withAuth } from 'hooks'
import { SQLEditorLayout } from 'components/layouts'
const PageConfig = () => {
const router = useRouter()
const { ref } = router.query
- const { meta } = useStore()
- const { profile: user } = useProfile()
+ const { meta, ui } = useStore()
+ const { profile: user } = ui
+
const contentStore: any = useProjectContentStore(ref)
const sqlEditorStore: any = useSqlEditorStore(ref, meta)
diff --git a/studio/stores/common/PostgresMetaInterface.ts b/studio/stores/common/PostgresMetaInterface.ts
index 8178249dbb1..f2be74940b7 100644
--- a/studio/stores/common/PostgresMetaInterface.ts
+++ b/studio/stores/common/PostgresMetaInterface.ts
@@ -12,6 +12,7 @@ export interface IPostgresMetaInterface<T> {
count: number
hasError: boolean
isLoading: boolean
+ isInitialized: boolean
load: () => void
create: (payload: any) => Promise<T | { error: ResponseError }>
@@ -67,6 +68,10 @@ export default class PostgresMetaInterface<T> implements IPostgresMetaInterface<
})
}
+ get count() {
+ return Object.keys(this.data).length
+ }
+
get hasError() {
return this.state === this.STATES.ERROR
}
@@ -75,8 +80,8 @@ export default class PostgresMetaInterface<T> implements IPostgresMetaInterface<
return this.state === this.STATES.INITIAL || this.state === this.STATES.LOADING
}
- get count() {
- return Object.keys(this.data).length
+ get isInitialized() {
+ return this.state !== this.STATES.INITIAL
}
async fetchData() {