feat(32.2-02): My account item rows with per-row actions; connect screen docs

- AccountContributions renders a section's items as list rows (title,
  subtitle, meta joined with ' · ') with one button per item action, or
  the dashed empty block; sections without items render as before
- item actions confirm with :title filled as text, post to
  /account/contribution-items/{name}/{item}/actions/{action}, reload and
  focus the section heading; 404 toasts item_gone and reloads
- myaccount.items_empty, item_gone, item_action_named in en and pl
- docs/backend/admin-spa.md: the /connect screen and My account item rows
This commit is contained in:
Jakub Zych
2026-11-21 00:52:23 +01:00
parent 6900bd1a72
commit 2708982601
5 changed files with 269 additions and 4 deletions

View File

@@ -1,8 +1,9 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { nextTick, onMounted, ref } from 'vue'
import { Ban } from '@lucide/vue'
import { api } from '../../api/client'
import type { AccountContribution, AccountContributionAction } from '../../api/types'
import { t } from '../../app/i18n'
import type { AccountContribution, AccountContributionAction, AccountItem } from '../../api/types'
import { interpolate, t } from '../../app/i18n'
import Button from '../ui/Button.vue'
import type { useConfirm } from '../ui/confirm'
import { showToast } from '../../state/useToasts'
@@ -13,6 +14,9 @@ import { showToast } from '../../state/useToasts'
// applies to them; labels, descriptions, statuses and messages arrive
// translated and render as text. Nothing renders while the list is empty.
// Shares AccountView's useConfirm() so ConfirmDialog stays at z-[90].
// A section with an Items function also lists the admin's rows, each with
// its item actions (32.2 UI-SPEC C3); a section without one sends items null
// and renders exactly as before.
const props = defineProps<{
confirm: ReturnType<typeof useConfirm>
}>()
@@ -96,6 +100,82 @@ async function post(section: AccountContribution, action: AccountContributionAct
}
}
/** The section's rows, or null for a section without an Items function. */
function itemsOf(section: AccountContribution): AccountItem[] | null {
return Array.isArray(section.items) ? section.items : null
}
/** After an item action the row may be gone: focus moves to its section heading. */
async function focusSection(name: string): Promise<void> {
await nextTick()
const card = document.querySelector<HTMLElement>(`[data-account-contribution="${CSS.escape(name)}"]`)
card?.querySelector<HTMLElement>('h2')?.focus()
}
async function postItem(section: AccountContribution, item: AccountItem, action: AccountContributionAction): Promise<void> {
running.value = `${section.name}/${item.id}/${action.name}`
try {
const result = await api.POST('/account/contribution-items/{name}/{item}/actions/{action}', {
params: { path: { name: section.name, item: item.id, action: action.name } },
})
if (result.data) {
const answer = result.data.data
showToast(answer.message || t('backend::lang.form.action_done'))
const url = safeUrl(answer.url ?? '')
if (url) {
links.value = { ...links.value, [section.name]: url }
window.open(url, '_blank', 'noopener')
}
await load()
await focusSection(section.name)
return
}
const status = result.response.status
if (status === 404) {
showToast(t('backend::lang.myaccount.item_gone'), 'danger')
await load()
await focusSection(section.name)
return
}
if (status === 409) {
showToast(t('backend::lang.myaccount.action_stale'), 'danger')
await load()
await focusSection(section.name)
return
}
if (status === 403) {
showToast(result.error?.error.message || t('backend::lang.list.action_forbidden'), 'danger')
return
}
showToast(result.error?.error.message || t('backend::lang.extension.action_failed'), 'danger')
} catch {
showToast(t('backend::lang.extension.action_failed'), 'danger')
} finally {
running.value = ''
}
}
async function runItem(section: AccountContribution, item: AccountItem, action: AccountContributionAction): Promise<void> {
if (running.value) {
return
}
if (!action.confirm && !action.danger) {
await postItem(section, item, action)
return
}
// :title is filled with the row's title as text, never as HTML.
await props.confirm.ask(
{
message: action.confirm
? interpolate(action.confirm, { title: item.title })
: t('backend::lang.myaccount.action_confirm', { action: action.label }),
confirmLabel: action.label,
danger: action.danger,
},
() => postItem(section, item, action),
)
}
async function run(section: AccountContribution, action: AccountContributionAction): Promise<void> {
if (running.value) {
return
@@ -127,13 +207,61 @@ onMounted(() => {
class="flex flex-col gap-4 rounded-card border border-border bg-surface p-7 shadow-card"
>
<div class="flex flex-col gap-1">
<h2 class="text-[18px] font-bold">{{ section.label }}</h2>
<h2 tabindex="-1" class="text-[18px] font-bold">{{ section.label }}</h2>
<p v-if="section.description" class="max-w-[640px] text-[13px] text-muted">{{ section.description }}</p>
</div>
<p v-if="section.status" data-account-contribution-status class="font-medium">{{ section.status }}</p>
<p v-if="section.error" data-account-contribution-error class="text-[13px] text-muted">
{{ t('backend::lang.myaccount.section_error') }}
</p>
<template v-else-if="itemsOf(section) !== null">
<div
v-if="itemsOf(section)!.length > 0"
role="list"
data-account-items
class="flex flex-col overflow-hidden rounded-inner border border-border"
>
<div
v-for="item in itemsOf(section)!"
:key="item.id"
role="listitem"
:data-account-item="item.id"
class="flex items-start justify-between gap-4 border-t border-border px-4 py-3.5 first:border-t-0 max-sm:flex-col"
>
<div class="flex min-w-0 flex-1 flex-col gap-1">
<span class="font-semibold [overflow-wrap:anywhere]">{{ item.title }}</span>
<span v-if="item.subtitle" class="text-[13px] text-muted [overflow-wrap:anywhere]">{{ item.subtitle }}</span>
<span
v-if="(item.meta ?? []).length > 0"
class="text-[13px] text-muted tabular-nums [overflow-wrap:anywhere]"
>{{ (item.meta ?? []).join(' · ') }}</span
>
</div>
<div v-if="(section.item_actions ?? []).length > 0" class="flex flex-wrap gap-2 max-sm:self-start">
<Button
v-for="action in section.item_actions ?? []"
:key="action.name"
:variant="action.danger ? 'danger' : 'outline'"
size="sm"
:icon="action.danger ? Ban : undefined"
:data-account-item-action="action.name"
:aria-label="t('backend::lang.myaccount.item_action_named', { action: action.label, title: item.title })"
:disabled="running !== ''"
@click="runItem(section, item, action)"
>
{{ action.label }}
</Button>
</div>
</div>
</div>
<div
v-else
data-account-items-empty
class="rounded-inner border border-dashed border-border-strong px-6 py-8 text-center"
>
<p class="text-[13px] text-muted">{{ section.items_empty || t('backend::lang.myaccount.items_empty') }}</p>
</div>
</template>
<a
v-if="safeUrl(links[section.name] ?? '')"
data-account-contribution-url

View File

@@ -0,0 +1,121 @@
// Smoke tests of My account item rows (Phase 32.2 UI-SPEC C3): a section
// without Items renders as before, an empty Items list renders the dashed
// empty block, and a danger item action confirms with :title filled and
// posts the item route. Fixtures are neutral acme.* data.
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { enableAutoUnmount, flushPromises } from '@vue/test-utils'
import { setBundle } from '../../src/app/i18n'
import { toasts } from '../../src/state/useToasts'
import { langFixture } from '../fixtures/typed'
import { API, mountApp, profile, requestsTo, resetState, type Route } from '../helpers'
const CONTRIBUTIONS = `${API}/account/contributions`
// openapi-fetch encodes each path parameter once.
const REVOKE = `${API}/account/contribution-items/agents/client%3A1/actions/revoke`
const envelope = (data: unknown) => ({ body: { data, meta: {} } })
const plain = {
name: 'site_link',
label: 'Site account',
description: '',
status: 'Not linked',
actions: [{ name: 'open', label: 'Link account', confirm: '', danger: false }],
items: null,
item_actions: null,
items_empty: '',
}
const agents = {
name: 'agents',
label: 'Connected agents',
description: '',
status: 'Connector address: https://example.test/mcp',
actions: [],
items: [
{ id: 'client:1', title: 'Acme Agent', subtitle: 'Read the admin, Acme', meta: ['Connected 2026-11-01', 'Never used'] },
],
item_actions: [{ name: 'revoke', label: 'Revoke', confirm: 'Revoke the connection “:title”?', danger: true }],
items_empty: '',
}
async function openAccount(routes: Record<string, Route>) {
return mountApp(
'/account',
{
[`GET ${API}/auth/me`]: envelope(profile.data),
[`GET ${API}/account/tokens`]: envelope([]),
...routes,
},
{ attach: true },
)
}
beforeEach(() => {
resetState()
setBundle(
{
...langFixture.data,
'backend::lang.myaccount.items_empty': { other: 'Nothing here yet.' },
'backend::lang.myaccount.item_action_named': { other: ':action: :title' },
},
'en',
)
})
afterEach(() => {
document.body.innerHTML = ''
vi.restoreAllMocks()
})
enableAutoUnmount(afterEach)
describe('My account item rows (UI-SPEC C3)', () => {
it('a section without items renders no list and no empty block', async () => {
const { wrapper } = await openAccount({ [`GET ${CONTRIBUTIONS}`]: envelope([plain]) })
const card = wrapper.find('[data-account-contribution="site_link"]')
expect(card.find('[data-account-contribution-status]').text()).toBe('Not linked')
expect(card.find('[data-account-contribution-action="open"]').exists()).toBe(true)
expect(card.find('[role="list"]').exists()).toBe(false)
expect(card.find('[data-account-items-empty]').exists()).toBe(false)
})
it('a section with items [] renders the empty block under the status line', async () => {
const { wrapper } = await openAccount({ [`GET ${CONTRIBUTIONS}`]: envelope([{ ...agents, items: [] }]) })
const card = wrapper.find('[data-account-contribution="agents"]')
expect(card.find('[data-account-contribution-status]').exists()).toBe(true)
expect(card.find('[data-account-items-empty]').text()).toBe('Nothing here yet.')
expect(card.find('[data-account-item]').exists()).toBe(false)
})
it('a danger item action confirms with :title filled and posts the item route', async () => {
let loads = 0
const { wrapper, calls } = await openAccount({
[`GET ${CONTRIBUTIONS}`]: () => {
loads++
return envelope([loads === 1 ? agents : { ...agents, items: [] }])
},
[`POST ${REVOKE}`]: envelope({ message: 'Connection revoked', url: '' }),
})
const row = wrapper.find('[data-account-item="client:1"]')
expect(row.attributes('role')).toBe('listitem')
expect(row.text()).toContain('Acme Agent')
expect(row.text()).toContain('Connected 2026-11-01 · Never used')
const button = row.find('[data-account-item-action="revoke"]')
expect(button.attributes('aria-label')).toBe('Revoke: Acme Agent')
await button.trigger('click')
await flushPromises()
const dialog = document.body.querySelector<HTMLElement>('[data-confirm-dialog]')
expect(dialog?.textContent).toContain('Revoke the connection “Acme Agent”?')
expect(requestsTo(calls, 'POST', REVOKE)).toHaveLength(0)
dialog!.querySelector<HTMLButtonElement>('[data-action="confirm"]')!.click()
await flushPromises()
expect(requestsTo(calls, 'POST', REVOKE)).toHaveLength(1)
expect(toasts.value.map((toast) => toast.text)).toEqual(['Connection revoked'])
expect(requestsTo(calls, 'GET', CONTRIBUTIONS)).toHaveLength(2)
expect(wrapper.find('[data-account-items-empty]').exists()).toBe(true)
expect(document.activeElement?.textContent).toBe('Connected agents')
})
})