Files
supabase/apps/studio/data/jit-db-access/jit-db-access-status-query.ts
Joshen Lim 15e95ab013 Joshenlim/fe 5242 jit db access show assigned role (#54214)
## Context

Building on top of https://github.com/supabase/supabase/pull/54192,
there was a missing piece to the JIT DB access UI which was showing the
assigned access to the user if any (On staging it just shows an error,
on the previous PR we show a "Contact your project admin" CTA).

So this PR addresses that - there's a couple of cases to consider and
I'd love for design to feedback / adjust as well as I'm not convinced
this is the best way to do things. Am deliberately leaving out the
"copyable connection strings" part for now as Danny's investigating into
that one

### If you're a non-owner

If not assigned a role:
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/111d7c4f-0083-4bd5-88d5-ec45974ccf4d"
/>

If assigned a single role:
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/1b4b1f3c-9a20-4010-bff8-7bf68a455e26"
/>

If assigned multiple roles:
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/42cc66f6-4604-42eb-ab30-f58b1396ca6b"
/>
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/0914e1a3-9c7d-463e-851d-65f6b9793bb7"
/>

If (all) access expired:
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/bda0fc65-e22a-49d9-b444-fc5d58a4745a"
/>

### If you're an owner

If not assigned a role: Status quo
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/435abe70-ae88-49bb-86b9-7a03963b5b66"
/>

If yourself is assigned a role:
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/9fafc74a-1a33-489e-a7da-70f57356a034"
/>

If yourself is assigned multiple roles:
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/f4af901b-a24e-4272-b573-9ccb474bb7e2"
/>

If (all) access expired: Same as not assigned a role, just refer back to
the table
2026-11-30 12:57:24 +07:00

51 lines
1.9 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { jitDbAccessKeys } from './keys'
import { get, handleError, unguarded } from '@/data/fetchers'
import type { StudioError } from '@/lib/errors/studio-errors'
import type { UseCustomQueryOptions } from '@/types'
type JitDbAccessStatusVariables = { projectRef?: string }
// [Joshen] JFYI endpoint returns a 406 if the user doesn't have any JIT access
// So dashboard will be intentionally triggering this error since we need to call this
// endpoint to figure out if the user has been assigned JIT access (esp for non owners
// who do not have the perms to GET /database/jit/list)
async function getJitDbAccessStatus(
{ projectRef }: JitDbAccessStatusVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
const { data, error, response } = await get(`/v1/projects/{ref}/database/jit`, {
params: { path: { ref: projectRef } },
signal,
grant: unguarded.migrating,
})
// API responds 406 when the user has no JIT access mapping on the project - not an error
if (response.status === 406) return { user_id: undefined, user_roles: [] }
if (error) return handleError(error)
return data
}
export type JitDbAccessStatusData = Awaited<ReturnType<typeof getJitDbAccessStatus>>
type JitDbAccessStatusError = StudioError
export const useJitDbAccessStatusQuery = <TData = JitDbAccessStatusData>(
{ projectRef }: JitDbAccessStatusVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<JitDbAccessStatusData, JitDbAccessStatusError, TData> = {}
) =>
useQuery<JitDbAccessStatusData, JitDbAccessStatusError, TData>({
queryKey: jitDbAccessKeys.status(projectRef),
queryFn: ({ signal }) => getJitDbAccessStatus({ projectRef }, signal),
enabled: enabled && typeof projectRef !== 'undefined',
refetchOnWindowFocus: false,
...options,
})