From 336664684b58892ad34a662b087be14beb75fcf2 Mon Sep 17 00:00:00 2001 From: Jakub Zych Date: Tue, 24 Nov 2026 19:40:19 +0100 Subject: [PATCH] feat(36-02): integrate ScopeAccessControl and in-dialog ScopePicker swap into ApiTokenCreateDialog --- .../account/ApiTokenCreateDialog.vue | 624 ++++++++++-------- 1 file changed, 345 insertions(+), 279 deletions(-) diff --git a/admin/src/components/account/ApiTokenCreateDialog.vue b/admin/src/components/account/ApiTokenCreateDialog.vue index a4b13e22..a80e6db7 100644 --- a/admin/src/components/account/ApiTokenCreateDialog.vue +++ b/admin/src/components/account/ApiTokenCreateDialog.vue @@ -10,19 +10,25 @@ import { RadioGroupItem, RadioGroupRoot, } from 'reka-ui' -import { Check, CircleAlert, Copy, Info, LoaderCircle, TriangleAlert, X } from '@lucide/vue' +import { ArrowLeft, Check, CircleAlert, Copy, LoaderCircle, TriangleAlert, X } from '@lucide/vue' import { api } from '../../api/client' import type { AdminAPITokenScope } from '../../api/types' import { shortcutHint } from '../../app/hotkeys' import { t } from '../../app/i18n' +import type { AccessTier } from '../../app/scopeCatalog' import { controlClass } from '../form/control' import { fieldErrors, focusField } from '../form/formState' +import ScopeAccessControl from '../scopes/ScopeAccessControl.vue' +import ScopePicker from '../scopes/ScopePicker.vue' +import { useScopeSelection } from '../scopes/useScopeSelection' import Button from '../ui/Button.vue' import { useConfirm } from '../ui/confirm' import { showToast } from '../../state/useToasts' -// Two-step create dialog (16.3 UI-SPEC §4-5): form, then show-once plaintext. -// The plaintext lives only in a local ref and is cleared on close and unmount. +// Two-step create dialog (16.3 UI-SPEC §4-5 + Phase 36 UI-SPEC C5): form +// (with in-dialog ScopePicker view swap at fixed max-w-[560px]), then +// show-once plaintext. The plaintext lives only in a local ref and is cleared +// on close and unmount. const props = defineProps<{ open: boolean confirm: ReturnType @@ -44,7 +50,6 @@ const EXPIRY_OPTIONS = [ const step = ref<1 | 2>(1) const name = ref('') const expires = ref('90d') -const checked = ref>(new Set()) const scopes = ref([]) const scopesLoading = ref(false) const scopesFailed = ref(false) @@ -60,10 +65,32 @@ let copyTimer: ReturnType | null = null const nameInput = ref(null) const plaintextInput = ref(null) const copyButton = ref(null) +const scopeAccessRef = ref | null>(null) +const scopePickerRef = ref | null>(null) + +const { + availableTiers, + tier, + custom, + pickerOpen, + draftAdminRead, + draftScopes, + submittedScopes, + canSubmitScopes, + reset: resetScopeSelection, + selectTier, + openPicker, + applyPicker, + discardPicker, +} = useScopeSelection(scopes, 'token') -const starChecked = computed(() => checked.value.has('*')) const canSubmit = computed( - () => !scopesLoading.value && !scopesFailed.value && scopes.value.length > 0 && checked.value.size > 0 && !busy.value, + () => + !scopesLoading.value && + !scopesFailed.value && + scopes.value.length > 0 && + canSubmitScopes.value && + !busy.value, ) function pad(value: number): string { @@ -100,7 +127,7 @@ function resetForm(): void { step.value = 1 name.value = '' expires.value = '90d' - checked.value = new Set() + resetScopeSelection([]) errors.value = {} busy.value = false scopesFailed.value = false @@ -115,11 +142,13 @@ async function loadScopes(): Promise { scopesLoading.value = true scopesFailed.value = false scopes.value = [] + resetScopeSelection([]) try { const result = await api.GET('/account/tokens/scopes') if (result.data) { const list = result.data.data ?? [] scopes.value = list + resetScopeSelection(list) scopesFailed.value = list.length === 0 return } @@ -139,28 +168,13 @@ watch( void loadScopes() } else { clearPlaintext() + resetScopeSelection([]) step.value = 1 } }, ) -function toggleScope(scopeName: string, on: boolean): void { - const next = new Set(checked.value) - if (scopeName === '*') { - if (on) { - next.clear() - next.add('*') - } else { - next.delete('*') - } - } else if (!starChecked.value) { - if (on) { - next.add(scopeName) - } else { - next.delete(scopeName) - } - } - checked.value = next +function clearScopeError(): void { if (errors.value.scopes) { const nextErrors = { ...errors.value } delete nextErrors.scopes @@ -168,15 +182,39 @@ function toggleScope(scopeName: string, on: boolean): void { } } -function scopeDisabled(scopeName: string): boolean { - return starChecked.value && scopeName !== '*' +async function onSelectTier(next: AccessTier): Promise { + if (busy.value) { + return + } + const wasOpen = pickerOpen.value + selectTier(next) + clearScopeError() + if (!wasOpen && pickerOpen.value) { + await nextTick() + scopePickerRef.value?.focusSearch() + } } -function selectedScopes(): string[] { - if (starChecked.value) { - return ['*'] +async function onPickerOpen(): Promise { + if (busy.value) { + return } - return scopes.value.map((scope) => scope.name).filter((scopeName) => checked.value.has(scopeName)) + openPicker() + await nextTick() + scopePickerRef.value?.focusSearch() +} + +async function onPickerApply(): Promise { + applyPicker() + clearScopeError() + await nextTick() + scopeAccessRef.value?.focusPrimaryControl() +} + +async function onPickerCancel(): Promise { + discardPicker() + await nextTick() + scopeAccessRef.value?.focusPrimaryControl() } function nameDescribedBy(): string | undefined { @@ -194,11 +232,7 @@ async function focusFirstInvalid(): Promise { return } if (errors.value.scopes) { - const first = scopes.value.find((scope) => !scopeDisabled(scope.name)) - const el = first - ? document.querySelector(`[data-scope="${CSS.escape(first.name)}"] input[type="checkbox"]`) - : null - el?.focus() + scopeAccessRef.value?.focusPrimaryControl() return } if (errors.value.expires) { @@ -207,14 +241,15 @@ async function focusFirstInvalid(): Promise { } async function onSubmit(): Promise { - if (busy.value || !canSubmit.value || step.value !== 1) { + if (busy.value || !canSubmit.value || step.value !== 1 || pickerOpen.value) { return } busy.value = true errors.value = {} + let invalid = false try { const result = await api.POST('/account/tokens', { - body: { name: name.value, expires: expires.value, scopes: selectedScopes() }, + body: { name: name.value, expires: expires.value, scopes: submittedScopes.value }, }) if (result.data) { plaintext.value = result.data.data.token @@ -226,15 +261,18 @@ async function onSubmit(): Promise { } if (result.response.status === 422) { errors.value = fieldErrors(result.error?.error.details) - await focusFirstInvalid() - return + invalid = true + } else { + showToast(t('backend::lang.api_tokens.create_failed'), 'danger') } - showToast(t('backend::lang.api_tokens.create_failed'), 'danger') } catch { showToast(t('backend::lang.api_tokens.create_failed'), 'danger') } finally { busy.value = false } + if (invalid) { + await focusFirstInvalid() + } } function markCopied(): void { @@ -280,6 +318,7 @@ function onPlaintextFocus(event: FocusEvent): void { async function closeDialog(): Promise { clearPlaintext() + resetScopeSelection([]) setOpen(false) } @@ -314,6 +353,10 @@ function onOpenAutoFocus(event: Event): void { copyButton.value?.querySelector('button')?.focus() ?? copyButton.value?.focus() return } + if (pickerOpen.value) { + scopePickerRef.value?.focusSearch() + return + } nameInput.value?.focus() } @@ -333,6 +376,11 @@ function onEscapeKey(event: Event): void { event.preventDefault() return } + if (step.value === 1 && pickerOpen.value) { + event.preventDefault() + void onPickerCancel() + return + } if (step.value === 2) { event.preventDefault() void requestClose() @@ -356,261 +404,279 @@ onBeforeUnmount(() => { data-token-dialog data-dialog-content aria-modal="true" - class="fixed top-1/2 left-1/2 z-50 flex max-h-[calc(100vh-32px)] w-[calc(100%-32px)] max-w-[560px] -translate-x-1/2 -translate-y-1/2 flex-col gap-4 rounded-modal bg-surface p-6 text-text shadow-pop" + class="fixed top-1/2 left-1/2 z-50 flex max-h-[calc(100vh-32px)] w-[calc(100%-32px)] max-w-[560px] -translate-x-1/2 -translate-y-1/2 flex-col overflow-hidden rounded-modal bg-surface text-text shadow-pop" @open-auto-focus="onOpenAutoFocus" @close-auto-focus="onCloseAutoFocus" @pointer-down-outside="onPointerOutside" @interact-outside="onPointerOutside" @escape-key-down="onEscapeKey" > -
-
- {{ - step === 1 - ? t('backend::lang.api_tokens.create_title') - : t('backend::lang.api_tokens.created_title') - }} - {{ - t('backend::lang.api_tokens.create_description') - }} - - -
- -
- -
-
-
- - -

- {{ t('backend::lang.api_tokens.name_comment') }} -

-
+ -
-
- - -
- - - -
+
+
+ {{ + step === 1 + ? t('backend::lang.api_tokens.create_title') + : t('backend::lang.api_tokens.created_title') + }} + {{ + t('backend::lang.api_tokens.create_description') + }} + -
+
-
- + + +
+
+ + +

+ {{ t('backend::lang.api_tokens.name_comment') }} +

+
+

+

+
+
+ +
+ {{ + t('backend::lang.api_tokens.expires') + }} + + + {{ t(option.label) }} + + +

{{ expiryPreview }}

+
+ +
+ + + +
+

+

+
+
+
+ +
+ + +
+ + +
+
+ + +
+ + + + + {{ announce }} +
+
+
+ +