Skip to content

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

Remove all restore useEffects, and convert restore project to RQ mutation

Joshen Limd01ce64d1f1c
diff --git a/studio/components/layouts/ProjectLayout/ProjectPausedState.tsx b/studio/components/layouts/ProjectLayout/ProjectPausedState.tsx
index 8f56ef4002a..3967ddb4f47 100644
--- a/studio/components/layouts/ProjectLayout/ProjectPausedState.tsx
+++ b/studio/components/layouts/ProjectLayout/ProjectPausedState.tsx
@@ -1,19 +1,19 @@
import * as Tooltip from '@radix-ui/react-tooltip'
import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useQueryClient } from '@tanstack/react-query'
+import { useParams } from 'common'
import Link from 'next/link'
import { useState } from 'react'
+import { Button, IconPauseCircle, Modal } from 'ui'
-import { useParams } from 'common'
import ConfirmModal from 'components/ui/Dialogs/ConfirmDialog'
import { useFreeProjectLimitCheckQuery } from 'data/organizations/free-project-limit-check-query'
+import { useProjectRestoreMutation } from 'data/projects/project-restore-mutation'
import { setProjectStatus } from 'data/projects/projects-query'
+import { useProjectSubscriptionV2Query } from 'data/subscriptions/project-subscription-v2-query'
import { useCheckPermissions, useSelectedOrganization, useStore } from 'hooks'
-import { post } from 'lib/common/fetch'
-import { API_URL, PROJECT_STATUS } from 'lib/constants'
-import { Button, IconPauseCircle, Modal } from 'ui'
+import { PROJECT_STATUS } from 'lib/constants'
import { useProjectContext } from './ProjectContext'
-import { useProjectSubscriptionV2Query } from 'data/subscriptions/project-subscription-v2-query'
export interface ProjectPausedStateProps {
product?: string
@@ -36,6 +36,13 @@ const ProjectPausedState = ({ product }: ProjectPausedStateProps) => {
const [showConfirmRestore, setShowConfirmRestore] = useState(false)
const [showFreeProjectLimitWarning, setShowFreeProjectLimitWarning] = useState(false)
+ const { mutate: restoreProject } = useProjectRestoreMutation({
+ onSuccess: (res, variables) => {
+ setProjectStatus(queryClient, variables.ref, PROJECT_STATUS.RESTORING)
+ ui.setNotification({ category: 'success', message: 'Restoring project' })
+ },
+ })
+
const canResumeProject = useCheckPermissions(
PermissionAction.INFRA_EXECUTE,
'queue_jobs.projects.initialize_or_resume'
@@ -51,7 +58,7 @@ const ProjectPausedState = ({ product }: ProjectPausedStateProps) => {
else setShowConfirmRestore(true)
}
- const onConfirmRestore = async () => {
+ const onConfirmRestore = () => {
if (!project) {
return ui.setNotification({
error: 'Project is required',
@@ -59,19 +66,7 @@ const ProjectPausedState = ({ product }: ProjectPausedStateProps) => {
message: 'Unable to restore: project is required',
})
}
-
- const { error } = await post(`${API_URL}/projects/${project.ref}/restore`, {})
-
- if (error) {
- ui.setNotification({
- category: 'error',
- error,
- message: `Failed to restore project: ${error.message}`,
- })
- } else {
- setProjectStatus(queryClient, project.ref, PROJECT_STATUS.RESTORING)
- ui.setNotification({ category: 'success', message: 'Restoring project' })
- }
+ restoreProject({ ref: project.ref })
}
return (
diff --git a/studio/data/projects/project-restore-mutation.ts b/studio/data/projects/project-restore-mutation.ts
new file mode 100644
index 00000000000..87c2626eb86
--- /dev/null
+++ b/studio/data/projects/project-restore-mutation.ts
@@ -0,0 +1,46 @@
+import { useMutation, UseMutationOptions } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
+import { post } from 'data/fetchers'
+import { ResponseError } from 'types'
+
+export type ProjectRestoreVariables = {
+ ref: string
+}
+
+export async function restoreProject({ ref }: ProjectRestoreVariables) {
+ const { data, error } = await post('/platform/projects/{ref}/restore', {
+ params: { path: { ref } },
+ body: {},
+ })
+ if (error) throw error
+ return data
+}
+
+type ProjectRestoreData = Awaited<ReturnType<typeof restoreProject>>
+
+export const useProjectRestoreMutation = ({
+ onSuccess,
+ onError,
+ ...options
+}: Omit<
+ UseMutationOptions<ProjectRestoreData, ResponseError, ProjectRestoreVariables>,
+ 'mutationFn'
+> = {}) => {
+ return useMutation<ProjectRestoreData, ResponseError, ProjectRestoreVariables>(
+ (vars) => restoreProject(vars),
+ {
+ async onSuccess(data, variables, context) {
+ await onSuccess?.(data, variables, context)
+ },
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to restore project: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ }
+ )
+}
diff --git a/studio/pages/project/[ref]/reports/index.tsx b/studio/pages/project/[ref]/reports/index.tsx
index c03b01da996..4a0f7d9fc26 100644
--- a/studio/pages/project/[ref]/reports/index.tsx
+++ b/studio/pages/project/[ref]/reports/index.tsx
@@ -9,8 +9,6 @@ import ProductEmptyState from 'components/to-be-cleaned/ProductEmptyState'
import { createReport } from 'components/to-be-cleaned/Reports/Reports.utils'
import Loading from 'components/ui/Loading'
import { useCheckPermissions, useStore } from 'hooks'
-import { post } from 'lib/common/fetch'
-import { API_URL, PROJECT_STATUS } from 'lib/constants'
import { useProfile } from 'lib/profile'
import { useProjectContentStore } from 'stores/projectContentStore'
import { NextPageWithLayout } from 'types'
@@ -31,12 +29,6 @@ export const UserReportPage: NextPageWithLayout = () => {
subject: { id: profile?.id },
})
- useEffect(() => {
- if (project && project.status === PROJECT_STATUS.INACTIVE) {
- post(`${API_URL}/projects/${ref}/restore`, {})
- }
- }, [project])
-
async function loadReports() {
await contentStore.load()
const reports = contentStore.reports()
diff --git a/studio/pages/project/[ref]/settings/storage.tsx b/studio/pages/project/[ref]/settings/storage.tsx
index 09dd18e9582..728a44bd4b7 100644
--- a/studio/pages/project/[ref]/settings/storage.tsx
+++ b/studio/pages/project/[ref]/settings/storage.tsx
@@ -1,23 +1,14 @@
-import { useEffect } from 'react'
+import { useParams } from 'common'
-import { useParams } from 'common/hooks'
import { SettingsLayout } from 'components/layouts'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import { StorageSettings } from 'components/to-be-cleaned/Storage'
-import { post } from 'lib/common/fetch'
-import { API_URL, PROJECT_STATUS } from 'lib/constants'
import { NextPageWithLayout } from 'types'
const PageLayout: NextPageWithLayout = () => {
const { ref: projectRef } = useParams()
const { project } = useProjectContext()
- useEffect(() => {
- if (project && project.status === PROJECT_STATUS.INACTIVE) {
- post(`${API_URL}/projects/${projectRef}/restore`, {})
- }
- }, [project])
-
return (
<div className="flex flex-grow p-4 storage-container">
<StorageSettings projectRef={projectRef} />
diff --git a/studio/pages/project/[ref]/storage/buckets/[bucketId].tsx b/studio/pages/project/[ref]/storage/buckets/[bucketId].tsx
index 1dc44fe2deb..99dde9c3777 100644
--- a/studio/pages/project/[ref]/storage/buckets/[bucketId].tsx
+++ b/studio/pages/project/[ref]/storage/buckets/[bucketId].tsx
@@ -1,15 +1,12 @@
import { find } from 'lodash'
import { observer } from 'mobx-react-lite'
-import { useEffect } from 'react'
-
import { useParams } from 'common'
+
import { StorageLayout } from 'components/layouts'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import StorageBucketsError from 'components/layouts/StorageLayout/StorageBucketsError'
import { StorageExplorer } from 'components/to-be-cleaned/Storage'
import { useBucketsQuery } from 'data/storage/buckets-query'
-import { post } from 'lib/common/fetch'
-import { API_URL, PROJECT_STATUS } from 'lib/constants'
import { NextPageWithLayout } from 'types'
const PageLayout: NextPageWithLayout = () => {
@@ -18,17 +15,10 @@ const PageLayout: NextPageWithLayout = () => {
const { data, isSuccess, isError, error } = useBucketsQuery({ projectRef: ref })
const buckets = data ?? []
-
- useEffect(() => {
- if (project && project.status === PROJECT_STATUS.INACTIVE) {
- post(`${API_URL}/projects/${ref}/restore`, {})
- }
- }, [ref, project])
-
- if (!project) return <div></div>
-
const bucket = find(buckets, { id: bucketId })
+ if (!project) return null
+
return (
<div className="storage-container flex flex-grow p-4">
{isError && <StorageBucketsError error={error as any} />}
diff --git a/studio/pages/project/[ref]/storage/buckets/index.tsx b/studio/pages/project/[ref]/storage/buckets/index.tsx
index 4d5ef33b303..cb611229f85 100644
--- a/studio/pages/project/[ref]/storage/buckets/index.tsx
+++ b/studio/pages/project/[ref]/storage/buckets/index.tsx
@@ -1,13 +1,10 @@
-import { useEffect } from 'react'
+import { useParams } from 'common'
-import { useParams } from 'common/hooks'
import { StorageLayout } from 'components/layouts'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import StorageBucketsError from 'components/layouts/StorageLayout/StorageBucketsError'
import ProductEmptyState from 'components/to-be-cleaned/ProductEmptyState'
import { useBucketsQuery } from 'data/storage/buckets-query'
-import { post } from 'lib/common/fetch'
-import { API_URL, PROJECT_STATUS } from 'lib/constants'
import { NextPageWithLayout } from 'types'
/**
@@ -16,15 +13,8 @@ import { NextPageWithLayout } from 'types'
const PageLayout: NextPageWithLayout = () => {
const { ref } = useParams()
const { project } = useProjectContext()
-
const { error, isError } = useBucketsQuery({ projectRef: ref })
- useEffect(() => {
- if (project && project.status === PROJECT_STATUS.INACTIVE) {
- post(`${API_URL}/projects/${ref}/restore`, {})
- }
- }, [ref, project])
-
if (!project) return <div></div>
if (isError) <StorageBucketsError error={error as any} />
diff --git a/studio/pages/project/[ref]/storage/policies.tsx b/studio/pages/project/[ref]/storage/policies.tsx
index 5b42fbbb18d..c858200857f 100644
--- a/studio/pages/project/[ref]/storage/policies.tsx
+++ b/studio/pages/project/[ref]/storage/policies.tsx
@@ -1,13 +1,11 @@
+import { useParams } from 'common'
import { observer } from 'mobx-react-lite'
import { useEffect } from 'react'
-import { useParams } from 'common'
import { StorageLayout } from 'components/layouts'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import { StoragePolicies } from 'components/to-be-cleaned/Storage'
import { useStore } from 'hooks'
-import { post } from 'lib/common/fetch'
-import { API_URL, PROJECT_STATUS } from 'lib/constants'
import { NextPageWithLayout } from 'types'
/**
@@ -20,9 +18,6 @@ const PageLayout: NextPageWithLayout = () => {
const { project } = useProjectContext()
useEffect(() => {
- if (project && project.status === PROJECT_STATUS.INACTIVE) {
- post(`${API_URL}/projects/${ref}/restore`, {})
- }
meta.roles.load()
}, [project])