Files
supabase/apps/studio/data/content/sql-snippets-query.ts
Alaister Young f6d9617413 chore: paginate user content (#30675)
* chore: paginate user content

* progress

* loading states

* add load more buttons to private snippets

* working pagination

* fix some types

* always show snippet counts

* support new api parameters

* favorite snippets

* progress

* searching

* paginate root folder

* fix renaming snippets

* fix ts

* removed unused prop

* Shift sharing/unsharing query logic outside of valtio to leverage on RQ only

* Fix invalidation on an unsaved snippet

* Clean up

* Fix

* Clean up

* Update API type

* Update API

* fix duplicate snippets error after moving a snippet

* add currently selected snippet

* Fix unsharing a snippet that has yet to be opened

* i'm dumb

* fix sharing a snippet

* fix sharing and unsharing

* show favorite or shared snippet in list even if it's in another page

* Fix wrong import for debounce

* Fix false positive toast error when creating custom report

* Update API type

* Change create new snippet CTA to link back to /new with skip flag

* Fix saving logs explorer query

* Bump page number

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2024-12-19 16:08:14 +08:00

78 lines
2.1 KiB
TypeScript

import { useInfiniteQuery, UseInfiniteQueryOptions } from '@tanstack/react-query'
import { get } from 'data/fetchers'
import { Content } from './content-query'
import { contentKeys } from './keys'
import { SNIPPET_PAGE_LIMIT } from './sql-folders-query'
export type SqlSnippet = Extract<Content, { type: 'sql' }>
interface GetSqlSnippetsVariables {
projectRef?: string
cursor?: string
visibility?: SqlSnippet['visibility']
favorite?: boolean
name?: string
sort?: 'name' | 'inserted_at'
}
export async function getSqlSnippets(
{ projectRef, cursor, visibility, favorite, name, sort }: GetSqlSnippetsVariables,
signal?: AbortSignal
) {
if (typeof projectRef === 'undefined') {
throw new Error('projectRef is required for getSqlSnippets')
}
const sortOrder = sort === 'name' ? 'asc' : 'desc'
const { data, error } = await get('/platform/projects/{ref}/content', {
params: {
path: { ref: projectRef },
query: {
type: 'sql',
cursor,
visibility,
favorite,
name,
limit: SNIPPET_PAGE_LIMIT.toString(),
sort_by: sort,
sort_order: sortOrder,
},
},
signal,
})
if (error) {
throw error
}
return {
cursor: data.cursor,
contents: data.data as unknown as SqlSnippet[],
}
}
export type SqlSnippetsData = Awaited<ReturnType<typeof getSqlSnippets>>
export type SqlSnippetsError = unknown
export const useSqlSnippetsQuery = <TData = SqlSnippetsData>(
{ projectRef, sort, name, visibility, favorite }: Omit<GetSqlSnippetsVariables, 'cursor'>,
{
enabled = true,
...options
}: UseInfiniteQueryOptions<SqlSnippetsData, SqlSnippetsError, TData> = {}
) =>
useInfiniteQuery<SqlSnippetsData, SqlSnippetsError, TData>(
contentKeys.sqlSnippets(projectRef, { sort, name, visibility, favorite }),
({ signal, pageParam: cursor }) =>
getSqlSnippets({ projectRef, cursor, sort, name, visibility, favorite }, signal),
{
enabled: enabled && typeof projectRef !== 'undefined',
getNextPageParam(lastPage) {
return lastPage.cursor
},
...options,
}
)