The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat: use new organizations and projects api to improve loading performance
phamhieud6861e04d7d5
diff --git a/studio/components/interfaces/App/RouteValidationWrapper.tsx b/studio/components/interfaces/App/RouteValidationWrapper.tsxindex ac97fd6a19e..5dbc8420ccc 100644--- a/studio/components/interfaces/App/RouteValidationWrapper.tsx+++ b/studio/components/interfaces/App/RouteValidationWrapper.tsx@@ -8,41 +8,42 @@ import { Organization, Project } from 'types'// Ideally these could all be within a _middleware when we use Next 12const RouteValidationWrapper: FC = ({ children }) => {const { ui, app } = useStore()- const userProfile = ui.profile+ const orgsInitialized = app.organizations.isInitialized+ const projectsInitialized = app.projects.isInitializedconst router = useRouter()const projectRef = router.query.refconst orgSlug = router.query.sluguseEffect(() => {- if (userProfile) {+ if (orgsInitialized && orgSlug) {// Check validity of organization that user is trying to access- if (orgSlug) {- const organizations = app.organizations.list()- const organizationSlugs = organizations.map((org: Organization) => org.slug)- const isValidOrg = organizationSlugs.includes(orgSlug as string)-- if (!isValidOrg) {- ui.setNotification({ category: 'error', message: 'This organization does not exist' })- router.push('/')- return- }+ const organizations = app.organizations.list()+ const organizationSlugs = organizations.map((org: Organization) => org.slug)+ const isValidOrg = organizationSlugs.includes(orgSlug as string)++ if (!isValidOrg) {+ ui.setNotification({ category: 'error', message: 'This organization does not exist' })+ router.push('/')+ return}+ }+ }, [orgsInitialized])+ useEffect(() => {+ if (projectsInitialized && projectRef) {// Check validity of project that the user is trying to access- if (projectRef) {- const projects = app.projects.list()- const projectRefs = projects.map((project: Project) => project.ref)- const isValidProject = projectRefs.includes(projectRef as string)-- if (!isValidProject) {- ui.setNotification({ category: 'error', message: 'This project does not exist' })- router.push('/')- return- }+ const projects = app.projects.list()+ const projectRefs = projects.map((project: Project) => project.ref)+ const isValidProject = projectRefs.includes(projectRef as string)++ if (!isValidProject) {+ ui.setNotification({ category: 'error', message: 'This project does not exist' })+ router.push('/')+ return}}- }, [userProfile])+ }, [projectsInitialized])return <>{children}</>}diff --git a/studio/hooks/misc/withAuth.tsx b/studio/hooks/misc/withAuth.tsxindex 680c4c34cb1..1ebbe3ef816 100644--- a/studio/hooks/misc/withAuth.tsx+++ b/studio/hooks/misc/withAuth.tsx@@ -42,11 +42,14 @@ export function withAuth(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)+ ui.setProfile(profile)++ if (!app.organizations.isInitialized) {+ app.organizations.load()+ }+ if (!app.projects.isInitialized) {+ app.projects.load()+ }}}diff --git a/studio/pages/new/[slug].tsx b/studio/pages/new/[slug].tsxindex 56a6e08074c..bc0ed031068 100644--- a/studio/pages/new/[slug].tsx+++ b/studio/pages/new/[slug].tsx@@ -80,7 +80,8 @@ export const Wizard = observer(() => {const totalFreeProjects = subscriptionStats.total_free_projectsconst freeProjectsLimit = ui.profile?.free_project_limit ?? DEFAULT_FREE_PROJECTS_LIMIT- const isEmptyOrganizations = organizations.length <= 0+ const isOrganizationOwner = currentOrg?.is_owner || !app.organizations.isInitialized+ const isEmptyOrganizations = organizations.length <= 0 && app.organizations.isInitializedconst isEmptyPaymentMethod = stripeCustomer? stripeCustomer.paymentMethods?.data?.length <= 0: undefined@@ -191,6 +192,7 @@ export const Wizard = observer(() => {<WizardLayout organization={currentOrg} project={null}><PanelhideHeaderStyling+ isLoading={app.organizations.isInitialized}title={<div key="panel-title"><Typography.Title level={4} className="mb-0">@@ -218,126 +220,127 @@ export const Wizard = observer(() => {</div>}>- <>- <Panel.Content className="pt-0 pb-6">- <Typography.Text>- Your project will have its own dedicated instance and full postgres database.- <br />- An API will be set up so you can easily interact with your new database.- <br />- </Typography.Text>- </Panel.Content>- <Panel.Content className="Form section-block--body has-inputs-centered border-b border-t border-panel-border-interior-light dark:border-panel-border-interior-dark space-y-4">+ <Panel.Content className="pt-0 pb-6">+ <Typography.Text>+ Your project will have its own dedicated instance and full postgres database.+ <br />+ An API will be set up so you can easily interact with your new database.+ <br />+ </Typography.Text>+ </Panel.Content>+ <Panel.Content className="Form section-block--body has-inputs-centered border-b border-t border-panel-border-interior-light dark:border-panel-border-interior-dark space-y-4">+ {/* Listbox will throw error on empty list box item. We need to add a check */}+ {organizations.length > 0 && (<Listboxlabel="Organization"layout="horizontal"value={currentOrg?.slug}onChange={(slug) => router.push(`/new/${slug}`)}>- {organizations.map((x: any) => (+ {organizations?.map((x: any) => (<Listbox.Option- key={x.id}- label={x.name}- value={x.slug}+ key={x?.id}+ label={x?.name}+ value={x?.slug}addOnBefore={() => <IconUsers />}>- {x.name}+ {x?.name}</Listbox.Option>))}</Listbox>+ )}- {!currentOrg?.is_owner && <NotOrganizationOwnerWarning />}- </Panel.Content>+ {!isOrganizationOwner && <NotOrganizationOwnerWarning />}+ </Panel.Content>- {canCreateProject && (- <>- <Panel.Content className="Form section-block--body has-inputs-centered border-b border-t border-panel-border-interior-light dark:border-panel-border-interior-dark">- <FormField- // @ts-ignore- label="Name"- type="text"- placeholder="Project name"- value={projectName}- onChange={onProjectNameChange}- autoFocus- />- </Panel.Content>-- <Panel.Content className="Form section-block--body has-inputs-centered border-b border-panel-border-interior-light dark:border-panel-border-interior-dark">- <FormField- // @ts-ignore- label="Database Password"- type="password"- placeholder="Type in a strong password"- value={dbPass}- onChange={onDbPassChange}- description={- <PasswordStrengthBar- passwordStrengthScore={passwordStrengthScore}- password={dbPass}- passwordStrengthMessage={passwordStrengthMessage}- />- }- errorMessage={passwordStrengthWarning}- />- </Panel.Content>-- <Panel.Content className="Form section-block--body has-inputs-centered border-b border-panel-border-interior-light dark:border-panel-border-interior-dark">- <FormField- // @ts-ignore- label="Region"- type="select"- choices={REGIONS}- value={dbRegion}- onChange={onDbRegionChange}- description="Select a region close to you for the best performance."- />- </Panel.Content>- </>- )}+ {canCreateProject && (+ <>+ <Panel.Content className="Form section-block--body has-inputs-centered border-b border-t border-panel-border-interior-light dark:border-panel-border-interior-dark">+ <FormField+ // @ts-ignore+ label="Name"+ type="text"+ placeholder="Project name"+ value={projectName}+ onChange={onProjectNameChange}+ autoFocus+ />+ </Panel.Content>- {currentOrg?.is_owner && (- <Panel.Content className="Form section-block--body has-inputs-centered ">- <Listbox- label="Pricing Plan"- layout="horizontal"- value={dbPricingPlan}- onChange={onDbPricingPlanChange}+ <Panel.Content className="Form section-block--body has-inputs-centered border-b border-panel-border-interior-light dark:border-panel-border-interior-dark">+ <FormField// @ts-ignore- descriptionText={- <>- Select a plan that suits your needs. - <a className="underline" target="_blank" href="https://supabase.com/pricing">- More details- </a>- </>+ label="Database Password"+ type="password"+ placeholder="Type in a strong password"+ value={dbPass}+ onChange={onDbPassChange}+ description={+ <PasswordStrengthBar+ passwordStrengthScore={passwordStrengthScore}+ password={dbPass}+ passwordStrengthMessage={passwordStrengthMessage}+ />}- >- {Object.entries(PRICING_PLANS).map(([k, v]) => (- <Listbox.Option key={k} label={v} value={v}>- {`${v}${v === PRICING_PLANS.PRO ? ' - $25/month' : ''}`}- </Listbox.Option>- ))}- </Listbox>-- {isSelectFreeTier && isOverFreeProjectLimit && (- <FreeProjectLimitWarning limit={freeProjectsLimit} />- )}- {!isSelectFreeTier && isEmptyPaymentMethod && (- <EmptyPaymentMethodWarning stripeCustomerId={stripeCustomerId} />- )}+ errorMessage={passwordStrengthWarning}+ /></Panel.Content>- )}- {subscriptionStats.isLoading && (- <Panel.Content>- <div className="py-10">- {/* @ts-ignore */}- <Loading active={true} />- </div>+ <Panel.Content className="Form section-block--body has-inputs-centered border-b border-panel-border-interior-light dark:border-panel-border-interior-dark">+ <FormField+ // @ts-ignore+ label="Region"+ type="select"+ choices={REGIONS}+ value={dbRegion}+ onChange={onDbRegionChange}+ description="Select a region close to you for the best performance."+ /></Panel.Content>- )}- </>+ </>+ )}++ {currentOrg?.is_owner && (+ <Panel.Content className="Form section-block--body has-inputs-centered ">+ <Listbox+ label="Pricing Plan"+ layout="horizontal"+ value={dbPricingPlan}+ onChange={onDbPricingPlanChange}+ // @ts-ignore+ descriptionText={+ <>+ Select a plan that suits your needs. + <a className="underline" target="_blank" href="https://supabase.com/pricing">+ More details+ </a>+ </>+ }+ >+ {Object.entries(PRICING_PLANS).map(([k, v]) => (+ <Listbox.Option key={k} label={v} value={v}>+ {`${v}${v === PRICING_PLANS.PRO ? ' - $25/month' : ''}`}+ </Listbox.Option>+ ))}+ </Listbox>++ {isSelectFreeTier && isOverFreeProjectLimit && (+ <FreeProjectLimitWarning limit={freeProjectsLimit} />+ )}+ {!isSelectFreeTier && isEmptyPaymentMethod && (+ <EmptyPaymentMethodWarning stripeCustomerId={stripeCustomerId} />+ )}+ </Panel.Content>+ )}++ {subscriptionStats.isLoading && (+ <Panel.Content>+ <div className="py-10">+ {/* @ts-ignore */}+ <Loading active={true} />+ </div>+ </Panel.Content>+ )}</Panel></WizardLayout>)diff --git a/studio/stores/common/PostgresMetaInterface.ts b/studio/stores/common/PostgresMetaInterface.tsindex 3314521b081..343824ad984 100644--- a/studio/stores/common/PostgresMetaInterface.ts+++ b/studio/stores/common/PostgresMetaInterface.ts@@ -87,7 +87,7 @@ export default class PostgresMetaInterface<T> implements IPostgresMetaInterface<}get isInitialized() {- return this.state !== this.STATES.INITIAL+ return this.state === this.STATES.LOADED || this.state === this.STATES.ERROR}async fetchData() {