Files
supabase/apps/studio/data/notifications/notifications-v2-query.ts
T
9d534c9f5a fix: response error codes (#30581)
* fix: response error codes

* upgrade docs

* remove request url modification middleware

* move api routes for self-hosted to platform folder

* remove some lib/common/fetch usage

* docs: use middleware for openapi-fetch (#30600)

Get rid of the unauthedAllowedPost function (I don't think there's any harm in letting any requests that require authentication to just 403, they should be disabled at the React Query level and if not they will fail gracefully enough...)

* fix local count query

* add default values for clone mutation

* fix ts and codegen

* add missing lodash dep to playwright tests

* Fix the playwright tests to match the new folder structure for selfhosted variant.

* remove unused import

* Remove unused state

* remove unused sql debug mutation

* remove unused export

* fix notifications query

* fix jwt updating status

* fix typescript

* save sql snippet after renaming

* update codegen & fix ts error

* override array querySerializer

---------

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2025-01-20 11:27:38 +08:00

86 lines
3.0 KiB
TypeScript

import { useInfiniteQuery, UseInfiniteQueryOptions } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import type { components } from 'data/api'
import type { ResponseError } from 'types'
import { notificationKeys } from './keys'
const NOTIFICATIONS_PAGE_LIMIT = 10
export type NotificationVariables = {
page: number
limit?: number
status?: 'new' | 'seen' | 'archived'
filters: {
priority: readonly string[]
organizations: readonly string[]
projects: readonly string[]
}
}
export type Notification = components['schemas']['NotificationResponseV2']
/**
* Notification Data - This is not typed from the API end as it's meant to be open-ended
* @param title: Title of the notification
* @param message: Rendered as Markdown to support inline links. Should be capped to n characters
* @param project_id: (Optional) Only available if notification is specifically for a project (e.g exhaustion notification)
* @param actions: (Optional) Any sort of actions that we want to provide users, could be external link, could be something that needs to be handled on FE
*/
export type NotificationData = {
title: string
message: string
org_slug?: string
project_ref?: string
actions: { label: string; url?: string; action_type?: string }[]
}
export async function getNotifications(options: NotificationVariables, signal?: AbortSignal) {
const { status, filters, page = 0, limit = NOTIFICATIONS_PAGE_LIMIT } = options
const { data, error } = await get('/platform/notifications', {
params: {
query: {
offset: String(page * limit),
limit: String(limit),
// [Alaister]: 'as any' is needed because the API types don't reflect an array of strings
...(status !== undefined ? { status } : { status: ['new', 'seen'].join(',') as any }),
...(filters.priority.length > 0 ? { priority: filters.priority.join(',') as any } : {}),
...(filters.organizations.length > 0 ? { org_slug: filters.organizations.join(',') } : {}),
...(filters.projects.length > 0 ? { project_ref: filters.projects.join(',') } : {}),
},
},
headers: { Version: '2' },
signal,
})
if (error) handleError(error)
return data
}
export type NotificationsData = Awaited<ReturnType<typeof getNotifications>>
export type NotificationsError = ResponseError
export const useNotificationsV2Query = <TData = NotificationsData>(
{ status, filters, limit = NOTIFICATIONS_PAGE_LIMIT }: Omit<NotificationVariables, 'page'>,
{
enabled = true,
...options
}: UseInfiniteQueryOptions<NotificationsData, NotificationsError, TData> = {}
) => {
return useInfiniteQuery<NotificationsData, NotificationsError, TData>(
notificationKeys.listV2({ status, filters, limit }),
({ signal, pageParam }) =>
getNotifications({ status, filters, limit, page: pageParam }, signal),
{
enabled: enabled,
getNextPageParam(lastPage, pages) {
const page = pages.length
if ((lastPage ?? []).length < limit) return undefined
return page
},
...options,
}
)
}