Skip to content

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

fix: project layouts to support metaStore with lazy projects data

phamhieu8f558184b463
diff --git a/studio/components/layouts/AuthLayout/AuthLayout.tsx b/studio/components/layouts/AuthLayout/AuthLayout.tsx
index 3feb7ba12a2..dfde160c31b 100644
--- a/studio/components/layouts/AuthLayout/AuthLayout.tsx
+++ b/studio/components/layouts/AuthLayout/AuthLayout.tsx
@@ -24,8 +24,10 @@ const AuthLayout: FC<Props> = ({ title, children }) => {
const [loaded, setLoaded] = useState<boolean>(isInitialized)
useEffect(() => {
- meta.tables.load()
- }, [])
+ if (meta.ready) {
+ meta.tables.load()
+ }
+ }, [meta.ready])
useEffect(() => {
if (!isLoading && !loaded) {
diff --git a/studio/components/layouts/DatabaseLayout/DatabaseLayout.tsx b/studio/components/layouts/DatabaseLayout/DatabaseLayout.tsx
index 50f018fc642..df81b0d5dec 100644
--- a/studio/components/layouts/DatabaseLayout/DatabaseLayout.tsx
+++ b/studio/components/layouts/DatabaseLayout/DatabaseLayout.tsx
@@ -24,15 +24,17 @@ const DatabaseLayout: FC<Props> = ({ title, children }) => {
const [loaded, setLoaded] = useState<boolean>(isInitialized)
useEffect(() => {
- // Eventually should only load the required stores based on the pages
- meta.schemas.load()
- meta.tables.load()
+ if (meta.ready) {
+ // Eventually should only load the required stores based on the pages
+ meta.schemas.load()
+ meta.tables.load()
- meta.roles.load()
- meta.triggers.load()
- meta.extensions.load()
- meta.publications.load()
- }, [])
+ meta.roles.load()
+ meta.triggers.load()
+ meta.extensions.load()
+ meta.publications.load()
+ }
+ }, [meta.ready])
useEffect(() => {
if (!isLoading && !loaded) {
diff --git a/studio/components/layouts/DocsLayout/DocsLayout.tsx b/studio/components/layouts/DocsLayout/DocsLayout.tsx
index ab09e77ad7c..780080a6ec9 100644
--- a/studio/components/layouts/DocsLayout/DocsLayout.tsx
+++ b/studio/components/layouts/DocsLayout/DocsLayout.tsx
@@ -12,8 +12,10 @@ function DocsLayout({ title, children }: { title: string; children: ReactElement
const { data, isLoading, error } = meta.openApi
useEffect(() => {
- meta.openApi.load()
- }, [])
+ if (meta.ready) {
+ meta.openApi.load()
+ }
+ }, [meta.ready])
if (error) {
return (
diff --git a/studio/components/layouts/ProjectLayout/ProjectLayout.tsx b/studio/components/layouts/ProjectLayout/ProjectLayout.tsx
index d232be0baf3..673cada9ac5 100644
--- a/studio/components/layouts/ProjectLayout/ProjectLayout.tsx
+++ b/studio/components/layouts/ProjectLayout/ProjectLayout.tsx
@@ -32,34 +32,6 @@ const ProjectLayout: FC<Props> = ({
hideHeader = false,
hideIconBar = false,
}) => {
- const { ui } = useStore()
- const project: Project | undefined = ui.selectedProject
-
- const router = useRouter()
- const requiresDbConnection: boolean = router.pathname !== '/project/[ref]/settings/general'
-
- // useEffect(() => {
- // if (isUndefined(project)) router.push('/404')
- // }, [])
-
- const renderContent = () => {
- if (!isUndefined(project)) {
- return requiresDbConnection ? (
- <TestConnection project={project}>
- {isLoading ? (
- <Connecting />
- ) : (
- <div className="flex flex-col flex-1 overflow-y-auto">{children}</div>
- )}
- </TestConnection>
- ) : (
- <>{children}</>
- )
- } else {
- return <>{children}</>
- }
- }
-
return (
<>
<Head>
@@ -72,16 +44,16 @@ const ProjectLayout: FC<Props> = ({
{!hideIconBar && <NavigationBar />}
{/* Product menu bar */}
- {productMenu && !isLoading && (
+ <MenuBarWrapper isLoading={isLoading} productMenu={productMenu}>
<ProductMenuBar title={product}>{productMenu}</ProductMenuBar>
- )}
+ </MenuBarWrapper>
<main
style={{ maxHeight: '100vh' }}
className="w-full flex flex-col flex-1 overflow-x-hidden"
>
{!hideHeader && <LayoutHeader />}
- {renderContent()}
+ <ContentWrapper isLoading={isLoading}>{children}</ContentWrapper>
</main>
</div>
</>
@@ -89,3 +61,48 @@ const ProjectLayout: FC<Props> = ({
}
export default observer(ProjectLayout)
+
+interface MenuBarWrapperProps {
+ isLoading: boolean
+ productMenu?: ReactNode
+}
+
+const MenuBarWrapper: FC<MenuBarWrapperProps> = observer(({ isLoading, productMenu, children }) => {
+ const { ui, meta } = useStore()
+ const project = ui.selectedProject
+ return <>{!isLoading && productMenu && project != undefined && meta.ready ? children : null}</>
+})
+
+interface ContentWrapperProps {
+ isLoading: boolean
+}
+
+const ContentWrapper: FC<ContentWrapperProps> = observer(({ isLoading, children }) => {
+ const { ui, meta, content } = useStore()
+ const router = useRouter()
+ const project = ui.selectedProject
+ const requiresDbConnection: boolean = router.pathname !== '/project/[ref]/settings/general'
+
+ console.log(
+ 'ProjectLayout: ',
+ project,
+ ' meta.ready:',
+ meta.ready,
+ ' content.ready:',
+ content.ready
+ )
+
+ return (
+ <>
+ {isLoading || project == undefined || !meta.ready ? (
+ <Connecting />
+ ) : requiresDbConnection ? (
+ <TestConnection project={project!}>
+ <div className="flex flex-col flex-1 overflow-y-auto">{children}</div>
+ </TestConnection>
+ ) : (
+ <>{children}</>
+ )}
+ </>
+ )
+})
diff --git a/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx b/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx
index 3ea80225b6c..729a0276ce2 100644
--- a/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx
+++ b/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx
@@ -33,9 +33,11 @@ const TableEditorLayout: FC<Props> = ({
const [loaded, setLoaded] = useState<boolean>(isInitialized)
useEffect(() => {
- meta.schemas.load()
- meta.tables.load()
- }, [])
+ if (meta.ready) {
+ meta.schemas.load()
+ meta.tables.load()
+ }
+ }, [meta.ready])
useEffect(() => {
let cancel = false
diff --git a/studio/pages/project/[ref]/index.tsx b/studio/pages/project/[ref]/index.tsx
index 076d15751ff..ab16f05bfb9 100644
--- a/studio/pages/project/[ref]/index.tsx
+++ b/studio/pages/project/[ref]/index.tsx
@@ -15,50 +15,24 @@ import {
NewProjectPanel,
} from 'components/interfaces/Home'
import { CLIENT_LIBRARIES, EXAMPLE_PROJECTS } from 'components/interfaces/Home/Home.constants'
+import { FC } from 'react'
const Home: NextPage = () => {
const { ui } = useStore()
const project = ui.selectedProject
const projectName =
- project?.ref !== 'default'
- ? project?.name ?? 'Welcome to your project'
+ project?.ref !== 'default' && project?.name != undefined
+ ? project?.name
: 'Welcome to your project'
- const { data: usage, error: usageError }: any = useSWR(
- `${API_URL}/projects/${project?.ref}/usage`,
- get
- )
-
- if (usageError) {
- return <Typography.Text type="danger">Error loading data {usageError.message}</Typography.Text>
- }
-
- const hasProjectData =
- usage && (usage?.bucketSize || (usage?.authUsers ?? '0') !== '0' || usage?.dbTables)
- ? true
- : false
-
return (
<BaseLayout>
<div className="max-w-7xl mx-auto w-full my-16 space-y-16">
<div className="mx-6 flex space-x-6 items-center">
<h1 className="text-3xl">{projectName}</h1>
</div>
- {IS_PLATFORM && project && (
- <>
- {isUndefined(usage) ? (
- <div className="w-full flex justify-center items-center space-x-2">
- <IconLoader className="animate-spin" size={14} />
- <p className="text-sm">Retrieving project usage statistics</p>
- </div>
- ) : !usage.error && hasProjectData ? (
- <ProjectUsage project={project} />
- ) : (
- <NewProjectPanel />
- )}
- </>
- )}
+ <ProjectUsageSection />
<div className="space-y-8">
<div className="mx-6">
<Typography.Title level={4}>Client libraries</Typography.Title>
@@ -85,3 +59,37 @@ const Home: NextPage = () => {
}
export default withAuth(observer(Home))
+
+const ProjectUsageSection: FC = observer(({}) => {
+ const { ui } = useStore()
+
+ const project = ui.selectedProject
+ const { data: usage, error: usageError }: any = useSWR(
+ `${API_URL}/projects/${project?.ref}/usage`,
+ get
+ )
+
+ if (usageError) {
+ return <Typography.Text type="danger">Error loading data {usageError.message}</Typography.Text>
+ }
+
+ const hasProjectData =
+ usage && (usage?.bucketSize || (usage?.authUsers ?? '0') !== '0' || usage?.dbTables)
+ ? true
+ : false
+
+ return IS_PLATFORM ? (
+ <>
+ {isUndefined(usage) ? (
+ <div className="w-full flex justify-center items-center space-x-2">
+ <IconLoader className="animate-spin" size={14} />
+ <p className="text-sm">Retrieving project usage statistics</p>
+ </div>
+ ) : !usage.error && hasProjectData ? (
+ <ProjectUsage project={project} />
+ ) : (
+ <NewProjectPanel />
+ )}
+ </>
+ ) : null
+})
diff --git a/studio/stores/RootStore.ts b/studio/stores/RootStore.ts
index 4bd4eb7bf5c..f9d2b83e1f3 100644
--- a/studio/stores/RootStore.ts
+++ b/studio/stores/RootStore.ts
@@ -1,4 +1,4 @@
-import { configure } from 'mobx'
+import { configure, reaction } from 'mobx'
import AppStore, { IAppStore } from './app/AppStore'
import MetaStore, { IMetaStore } from './pgmeta/MetaStore'
import UiStore, { IUiStore } from './UiStore'
@@ -32,17 +32,31 @@ export class RootStore implements IRootStore {
connectionString: '',
})
this.app = new AppStore(this)
+
+ reaction(
+ () => this.ui.selectedProject,
+ (selectedProject) => {
+ if (selectedProject) {
+ this.content = new ProjectContentStore(this, { projectRef: selectedProject.ref })
+ this.meta = new MetaStore(this, {
+ projectRef: selectedProject.ref,
+ connectionString: selectedProject.connectionString ?? '',
+ })
+ } else {
+ this.content = new ProjectContentStore(this, { projectRef: '' })
+ this.meta = new MetaStore(this, {
+ projectRef: '',
+ connectionString: '',
+ })
+ }
+ console.log('selectedProject changed: ', selectedProject)
+ }
+ )
}
setProjectRef(value?: string) {
- if (this.ui.selectedProject?.ref != value) {
- this.ui.setProjectRef(value)
- this.content = new ProjectContentStore(this, { projectRef: value || '' })
- this.meta = new MetaStore(this, {
- projectRef: value || '',
- connectionString: this.ui.selectedProject?.connectionString ?? '',
- })
- }
+ if (this.ui.selectedProject?.ref == value) return
+ this.ui.setProjectRef(value)
}
setOrganizationSlug(value?: string) {
diff --git a/studio/stores/content/ProjectContentStore.ts b/studio/stores/content/ProjectContentStore.ts
index 2952aaa42a0..ec05327bc2b 100644
--- a/studio/stores/content/ProjectContentStore.ts
+++ b/studio/stores/content/ProjectContentStore.ts
@@ -10,6 +10,7 @@ import { API_URL } from 'lib/constants'
// but use the one under the stores folder projectContentStore first while we transition
export interface IProjectContentStore {
+ ready: boolean
isLoading: boolean
isInitialized: boolean
error: any
@@ -21,6 +22,7 @@ export interface IProjectContentStore {
}
export default class ProjectContentStore implements IProjectContentStore {
+ ready: boolean
rootStore: IRootStore
STATES = {
@@ -38,7 +40,7 @@ export default class ProjectContentStore implements IProjectContentStore {
constructor(rootStore: IRootStore, options: { projectRef: string }) {
const { projectRef } = options
-
+ this.ready = projectRef !== ''
this.rootStore = rootStore
this.baseUrl = `${API_URL}/projects/${projectRef}/content`
makeAutoObservable(this)
diff --git a/studio/stores/pgmeta/MetaStore.ts b/studio/stores/pgmeta/MetaStore.ts
index c2954314903..0537bb4538d 100644
--- a/studio/stores/pgmeta/MetaStore.ts
+++ b/studio/stores/pgmeta/MetaStore.ts
@@ -43,6 +43,7 @@ const BATCH_SIZE = 1000
const CHUNK_SIZE = 1024 * 1024 * 0.25 // 0.25MB
export interface IMetaStore {
+ ready: boolean
excludedSchemas: string[]
openApi: IOpenApiStore
@@ -114,6 +115,7 @@ export default class MetaStore implements IMetaStore {
extensions: ExtensionsStore
publications: PublicationStore
+ ready: boolean
connectionString?: string
baseUrl: string
queryBaseUrl: string
@@ -131,6 +133,7 @@ export default class MetaStore implements IMetaStore {
constructor(rootStore: IRootStore, options: { projectRef: string; connectionString: string }) {
const { projectRef, connectionString } = options
+ this.ready = projectRef !== ''
this.rootStore = rootStore
this.baseUrl = `/api/pg-meta/${projectRef}`
this.queryBaseUrl = `${API_URL}/pg-meta/${projectRef}`