import { useQuery } from '@tanstack/react-query' import { useId } from 'react' import type { BaoClient } from '../../api/bao' import type { EngineDef } from '../../api/engines/registry' import { listRoles } from '../../api/engines/roles' export interface RoleSelection { kindId: string /** A role name, "*" for every role of the kind, or "" for none chosen yet. */ role: string } /** Pick a role of a dynamic engine (or all of them) to grant access to. Lists role names only. */ export function RolePicker({ client, mount, engine, value, onChange, }: { client: BaoClient mount: string engine: EngineDef value: RoleSelection onChange: (v: RoleSelection) => void }) { const id = useId() const kind = engine.roleKinds.find((k) => k.id === value.kindId) ?? engine.roleKinds[0] const roles = useQuery({ queryKey: ['engine-roles', mount, kind.id], queryFn: () => listRoles(client, mount, kind) }) const all = value.role === '*' return (
Role {engine.roleKinds.length > 1 && ( )} {roles.isPending &&

Loading…

} {roles.error &&

Can’t list roles here: {roles.error.message}

} {roles.data?.length === 0 &&

No roles in {mount}{kind.path} yet.

} {!!roles.data?.length && ( )}
) }