mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 09:25:06 +03:00
## Problem Storage's `list` (v1) endpoint is being deprecated in favor of `list-v2`, which uses cursor pagination instead of numeric offset (which degrades on large buckets) and fixes folders that differ only by case not both being listable. This is PR 1 of the migration (parent: FE-4423); it covers the shared infinite-query hook and its two consumers. ## Solution Added `listBucketObjectsV2` alongside the existing v1 `listBucketObjects` (still used elsewhere, migrated in a later PR), and replaced the `useBucketObjectsInfiniteQuery` hook with `bucketObjectsInfiniteQueryOptions` built on `infiniteQueryOptions`, following the repo's preferred data-fetching pattern. Pagination now uses `hasNext`/`nextCursor` instead of an offset multiplier, and `queryFn` rejects a response that claims `hasNext` without advancing the cursor so a misbehaving backend can't send `fetchNextPage` into an infinite loop. v2 splits results into separate `folders`/`objects` arrays and has no `search` field, so the two consumers (`BucketFilePickerColumn`, `MoveItemsFolderPicker`) merge/sort those arrays themselves, and search is folded into a `prefix` match instead. Also removed "Time last accessed" from the picker's sort dropdown since v2's `sortBy.column` doesn't support it, with a defensive fallback to `name` in case the shared sort preference (still used by the v1 main file explorer) carries that value over. Added the missing self-hosted `list-v2` API proxy route (`pages/api` + the TanStack `routes/api` wrapper) using storage-js's `listV2()`, since self-hosted Studio only had a v1 route and every v2 request was 404ing there. ## Review instructions 1. Open the bucket file picker (e.g. via an OAuth app logo upload), confirm folders and files both render and paginate correctly, and that searching still filters as expected. 2. Open the "Move items" modal's folder picker, confirm you can navigate into and back out of subfolders, and that folder search still works. 3. Run `pnpm test:studio -- MoveItemsModal`. ## 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 references [WORD_LIST](https://github.com/supabase/supabase/blob/master/apps/docs/WORD_LIST.md) and the docs [CONTRIBUTING](https://github.com/supabase/supabase/blob/master/apps/docs/CONTRIBUTING.md) guide 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Storage browsing loads large bucket listings in pages, helping keep navigation responsive. * Folder pickers display folders and files together across paginated results. * Moving items between folders uses the same paginated browsing experience. * Search remains available in the final folder level, and folder navigation shows the correct contents. * **Updates** * “Time last accessed” is no longer available as a sorting option in storage pickers. Sorting is available by name, creation time, or update time. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
63 lines
2.3 KiB
TypeScript
63 lines
2.3 KiB
TypeScript
import { infiniteQueryOptions } from '@tanstack/react-query'
|
|
|
|
import { listBucketObjectsV2 } from './bucket-objects-list-mutation'
|
|
import { storageKeys } from './keys'
|
|
import type { components } from '@/data/api'
|
|
|
|
const DEFAULT_LIMIT = 200
|
|
|
|
type StorageObjectsQueryParams = {
|
|
projectRef?: string
|
|
bucketId?: string
|
|
path: string
|
|
options?: Omit<
|
|
components['schemas']['GetObjectsV2Body'],
|
|
'prefix' | 'cursor' | 'with_delimiter'
|
|
> & {
|
|
/** Not a real v2 API field: folded into `prefix` so v2 returns entries starting with it */
|
|
search?: string
|
|
}
|
|
}
|
|
|
|
export type StorageObjectsPage = components['schemas']['StorageListResponseV2_Output']
|
|
|
|
export const bucketObjectsInfiniteQueryOptions = (
|
|
{ projectRef, bucketId, path, options }: StorageObjectsQueryParams,
|
|
{ enabled = true }: { enabled?: boolean } = {}
|
|
) => {
|
|
const { search, ...v2Options } = options ?? {}
|
|
// A trailing slash is required to browse a folder's contents with with_delimiter: true —
|
|
// without it, v2 treats e.g. "docs" as a partial name match against siblings like "docs-old"
|
|
// rather than descending into the folder.
|
|
const browsePrefix = path ? `${path}/` : ''
|
|
const prefix = search ? `${browsePrefix}${search}` : browsePrefix
|
|
const limit = v2Options.limit ?? DEFAULT_LIMIT
|
|
|
|
return infiniteQueryOptions({
|
|
// prefix and search are incorporated into the path.
|
|
// eslint-disable-next-line @tanstack/query/exhaustive-deps
|
|
queryKey: storageKeys.objects(projectRef, bucketId, path, options),
|
|
queryFn: ({ signal, pageParam }) =>
|
|
listBucketObjectsV2(
|
|
{
|
|
projectRef: projectRef!,
|
|
bucketId,
|
|
prefix,
|
|
cursor: pageParam,
|
|
options: { ...v2Options, limit },
|
|
},
|
|
signal
|
|
),
|
|
enabled: enabled && !!projectRef && !!bucketId,
|
|
initialPageParam: undefined as string | undefined,
|
|
getNextPageParam: (lastPage, _pages, lastPageParam) => {
|
|
if (!lastPage.hasNext) return undefined
|
|
// A backend that says there's more but doesn't advance the cursor would otherwise send
|
|
// fetchNextPage into an infinite loop re-fetching the same page forever; treat it as the
|
|
// end of the list instead.
|
|
if (!lastPage.nextCursor || lastPage.nextCursor === lastPageParam) return undefined
|
|
return lastPage.nextCursor
|
|
},
|
|
})
|
|
}
|