Files
envbao-admin/src/features/identities/EntitiesTab.tsx
T
mattandClaude Sonnet 5.5 61bb6d912a
CI / integration (push) Successful in 12s
CI / check (push) Successful in 16s
Phone-width layout fixes (ENB-23)
Checked at 390px and 360px in a headless mobile Chrome; no horizontal overflow
on any page.

- Tables become stacked cards on phones, with column names from data-label and
  row actions underneath instead of off the edge.
- Nav fits on one line; chips no longer wrap mid-name; less nested padding.
- Graph opens at a fixed readable zoom on phones with a pan/zoom hint, and
  scrolls to the details card when a node or label is tapped.
- Add the missing headings and wide layout to the user, entity and group forms
  (previously only the token form had them).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-20 13:34:21 +01:00

166 lines
6.2 KiB
TypeScript

import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { useState, type FormEvent } from 'react'
import type { BaoClient } from '../../api/bao'
import { addAlias, deleteAlias, deleteEntity, listEntities, saveEntity, userpassAccessor, type Entity } from '../../api/identities'
import { AccessEditor } from '../access/AccessEditor'
import { ListState } from '../shared/ListState'
import { PolicyPicker } from '../shared/PolicyPicker'
import { useConfirm } from '../shared/useConfirm'
import { NameChips, PolicyChips } from '../shared/PolicyChips'
interface Draft {
name: string
policies: string[]
isNew: boolean
entity?: Entity
}
export function EntitiesTab({ client }: { client: BaoClient }) {
const qc = useQueryClient()
const confirm = useConfirm()
const entities = useQuery({ queryKey: ['entities'], queryFn: () => listEntities(client) })
const [draft, setDraft] = useState<Draft | null>(null)
const [accessFor, setAccessFor] = useState<string | null>(null)
const [aliasName, setAliasName] = useState('')
const refresh = () => qc.invalidateQueries({ queryKey: ['entities'] })
const save = useMutation({
mutationFn: (d: Draft) => saveEntity(client, { name: d.name, policies: d.policies }),
onSuccess: () => {
setDraft(null)
return refresh()
},
})
const remove = useMutation({ mutationFn: (name: string) => deleteEntity(client, name), onSuccess: refresh })
const attach = useMutation({
mutationFn: async ({ entity, name }: { entity: Entity; name: string }) => {
const accessor = await userpassAccessor(client)
if (!accessor) throw new Error('The userpass auth method is not enabled.')
await addAlias(client, { name, entityId: entity.id, mountAccessor: accessor })
},
onSuccess: () => {
setAliasName('')
setDraft(null)
return refresh()
},
})
const detach = useMutation({
mutationFn: (aliasId: string) => deleteAlias(client, aliasId),
onSuccess: () => {
setDraft(null)
return refresh()
},
})
function submit(e: FormEvent) {
e.preventDefault()
if (draft) save.mutate(draft)
}
return (
<section>
<ListState query={entities} empty="No entities yet." />
{!!entities.data?.length && (
<table aria-label="Entities">
<thead>
<tr>
<th>Entity</th>
<th>Aliases</th>
<th>Policies</th>
<th />
</tr>
</thead>
<tbody>
{entities.data?.map((e) => (
<tr key={e.id}>
<td>{e.name}</td>
<td data-label="Aliases">
<NameChips names={e.aliases.map((a) => a.name)} />
</td>
<td data-label="Policies">
<PolicyChips policies={e.policies} />
</td>
<td>
<button type="button" aria-label={`Access ${e.name}`} onClick={() => setAccessFor(e.name)}>
Access
</button>
<button type="button" aria-label={`Edit ${e.name}`} onClick={() => setDraft({ name: e.name, policies: e.policies, isNew: false, entity: e })}>
Edit
</button>
<button
type="button"
aria-label={`Delete ${e.name}`}
onClick={async () => {
if (await confirm({ title: `Delete entity “${e.name}”?`, message: 'Its aliases are deleted too. This cannot be undone.', confirmLabel: 'Delete' }))
remove.mutate(e.name)
}}
>
Delete
</button>
</td>
</tr>
))}
</tbody>
</table>
)}
{remove.error && <p role="alert">{remove.error.message}</p>}
{!draft && (
<button type="button" onClick={() => setDraft({ name: '', policies: [], isNew: true })}>
New entity
</button>
)}
{draft && (
<form onSubmit={submit} aria-label={draft.isNew ? 'New entity' : `Edit ${draft.name}`} className="wide">
<h3>{draft.isNew ? 'New entity' : `Edit ${draft.name}`}</h3>
<label>
Name
<input value={draft.name} disabled={!draft.isNew} onChange={(e) => setDraft({ ...draft, name: e.target.value })} required />
</label>
<PolicyPicker client={client} value={draft.policies} onChange={(policies) => setDraft({ ...draft, policies })} />
{save.error && <p role="alert">{save.error.message}</p>}
<div className="form-actions">
<button type="submit" disabled={save.isPending}>
Save
</button>
<button type="button" onClick={() => setDraft(null)}>
Cancel
</button>
</div>
{draft.entity && (
<fieldset>
<legend>Userpass aliases</legend>
{draft.entity.aliases.map((a) => (
<div key={a.id}>
{a.name}{' '}
<button
type="button"
onClick={async () => {
if (await confirm({ title: `Remove alias “${a.name}”?`, message: 'The userpass user is no longer linked to this entity.', confirmLabel: 'Remove' }))
detach.mutate(a.id)
}}
>
Remove
</button>
</div>
))}
<label>
Link userpass user
<input value={aliasName} onChange={(e) => setAliasName(e.target.value)} placeholder="username" />
</label>
<button
type="button"
disabled={!aliasName || attach.isPending}
onClick={() => draft.entity && attach.mutate({ entity: draft.entity, name: aliasName })}
>
Link
</button>
{(attach.error || detach.error) && <p role="alert">{(attach.error ?? detach.error)?.message}</p>}
</fieldset>
)}
</form>
)}
{accessFor && <AccessEditor key={accessFor} client={client} target={{ kind: 'entity', name: accessFor }} onClose={() => setAccessFor(null)} />}
</section>
)
}