mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## Problem
`useNotificationsV2Query` and `useProjectServiceStatusQuery` run
whenever their component is mounted, whether or not a session exists.
When a session ends and the app redirects to `/sign-in`, they can still
fetch (or refetch) `/platform/notifications` and
`/v1/projects/{ref}/health` with no `Authorization` header. The API then
responds 401 `No access token provided`, and the response is classified
as an `UnclassifiedApiError` and reported.
## Solution
Both hooks now gate `enabled` on `useIsLoggedIn()` from `common`.
`usePermissionsQuery` and `useOverdueInvoicesQuery` already follow this
pattern. The caller's `enabled` option still applies on top of it.
Self-hosted builds always count as logged in, so their behavior is
unchanged.
Checks run:
- `pnpm turbo run typecheck --filter='...[origin/master]'`: passes
- `pnpm turbo run lint --filter='[origin/master]'`: passes. The first
run of `studio#lint:type-checks` ran out of heap memory. It passed on a
rerun with `NODE_OPTIONS=--max-old-space-size=8192`.
- `pnpm exec vitest run
components/layouts/AppLayout/AdvisorButton.test.tsx
components/layouts/AppLayout/AdvisorButton.platform.test.tsx
components/ui/AdvisorPanel/AdvisorSignals.integration.test.tsx` (in
`apps/studio`): passes
- Prettier check on the changed files: passes
## Review instructions
1. Sign out of the Studio preview. Open
`/dashboard/sign-in?returnTo=/project/<ref>`, keep the network tab open,
and confirm that no request to `/platform/notifications` or
`/v1/projects/<ref>/health` returns 401.
2. Sign in and open a project's home page. The service status indicator
still loads its status.
3. Open the Advisor/Issues panel from the header. Notifications still
load, and the header button still shows the unread/critical indicator
when it applies.
## Checklist
- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
[FE-4989](https://linear.app/supabase/issue/FE-4989/unclassifiedapierror-no-access-token-provided)
Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com>
96 lines
3.2 KiB
TypeScript
96 lines
3.2 KiB
TypeScript
import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query'
|
|
import { useIsLoggedIn } from 'common'
|
|
|
|
import { notificationKeys } from './keys'
|
|
import type { components } from '@/data/api'
|
|
import { get, handleError } from '@/data/fetchers'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import type { UseCustomInfiniteQueryOptions } from '@/types'
|
|
|
|
const NOTIFICATIONS_PAGE_LIMIT = 10
|
|
|
|
export type NotificationVariables = {
|
|
page: number | undefined
|
|
limit?: number
|
|
status?: 'new' | 'seen' | 'archived'
|
|
filters: {
|
|
priority?: readonly string[]
|
|
organizations?: readonly string[]
|
|
projects?: readonly string[]
|
|
}
|
|
}
|
|
|
|
export type Notification = components['schemas']['NotificationResponse_Output']
|
|
|
|
/**
|
|
* 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 { priority = [], organizations = [], projects = [] } = filters
|
|
|
|
const { data, error } = await get('/platform/notifications', {
|
|
params: {
|
|
query: {
|
|
offset: page * limit,
|
|
limit,
|
|
...(status !== undefined ? { status } : { status: ['new', 'seen'].join(',') }),
|
|
...(priority.length > 0 ? { priority: priority.join(',') } : {}),
|
|
...(organizations.length > 0 ? { org_slug: organizations.join(',') } : {}),
|
|
...(projects.length > 0 ? { project_ref: projects.join(',') } : {}),
|
|
},
|
|
},
|
|
headers: { Version: '2' },
|
|
signal,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
|
|
return data
|
|
}
|
|
|
|
export type NotificationsData = Awaited<ReturnType<typeof getNotifications>>
|
|
export type NotificationsError = StudioError
|
|
|
|
export const useNotificationsV2Query = <TData = NotificationsData>(
|
|
{ status, filters, limit = NOTIFICATIONS_PAGE_LIMIT }: Omit<NotificationVariables, 'page'>,
|
|
{
|
|
enabled,
|
|
...options
|
|
}: UseCustomInfiniteQueryOptions<
|
|
NotificationsData,
|
|
NotificationsError,
|
|
InfiniteData<TData>,
|
|
readonly unknown[],
|
|
number | undefined
|
|
> = {}
|
|
) => {
|
|
const isLoggedIn = useIsLoggedIn()
|
|
|
|
return useInfiniteQuery({
|
|
queryKey: notificationKeys.listV2({ status, filters, limit }),
|
|
queryFn: ({ signal, pageParam }) =>
|
|
getNotifications({ status, filters, limit, page: pageParam }, signal),
|
|
enabled: (enabled ?? true) && isLoggedIn,
|
|
initialPageParam: 0,
|
|
getNextPageParam(lastPage, pages) {
|
|
const page = pages.length
|
|
if ((lastPage ?? []).length < limit) return undefined
|
|
return page
|
|
},
|
|
...options,
|
|
})
|
|
}
|