Skip to content

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

fix: TestConnection component

phamhieu1576f6161839
diff --git a/studio/components/layouts/ProjectLayout/TestConnection.tsx b/studio/components/layouts/ProjectLayout/TestConnection.tsx
index f4f3e3aeaa4..a3c191d24be 100644
--- a/studio/components/layouts/ProjectLayout/TestConnection.tsx
+++ b/studio/components/layouts/ProjectLayout/TestConnection.tsx
@@ -1,15 +1,7 @@
-import useSWR, { mutate } from 'swr'
import { useRouter } from 'next/router'
import { observer } from 'mobx-react-lite'
-import { FC, useEffect, ReactNode } from 'react'
-import { isUndefined } from 'lodash'
-import { Typography } from '@supabase/ui'
-
+import { FC, ReactNode } from 'react'
import { Project } from 'types'
-import { useStore } from 'hooks'
-import { get } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
-import Loading from 'components/ui/Loading'
import BuildingState from 'components/to-be-cleaned/BuildingState'
interface Props {
@@ -17,47 +9,35 @@ interface Props {
children: ReactNode
}
+/**
+ * Check project.status to show building state or error state
+ *
+ * TODO: how can we test project connection properly?
+ * ex: the status is ACTIVE_HEALTHY but the project instance is down.
+ */
const TestConnection: FC<Props> = ({ project, children }) => {
const router = useRouter()
- const { app } = useStore()
-
- const FETCH_PROJECT_URL = `${API_URL}/props/project/${project!.ref}`
- const { data: projectRealtime, error }: any = useSWR(FETCH_PROJECT_URL, get)
-
- const isBuilding = project && !['ACTIVE_HEALTHY', 'RESTORING'].includes(project.status)
- const isWaitingForConnection =
- !isBuilding && !isUndefined(error) && projectRealtime?.project?.status !== 'ACTIVE_HEALTHY'
- const isActive = !isBuilding && !isWaitingForConnection
-
- if (isActive) app.onProjectUpdated(projectRealtime)
-
- useEffect(() => {
- if (isBuilding && router.asPath !== `/project/${project.ref}/building`) {
- router.push(`/project/${project.ref}/building`)
- }
- }, [])
+ const isBuilding = ['COMING_UP', 'RESTORING'].includes(project?.status)
+ const isProjectActive = project.status === 'ACTIVE_HEALTHY'
- useEffect(() => {
- // mutate project info cached. To ensure realtime project info is latest.
- mutate(FETCH_PROJECT_URL)
- }, [project])
-
- if (isWaitingForConnection) {
- return (
- <div className="w-full h-full m-4 flex flex-col items-center justify-center">
- <div className="w-32 flex items-center justify-center">
- <Loading />
- </div>
- <Typography.Text>
- {project.status === 'RESTORING' ? 'Restoring...' : 'Connecting...'}
- </Typography.Text>
- </div>
- )
+ if (isBuilding && router.asPath !== `/project/${project.ref}/building`) {
+ router.push(`/project/${project.ref}/building`, undefined, { shallow: true })
}
- if (isBuilding) return <BuildingState />
-
- return <>{children}</>
+ return (
+ <>
+ {isProjectActive ? (
+ children
+ ) : isBuilding ? (
+ <BuildingState project={project} />
+ ) : (
+ <div className="w-full h-full m-4 flex flex-col items-center justify-center">
+ <p>{`Invalid project status: ${project.status}`}</p>
+ <p>Try refreshing your browser, or reach out to us at [email protected]</p>
+ </div>
+ )}
+ </>
+ )
}
export default observer(TestConnection)
diff --git a/studio/components/to-be-cleaned/BuildingState.tsx b/studio/components/to-be-cleaned/BuildingState.tsx
index 8444d5bc1f2..c4ec88a19b6 100644
--- a/studio/components/to-be-cleaned/BuildingState.tsx
+++ b/studio/components/to-be-cleaned/BuildingState.tsx
@@ -1,6 +1,5 @@
-import * as React from 'react'
import Link from 'next/link'
-import { useRouter } from 'next/router'
+import { FC, useEffect, useRef } from 'react'
import { observer } from 'mobx-react-lite'
import { Badge, IconArrowRight, IconLoader, Button } from '@supabase/ui'
import {
@@ -14,30 +13,31 @@ import { CLIENT_LIBRARIES, EXAMPLE_PROJECTS } from 'components/interfaces/Home/H
import { API_URL } from 'lib/constants'
import { useStore } from 'hooks'
import { get } from 'lib/common/fetch'
+import { Project } from 'types'
-type ProjectBuildingState = {} & any
-const ProjectBuildingState: React.FC<ProjectBuildingState> = () => {
- const { ui, app } = useStore()
- const project: any = ui.selectedProject
-
- const router = useRouter()
- const { ref } = router.query
- const checkServerInterval = React.useRef<number>()
+type ProjectBuildingState = { project: Project }
+const ProjectBuildingState: FC<ProjectBuildingState> = ({ project }) => {
+ const { app } = useStore()
+ const checkServerInterval = useRef<number>()
async function checkServer() {
- const response = await get(`${API_URL}/props/project/${ref}`)
- if (response && response.project) {
- const { project } = response
- if (project.status === 'ACTIVE_HEALTHY') {
- const res = await get(`${API_URL}/props/project/${ref}/connection-string`)
- if (res && res.connectionString) project.connectionString = res.connectionString
- app.onProjectUpdated(project)
+ if (!project) return
+
+ const projectStatus = await get(`${API_URL}/projects/${project.ref}/status`)
+ if (projectStatus && !projectStatus.error) {
+ const { status } = projectStatus
+ if (status === 'ACTIVE_HEALTHY') {
+ const res = await get(`${API_URL}/props/project/${project.ref}/connection-string`)
+ if (res && res.connectionString) {
+ app.onProjectConnectionStringUpdated(project.id, res.connectionString)
+ }
+ app.onProjectStatusUpdated(project.id, status)
clearInterval(checkServerInterval.current)
}
}
}
- React.useEffect(() => {
+ useEffect(() => {
// check server status every 2s
checkServerInterval.current = window.setInterval(checkServer, 2000)
return () => {
@@ -50,11 +50,13 @@ const ProjectBuildingState: React.FC<ProjectBuildingState> = () => {
<div className="flex flex-col space-y-16 mx-6">
<div className=" flex flex-col gap-4">
<div className="space-x-3 flex items-center">
- <h1 className="text-scale-1200 text-3xl">{project.name}</h1>
+ <h1 className="text-scale-1200 text-3xl">{project?.name}</h1>
<Badge color="brand">
<div className="flex items-center gap-2">
<IconLoader className="animate-spin" size={12} />
- <span>Setting up project</span>
+ <span>
+ {project.status === 'RESTORING' ? 'Restoring project' : 'Setting up project'}
+ </span>
</div>
</Badge>
</div>
diff --git a/studio/stores/app/AppStore.ts b/studio/stores/app/AppStore.ts
index 17c22e03245..63ff300c127 100644
--- a/studio/stores/app/AppStore.ts
+++ b/studio/stores/app/AppStore.ts
@@ -14,6 +14,8 @@ export interface IAppStore {
database: IDatabaseStore
onProjectUpdated: (project: any) => void
onProjectDeleted: (project: any) => void
+ onProjectConnectionStringUpdated: (projectId: number, value: string) => void
+ onProjectStatusUpdated: (projectId: number, value: string) => void
onOrgAdded: (org: any) => void
onOrgUpdated: (org: any) => void
onOrgDeleted: (org: any) => void
@@ -39,12 +41,7 @@ export default class AppStore implements IAppStore {
onProjectUpdated(project: any) {
if (project && project.id) {
- const kpsVersion =
- project.services?.length > 0
- ? project.services[0]?.infrastructure[0]?.app_versions?.version
- : undefined
const clone = cloneDeep(this.projects.data[project.id])
- clone.kpsVersion = kpsVersion ?? clone.kpsVersion
clone.name = project.name
clone.status = project.status
this.projects.data[project.id] = clone
@@ -60,6 +57,18 @@ export default class AppStore implements IAppStore {
}
}
+ onProjectConnectionStringUpdated(projectId: number, value: string) {
+ const clone = cloneDeep(this.projects.data[projectId])
+ clone.connectionString = value
+ this.projects.data[projectId] = clone
+ }
+
+ onProjectStatusUpdated(projectId: number, value: string) {
+ const clone = cloneDeep(this.projects.data[projectId])
+ clone.status = value
+ this.projects.data[projectId] = clone
+ }
+
onOrgUpdated(updatedOrg: Organization) {
if (updatedOrg && updatedOrg.id) {
const originalOrg = this.organizations.data[updatedOrg.id]