Files
supabase/apps/studio/data/projects/projects-infinite-query.ts
T
Joshen Lim fae1dd8b5e Chore/projects pagination part 01 (#39310)
* Midway

* Add projects-infinite-query and remove use projects query in account audit logs

* Remove useProjectsQuery from RouteValidationWrapper

* Remove useProjectsQuery from OrganizationCard

* Remove useProjectsQuery in new project page

* Remove useProjectsQuery from Organization Audit logs

* Fix check
2025-10-07 17:11:49 +08:00

77 lines
2.6 KiB
TypeScript

import { useInfiniteQuery, UseInfiniteQueryOptions } from '@tanstack/react-query'
import { components } from 'api-types'
import { get, handleError } from 'data/fetchers'
import { useProfile } from 'lib/profile'
import { ResponseError } from 'types'
import { projectKeys } from './keys'
// [Joshen] Try to keep this value a multiple of 6 (common denominator of 2 and 3) to fit the cards view
// So that the last row will always be a full row of cards while there's a next page
// API max rows is 100, I'm just choosing 96 here as the highest value thats a multiple of 6
const DEFAULT_LIMIT = 96
interface GetProjectsInfiniteVariables {
limit?: number
sort?: 'name_asc' | 'name_desc' | 'created_asc' | 'created_desc'
search?: string
page?: number
}
export type ProjectInfoInfinite =
components['schemas']['ListProjectsPaginatedResponse']['projects'][number]
async function getProjects(
{
limit = DEFAULT_LIMIT,
page = 0,
sort = 'name_asc',
search: _search = '',
}: GetProjectsInfiniteVariables,
signal?: AbortSignal,
headers?: Record<string, string>
) {
const offset = page * limit
const search = _search.length === 0 ? undefined : _search
const { data, error } = await get('/platform/projects', {
// @ts-ignore [Joshen] API type issue for Version 2 endpoints
params: { query: { limit, offset, sort, search } },
signal,
headers: { ...headers, Version: '2' },
})
if (error) handleError(error)
return data as unknown as components['schemas']['ListProjectsPaginatedResponse']
}
export type ProjectsInfiniteData = Awaited<ReturnType<typeof getProjects>>
export type ProjectsInfiniteError = ResponseError
export const useProjectsInfiniteQuery = <TData = ProjectsInfiniteData>(
{ limit = DEFAULT_LIMIT, sort = 'name_asc', search }: GetProjectsInfiniteVariables,
{
enabled = true,
...options
}: UseInfiniteQueryOptions<ProjectsInfiniteData, ProjectsInfiniteError, TData> = {}
) => {
const { profile } = useProfile()
return useInfiniteQuery<ProjectsInfiniteData, ProjectsInfiniteError, TData>(
projectKeys.infiniteList({ limit, sort, search }),
({ signal, pageParam }) => getProjects({ limit, page: pageParam, sort, search }, signal),
{
enabled: enabled && profile !== undefined,
getNextPageParam(lastPage, pages) {
const page = pages.length
const currentTotalCount = page * limit
// @ts-ignore [Joshen] API type issue for Version 2 endpoints
const totalCount = lastPage.pagination.count
if (currentTotalCount >= totalCount) return undefined
return page
},
...options,
}
)
}