Skip to content

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

move useNotifications to react-query

Alaister Younga0f7792b7bfd
diff --git a/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/NotificationRow.tsx b/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/NotificationRow.tsx
index 897df2353c1..30cb21a6273 100644
--- a/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/NotificationRow.tsx
+++ b/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/NotificationRow.tsx
@@ -4,13 +4,15 @@ import { FC, useState } from 'react'
import * as Tooltip from '@radix-ui/react-tooltip'
import { Notification, NotificationStatus } from '@supabase/shared-types/out/notifications'
-import { useStore, useNotifications } from 'hooks'
+import { useStore } from 'hooks'
+import { notificationKeys } from 'data/notifications/keys'
import { Project } from 'types'
import { formatNotificationCTAText, formatNotificationText } from './NotificationRows.utils'
import NotificationActions from './NotificationActions'
import { get, delete_ } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import { Button, IconX } from 'ui'
+import { useQueryClient } from '@tanstack/react-query'
interface Props {
notification: Notification
@@ -28,7 +30,7 @@ const NotificationRow: FC<Props> = ({
onSelectFinalizeMigration,
}) => {
const { app, ui } = useStore()
- const { refresh } = useNotifications()
+ const queryClient = useQueryClient()
const [dismissing, setDismissing] = useState(false)
const [project] = app.projects.list((project: Project) => project.id === notification.project_id)
@@ -36,7 +38,7 @@ const NotificationRow: FC<Props> = ({
const changelogLink = (notification.data as any).changelog_link
const availableActions = notification.meta?.actions_available ?? []
- // [Joshen TODO] This should be removed after 5th November when the migration notifications
+ // [Joshen TODO] This should be removed after the env of Feb when the migration notifications
// have been removed, double check with Qiao before removing.
// Relevant PR: https://github.com/supabase/supabase/pull/9229
const { data: ownerReassignStatus } = useSWR(
@@ -58,7 +60,7 @@ const NotificationRow: FC<Props> = ({
duration: 4000,
})
} else {
- refresh()
+ await queryClient.invalidateQueries(notificationKeys.list())
}
setDismissing(false)
}
diff --git a/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/index.tsx b/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/index.tsx
index 00b33c7de2e..0aa4afd87a2 100644
--- a/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/index.tsx
+++ b/studio/components/layouts/ProjectLayout/LayoutHeader/NotificationsPopover/index.tsx
@@ -9,18 +9,19 @@ import {
} from '@supabase/shared-types/out/notifications'
import { Project } from 'types'
-import { useNotifications, useStore } from 'hooks'
+import { useStore } from 'hooks'
import { delete_, patch, post } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import NotificationRow from './NotificationRow'
import ConfirmModal from 'components/ui/Dialogs/ConfirmDialog'
+import { useNotificationsQuery } from 'data/notifications/notifications-query'
interface Props {}
const NotificationsPopover: FC<Props> = () => {
const router = useRouter()
const { app, meta, ui } = useStore()
- const { notifications, refresh } = useNotifications()
+ const { data: notifications, refetch } = useNotificationsQuery()
const [projectToRestart, setProjectToRestart] = useState<Project>()
const [projectToApplyMigration, setProjectToApplyMigration] = useState<Project>()
@@ -36,6 +37,7 @@ const NotificationsPopover: FC<Props> = () => {
)
const onOpenChange = async (open: boolean) => {
+ // TODO(alaister): move this to a mutation
if (!open) {
// Mark notifications as seen
const notificationIds = notifications
@@ -44,7 +46,7 @@ const NotificationsPopover: FC<Props> = () => {
if (notificationIds.length > 0) {
const { error } = await patch(`${API_URL}/notifications`, { ids: notificationIds })
if (error) console.error('Failed to update notifications', error)
- refresh()
+ refetch()
}
}
}
diff --git a/studio/data/notifications/keys.ts b/studio/data/notifications/keys.ts
new file mode 100644
index 00000000000..9142775070a
--- /dev/null
+++ b/studio/data/notifications/keys.ts
@@ -0,0 +1,3 @@
+export const notificationKeys = {
+ list: () => ['notifications'] as const,
+}
diff --git a/studio/data/notifications/notifications-query.ts b/studio/data/notifications/notifications-query.ts
new file mode 100644
index 00000000000..7cbc6fb0c98
--- /dev/null
+++ b/studio/data/notifications/notifications-query.ts
@@ -0,0 +1,39 @@
+import { Notification } from '@supabase/shared-types/out/notifications'
+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 { notificationKeys } from './keys'
+
+export type NotificationsResponse = Notification[]
+
+export async function getNotifications(signal?: AbortSignal) {
+ const response = await get(`${API_URL}/notifications`, {
+ signal,
+ })
+ if (response.error) {
+ throw response.error
+ }
+
+ return response as NotificationsResponse
+}
+
+export type NotificationsData = Awaited<ReturnType<typeof getNotifications>>
+export type NotificationsError = unknown
+
+export const useNotificationsQuery = <TData = NotificationsData>(
+ options: UseQueryOptions<NotificationsData, NotificationsError, TData> = {}
+) =>
+ useQuery<NotificationsData, NotificationsError, TData>(
+ notificationKeys.list(),
+ ({ signal }) => getNotifications(signal),
+ options
+ )
+
+export const useNotificationsPrefetch = () => {
+ const client = useQueryClient()
+
+ return useCallback(() => {
+ client.prefetchQuery(notificationKeys.list(), ({ signal }) => getNotifications(signal))
+ }, [])
+}
diff --git a/studio/data/platform/platform-status-query.ts b/studio/data/platform/platform-status-query.ts
index 5f890f4abd1..0cff8bfd4f5 100644
--- a/studio/data/platform/platform-status-query.ts
+++ b/studio/data/platform/platform-status-query.ts
@@ -22,10 +22,9 @@ export async function getPlatformStatus(signal?: AbortSignal) {
export type PlatformStatusData = Awaited<ReturnType<typeof getPlatformStatus>>
export type PlatformStatusError = unknown
-export const usePlatformStatusQuery = <TData = PlatformStatusData>({
- enabled = true,
- ...options
-}: UseQueryOptions<PlatformStatusData, PlatformStatusError, TData> = {}) =>
+export const usePlatformStatusQuery = <TData = PlatformStatusData>(
+ options: UseQueryOptions<PlatformStatusData, PlatformStatusError, TData> = {}
+) =>
useQuery<PlatformStatusData, PlatformStatusError, TData>(
platformKeys.status(),
({ signal }) => getPlatformStatus(signal),
diff --git a/studio/hooks/index.ts b/studio/hooks/index.ts
index c0834b091a9..1de5bd71b18 100644
--- a/studio/hooks/index.ts
+++ b/studio/hooks/index.ts
@@ -1,7 +1,6 @@
export * from './misc/useStore'
export * from './misc/withAuth'
export * from './misc/useOptimisticSqlSnippetCreate'
-export * from './misc/useNotifications'
export * from './misc/useParams'
export * from './misc/useCheckPermissions'
export * from './deprecated'
diff --git a/studio/hooks/misc/useNotifications.tsx b/studio/hooks/misc/useNotifications.tsx
deleted file mode 100644
index 23b8f35d3dd..00000000000
--- a/studio/hooks/misc/useNotifications.tsx
+++ /dev/null
@@ -1,19 +0,0 @@
-import useSWR from 'swr'
-import { Notification } from '@supabase/shared-types/out/notifications'
-
-import { get } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
-
-export const useNotifications = () => {
- const { data, error, mutate } = useSWR<Notification[]>(`${API_URL}/notifications`, get)
-
- const anyError = (data as any)?.error || error !== undefined
- const refresh = () => mutate()
-
- return {
- notifications: anyError ? undefined : data,
- isLoading: !anyError && !data,
- isError: !!anyError,
- refresh,
- }
-}