mirror of
https://github.com/supabase/supabase.git
synced 2026-10-13 13:19:09 +03:00
* Replace all usage of useProjectContext with useSelectedProjectQuery * Replace all usage of useSelectedProject with useSelectedProjectQuery * Replace all usage of useProjectByRef with useProjectByRefQuery * Replace all usage of useSelectedOrganization with useSelectedOrganizationQuery * Deprecate useSelectedProject, useSelectedOrganization, and useProjectByRef hooks * Deprecate ProjecContext
63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
|
|
|
|
import { components } from 'api-types'
|
|
import { get, handleError } from 'data/fetchers'
|
|
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
|
|
import { PROJECT_STATUS } from 'lib/constants'
|
|
import type { ResponseError } from 'types'
|
|
import { storageKeys } from './keys'
|
|
|
|
export type BucketsVariables = { projectRef?: string }
|
|
|
|
export type Bucket = components['schemas']['StorageBucketResponse']
|
|
|
|
export type BucketType = Bucket['type']
|
|
|
|
export async function getBuckets({ projectRef }: BucketsVariables, signal?: AbortSignal) {
|
|
if (!projectRef) throw new Error('projectRef is required')
|
|
|
|
const { data, error } = await get('/platform/storage/{ref}/buckets', {
|
|
params: { path: { ref: projectRef } },
|
|
signal,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data as Bucket[]
|
|
}
|
|
|
|
export type BucketsData = Awaited<ReturnType<typeof getBuckets>>
|
|
export type BucketsError = ResponseError
|
|
|
|
export const useBucketsQuery = <TData = BucketsData>(
|
|
{ projectRef }: BucketsVariables,
|
|
{ enabled = true, ...options }: UseQueryOptions<BucketsData, BucketsError, TData> = {}
|
|
) => {
|
|
const { data: project } = useSelectedProjectQuery()
|
|
const isActive = project?.status === PROJECT_STATUS.ACTIVE_HEALTHY
|
|
|
|
return useQuery<BucketsData, BucketsError, TData>(
|
|
storageKeys.buckets(projectRef),
|
|
({ signal }) => getBuckets({ projectRef }, signal),
|
|
{
|
|
enabled: enabled && typeof projectRef !== 'undefined' && isActive,
|
|
...options,
|
|
retry: (failureCount, error) => {
|
|
if (
|
|
typeof error === 'object' &&
|
|
error !== null &&
|
|
error.message.startsWith('Tenant config') &&
|
|
error.message.endsWith('not found')
|
|
) {
|
|
return false
|
|
}
|
|
|
|
if (failureCount < 3) {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
},
|
|
}
|
|
)
|
|
}
|