Files
supabase/apps/studio/data/auth/user-query.ts
Ivan Vasilov f9cbf661ca fix: Fix a bug in Edit FDW sheet (#41685)
* Add types to the getDecryptedValues function.

* Refactor the edit-wrapper-sheet to fetch the secrets by id, instead of name.

* Add try/catch for the secret values fetching.

* Minor CodeRabbit nitpicks.

* Small improvement for types in EditWrapperSheet

* Small fix for WrapperRow icon space issue hiding ChevronRight

* Update comment

* Only fetch encrypted IDs if value is a valid UUID

* Nit

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-01-05 12:00:49 +07:00

46 lines
1.5 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { UUID_REGEX } from '@/lib/constants'
import { executeSql, type ExecuteSqlError } from 'data/sql/execute-sql-query'
import { getUserSQL } from 'data/sql/queries/get-user'
import { UseCustomQueryOptions } from 'types'
import { authKeys } from './keys'
import { User } from './users-infinite-query'
type UserVariables = {
projectRef?: string
connectionString?: string | null
userId?: string | null
}
export async function getUser(
{ projectRef, connectionString, userId }: UserVariables,
signal?: AbortSignal
) {
if (!userId) throw new Error('UserID is required')
if (!UUID_REGEX.test(userId)) throw new Error('Invalid user ID format')
const sql = getUserSQL(userId)
const { result } = await executeSql(
{ projectRef, connectionString, sql, queryKey: [`user-${userId}`] },
signal
)
const user = result[0] as User | undefined
return user
}
export type UserData = Awaited<ReturnType<typeof getUser>>
export type UserError = ExecuteSqlError
export const useUserQuery = <TData = UserData>(
{ projectRef, connectionString, userId }: UserVariables,
{ enabled = true, ...options }: UseCustomQueryOptions<UserData, UserError, TData> = {}
) =>
useQuery<UserData, UserError, TData>({
queryKey: authKeys.user(projectRef, userId),
queryFn: ({ signal }) => getUser({ projectRef, connectionString, userId }, signal),
enabled: enabled && typeof projectRef !== 'undefined',
...options,
})