Skip to content

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

feat: lazy load project detail

phamhieu17d3f1d48d3c
diff --git a/studio/stores/RootStore.ts b/studio/stores/RootStore.ts
index 6117098a77e..5331f208c45 100644
--- a/studio/stores/RootStore.ts
+++ b/studio/stores/RootStore.ts
@@ -1,4 +1,5 @@
import { configure, reaction } from 'mobx'
+import { Project } from 'types'
import AppStore, { IAppStore } from './app/AppStore'
import MetaStore, { IMetaStore } from './pgmeta/MetaStore'
import UiStore, { IUiStore } from './UiStore'
@@ -70,6 +71,14 @@ export class RootStore implements IRootStore {
setProjectRef(value?: string) {
if (this.ui.selectedProject?.ref === value) return
+ if (value) {
+ // fetch project detail when connectionString is not available
+ const found = this.app.projects.find((x: Project) => x.ref == value)
+ if (!found || !found.connectionString) {
+ this.app.projects.fetchDetail(value)
+ }
+ }
+
this.ui.setProjectRef(value)
this.functions.setProjectRef(value)
}
diff --git a/studio/stores/UiStore.ts b/studio/stores/UiStore.ts
index 312e19bc336..354eb335714 100644
--- a/studio/stores/UiStore.ts
+++ b/studio/stores/UiStore.ts
@@ -43,12 +43,19 @@ export default class UiStore implements IUiStore {
})
}
+ /**
+ * we use this getter to check for project ready.
+ * Only return selectedProject when it has full detail
+ * like connectionString prop
+ *
+ * @returns Project or undefined
+ */
get selectedProject() {
if (this.selectedProjectRef) {
const found = this.rootStore.app.projects.find(
(x: Project) => x.ref == this.selectedProjectRef
)
- return found
+ return !!found?.connectionString ? found : undefined
}
return undefined
}
diff --git a/studio/stores/app/AppStore.ts b/studio/stores/app/AppStore.ts
index 4146516ddc2..c631b88063d 100644
--- a/studio/stores/app/AppStore.ts
+++ b/studio/stores/app/AppStore.ts
@@ -6,10 +6,10 @@ import { API_URL } from 'lib/constants'
import { IRootStore } from '../RootStore'
import DatabaseStore, { IDatabaseStore } from './DatabaseStore'
import OrganizationStore from './OrganizationStore'
-import ProjectStore from './ProjectStore'
+import ProjectStore, { IProjectStore } from './ProjectStore'
export interface IAppStore {
- projects: ProjectStore
+ projects: IProjectStore
organizations: OrganizationStore
database: IDatabaseStore
onProjectUpdated: (project: any) => void
@@ -43,12 +43,11 @@ export default class AppStore implements IAppStore {
project.services?.length > 0
? project.services[0]?.infrastructure[0]?.app_versions?.version
: undefined
- const clone: any = cloneDeep((this.projects.data as any)[project.id])
+ const clone = cloneDeep(this.projects.data[project.id])
clone.kpsVersion = kpsVersion
clone.name = project.name
clone.status = project.status
- clone.services = project.services
- ;(this.projects.data as any)[project.id] = clone
+ this.projects.data[project.id] = clone
}
}
diff --git a/studio/stores/app/ProjectStore.ts b/studio/stores/app/ProjectStore.ts
index 0168c004e66..c8157869d43 100644
--- a/studio/stores/app/ProjectStore.ts
+++ b/studio/stores/app/ProjectStore.ts
@@ -1,6 +1,12 @@
-import { Project } from 'types'
+import { Project, ResponseError } from 'types'
import { IRootStore } from '../RootStore'
-import PostgresMetaInterface from '../common/PostgresMetaInterface'
+import { constructHeaders } from 'lib/api/apiHelpers'
+import { get } from 'lib/common/fetch'
+import PostgresMetaInterface, { IPostgresMetaInterface } from '../common/PostgresMetaInterface'
+
+export interface IProjectStore extends IPostgresMetaInterface<Project> {
+ fetchDetail: (projectRef: string) => void
+}
export default class ProjectStore extends PostgresMetaInterface<Project> {
constructor(
@@ -13,4 +19,14 @@ export default class ProjectStore extends PostgresMetaInterface<Project> {
) {
super(rootStore, dataUrl, headers, options)
}
+
+ async fetchDetail(projectRef: string) {
+ const url = `${this.url}/${projectRef}`
+ const headers = constructHeaders(this.headers)
+ const response = await get(url, { headers })
+ if (!response.error) {
+ const project = response as Project
+ this.data[project.id] = project
+ }
+ }
}
diff --git a/studio/types/base.ts b/studio/types/base.ts
index 79828047f8a..552c545d1fe 100644
--- a/studio/types/base.ts
+++ b/studio/types/base.ts
@@ -20,10 +20,13 @@ export interface Project {
organization_id: number
cloud_provider: string
region: string
- connectionString: string
inserted_at: string
subscription_id: string
+ // only available after projects.fetchDetail
+ kpsVersion?: string
+ connectionString?: string
+
// Possibly deprecated, just double check
subscription_tier?: string
subscription_tier_prod_id?: string