Files
supabase/apps/studio/data/notifications/notifications-v2-query.ts
Jordi Enric fa7c223209 fix(studio): use Compute management endpoints FUNC-896 (#50393)
## Problem

Studio still called the legacy `/workers` Management API routes and used
the old `project_worker` response contract, so Compute instances could
not be listed or retrieved after the API rename. The production API type
check also detected drift in the v1 and platform declarations.

## Fix

- Regenerate the v1, v2, and platform API declarations from the deployed
schemas.
- Update Studio list and detail queries to `/compute`.
- Align typed fixtures with the Compute response schemas and
`project_compute_instance` resource type.
- Update platform response type references to the generated `_Output`
schema names.

## How to test

- Run `pnpm api:verify-types`.
- Run `pnpm --filter api-types test`.
- Run `pnpm --filter studio test data/compute/compute.utils.test.ts
"tests/pages/project/[ref]/compute/index.test.tsx"`.
- Run `pnpm --filter studio typecheck`.
- Run `pnpm --filter common typecheck`.
- Run `pnpm --filter studio lint:ratchet`.

Expected result: production API declarations are synchronized, and
Studio requests the `/compute` list and detail endpoints and renders
`project_compute_instance` responses successfully.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Improvements**
* Updated API response handling across profiles, backups, notifications,
integrations, warehouses, access tokens, payments, and other Studio
workflows for more accurate serialized data.
* Compute instance pages and queries now use the compute-specific API
endpoints and response data.
* Improved feature-flag type handling when disabled feature data is
unavailable.

* **Tests**
* Updated automated coverage and fixtures to reflect current compute and
API response formats.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-09-15 12:50:56 +02:00

92 lines
3.0 KiB
TypeScript

import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query'
import { notificationKeys } from './keys'
import type { components } from '@/data/api'
import { get, handleError } from '@/data/fetchers'
import type { ResponseError, 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 = ResponseError
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
> = {}
) => {
return useInfiniteQuery({
queryKey: notificationKeys.listV2({ status, filters, limit }),
queryFn: ({ signal, pageParam }) =>
getNotifications({ status, filters, limit, page: pageParam }, signal),
enabled: enabled,
initialPageParam: 0,
getNextPageParam(lastPage, pages) {
const page = pages.length
if ((lastPage ?? []).length < limit) return undefined
return page
},
...options,
})
}