Skip to content

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

move usePlatformStatus to react-query

Alaister Young8420764edf72
diff --git a/studio/data/platform/keys.ts b/studio/data/platform/keys.ts
new file mode 100644
index 00000000000..5b17e948137
--- /dev/null
+++ b/studio/data/platform/keys.ts
@@ -0,0 +1,3 @@
+export const platformKeys = {
+ status: () => ['platform', 'status'] as const,
+}
diff --git a/studio/data/platform/platform-status-query.ts b/studio/data/platform/platform-status-query.ts
new file mode 100644
index 00000000000..5f890f4abd1
--- /dev/null
+++ b/studio/data/platform/platform-status-query.ts
@@ -0,0 +1,41 @@
+import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
+import { get } from 'lib/common/fetch'
+import { API_URL } from 'lib/constants'
+import { useCallback } from 'react'
+import { platformKeys } from './keys'
+
+export type PlatformStatusResponse = {
+ isHealthy: boolean
+}
+
+export async function getPlatformStatus(signal?: AbortSignal) {
+ const response = await get(`${API_URL}/status`, {
+ signal,
+ })
+ if (response.error) {
+ throw response.error
+ }
+
+ return { isHealthy: response.is_healthy } as PlatformStatusResponse
+}
+
+export type PlatformStatusData = Awaited<ReturnType<typeof getPlatformStatus>>
+export type PlatformStatusError = unknown
+
+export const usePlatformStatusQuery = <TData = PlatformStatusData>({
+ enabled = true,
+ ...options
+}: UseQueryOptions<PlatformStatusData, PlatformStatusError, TData> = {}) =>
+ useQuery<PlatformStatusData, PlatformStatusError, TData>(
+ platformKeys.status(),
+ ({ signal }) => getPlatformStatus(signal),
+ options
+ )
+
+export const usePlatformStatusPrefetch = () => {
+ const client = useQueryClient()
+
+ return useCallback(() => {
+ client.prefetchQuery(platformKeys.status(), ({ signal }) => getPlatformStatus(signal))
+ }, [])
+}
diff --git a/studio/hooks/index.ts b/studio/hooks/index.ts
index 9171556da97..c0834b091a9 100644
--- a/studio/hooks/index.ts
+++ b/studio/hooks/index.ts
@@ -2,7 +2,6 @@ export * from './misc/useStore'
export * from './misc/withAuth'
export * from './misc/useOptimisticSqlSnippetCreate'
export * from './misc/useNotifications'
-export * from './misc/usePlatformStatus'
export * from './misc/useParams'
export * from './misc/useCheckPermissions'
export * from './deprecated'
diff --git a/studio/hooks/misc/usePlatformStatus.tsx b/studio/hooks/misc/usePlatformStatus.tsx
deleted file mode 100644
index 1fac085769d..00000000000
--- a/studio/hooks/misc/usePlatformStatus.tsx
+++ /dev/null
@@ -1,17 +0,0 @@
-import useSWR from 'swr'
-import { get } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
-
-export const usePlatformStatus = () => {
- const { data, error, mutate } = useSWR<{ is_healthy: boolean }>(`${API_URL}/status`, get)
-
- const anyError = (data as any)?.error || error !== undefined
- const refresh = () => mutate()
-
- return {
- isHealthy: anyError ? undefined : data?.is_healthy,
- isLoading: !anyError && !data,
- isError: !!anyError,
- refresh,
- }
-}
diff --git a/studio/pages/support/new.tsx b/studio/pages/support/new.tsx
index 424e7e991ca..8fb972d306f 100644
--- a/studio/pages/support/new.tsx
+++ b/studio/pages/support/new.tsx
@@ -5,7 +5,8 @@ import { observer } from 'mobx-react-lite'
import { Button, IconLoader } from 'ui'
import * as Tooltip from '@radix-ui/react-tooltip'
-import { withAuth, useFlag, usePlatformStatus } from 'hooks'
+import { withAuth, useFlag } from 'hooks'
+import { usePlatformStatusQuery } from 'data/platform/platform-status-query'
import Success from 'components/interfaces/Support/Success'
import SupportForm from 'components/interfaces/Support/SupportForm'
@@ -15,7 +16,8 @@ const SupportPage = () => {
const ongoingIncident = useFlag('ongoingIncident')
const maxHeight = ongoingIncident ? 'calc(100vh - 44px)' : '100vh'
- const { isHealthy, isLoading } = usePlatformStatus()
+ const { data, isLoading } = usePlatformStatusQuery()
+ const isHealthy = data?.isHealthy
return (
<div