import { useQuery } from '@tanstack/react-query' import { useId, useState } from 'react' import type { BaoClient } from '../../api/bao' import { listSecrets } from '../../api/secrets' interface Props { client: BaoClient mount: string /** Path within the mount: a secret ("app/db"), or a folder wildcard ("app/*", "*"). */ value: string onChange: (path: string) => void } /** * Pick a secret by browsing the mount, or tick "everything inside" to grant a whole folder. * Lists names only; secret values are never read. */ export function PathPicker({ client, mount, value, onChange }: Props) { const id = useId() const [prefix, setPrefix] = useState('') const [manual, setManual] = useState(false) const entries = useQuery({ queryKey: ['secrets', mount, prefix], queryFn: () => listSecrets(client, mount, prefix) }) const wildcard = value === `${prefix}*` const crumbs = prefix.split('/').filter(Boolean) function open(next: string) { setPrefix(next) // Keep the "everything inside" intent while moving between folders. onChange(wildcard ? `${next}*` : '') } if (manual) { return (

For a secret that doesn’t exist yet. A trailing * matches everything below.{' '}

) } const where = prefix ? `${mount}${prefix}` : mount return (
Secret {entries.isPending &&

Loading…

} {entries.error &&

Can’t list secrets here: {entries.error.message}

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

Nothing in {where} yet.

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

{value ? ( <> Granting on {wildcard ? `${where}*` : `${mount}${value}`}. ) : ( 'Choose a secret or tick the box above.' )}{' '}

) }