feat(36-02): add shared scope picker module and wire ConnectView

This commit is contained in:
Jakub Zych
2026-11-24 19:40:02 +01:00
parent 3fd804e8c8
commit 6d7325e6d8
6 changed files with 1330 additions and 94 deletions

View File

@@ -0,0 +1,210 @@
<script setup lang="ts">
import { computed, ref } from 'vue'
import { RadioGroupItem, RadioGroupRoot } from 'reka-ui'
import { Info, Lock, SlidersHorizontal } from '@lucide/vue'
import type { AdminAPITokenScope } from '../../api/types'
import {
buildSummaryModel,
partitionCatalog,
type AccessTier,
type CustomScopeSelection,
} from '../../app/scopeCatalog'
import { t, tc } from '../../app/i18n'
// Shared 3-tier access selector + compact effective-access summary box
// (Phase 36 UI-SPEC C2). Uses only semantic Direction C v2 Tailwind utilities
// so ApiTokenCreateDialog inherits light/dark automatically while ConnectView
// pins light-literal tokens via .connect-light-surface.
const props = defineProps<{
catalog: readonly AdminAPITokenScope[]
availableTiers: readonly AccessTier[]
tier: AccessTier
custom: CustomScopeSelection
disabled?: boolean
invalid?: boolean
idPrefix: string
}>()
const emit = defineEmits<{
'update:tier': [tier: AccessTier]
customize: []
}>()
const TIER_LABELS: Record<AccessTier, string> = {
full: 'backend::lang.api_tokens.tier_full',
read_only: 'backend::lang.api_tokens.tier_read_only',
custom: 'backend::lang.api_tokens.tier_custom',
}
const rootEl = ref<HTMLElement | null>(null)
const customizeButton = ref<HTMLButtonElement | null>(null)
const partitioned = computed(() => partitionCatalog(props.catalog))
const fullScope = computed(() => partitioned.value.fullScope)
const adminReadScope = computed(() => partitioned.value.adminReadScope)
const summary = computed(() => buildSummaryModel(props.catalog, props.custom))
const adminReadActive = computed(() => props.custom.adminRead && adminReadScope.value !== null)
const customSubtitle = computed(() => {
if (!adminReadActive.value && summary.value.selectedPluginCount === 0) {
return t('backend::lang.api_tokens.scope_summary_empty')
}
if (adminReadActive.value && summary.value.selectedWriteCount === 0) {
return t('backend::lang.api_tokens.scope_summary_admin_read_only')
}
if (adminReadActive.value) {
return t('backend::lang.api_tokens.scope_summary_admin_read_plus', {
scopes: tc('backend::lang.api_tokens.scope_write_count', summary.value.selectedWriteCount),
plugins: tc('backend::lang.api_tokens.scope_plugin_count', summary.value.distinctPluginCount),
})
}
return t('backend::lang.api_tokens.scope_summary_line', {
scopes: tc('backend::lang.api_tokens.scope_count', summary.value.selectedPluginCount),
plugins: tc('backend::lang.api_tokens.scope_plugin_count', summary.value.distinctPluginCount),
})
})
function onTierUpdate(value: unknown): void {
if (value === 'full' || value === 'read_only' || value === 'custom') {
emit('update:tier', value)
}
}
function focusPrimaryControl(): void {
if (customizeButton.value) {
customizeButton.value.focus()
return
}
const checked = rootEl.value?.querySelector<HTMLElement>('[data-access-tier][data-state="checked"]')
if (checked) {
checked.focus()
return
}
rootEl.value?.querySelector<HTMLElement>('[data-access-tier]')?.focus()
}
defineExpose({ focusPrimaryControl })
</script>
<template>
<div ref="rootEl" class="flex min-w-0 flex-col gap-1.5">
<span :id="`${idPrefix}-access-label`" class="font-semibold text-text">{{
t('backend::lang.api_tokens.access_level')
}}</span>
<RadioGroupRoot
v-if="availableTiers.length > 1"
:model-value="tier"
orientation="horizontal"
:disabled="disabled"
:aria-labelledby="`${idPrefix}-access-label`"
data-access-tiers
:class="availableTiers.length === 3 ? 'grid-cols-3' : 'grid-cols-2'"
class="grid gap-1 rounded-inner border border-border bg-subtle p-1"
@update:model-value="onTierUpdate"
>
<RadioGroupItem
v-for="item in availableTiers"
:key="item"
:value="item"
:data-access-tier="item"
class="inline-flex min-h-[38px] items-center justify-center rounded-tab px-2 py-2 text-center text-[13px] text-muted transition-colors duration-150 ease-out hover:text-text data-[state=checked]:bg-surface data-[state=checked]:text-text data-[state=checked]:shadow-tab data-[state=checked]:font-semibold disabled:cursor-not-allowed disabled:opacity-60"
>
{{ t(TIER_LABELS[item]) }}
</RadioGroupItem>
</RadioGroupRoot>
<div
data-scope-summary
role="region"
:aria-labelledby="`${idPrefix}-access-label`"
:aria-invalid="invalid ? 'true' : undefined"
:class="invalid ? 'border-danger' : 'border-border'"
class="flex flex-col gap-2 rounded-inner border bg-subtle p-4"
>
<template v-if="tier === 'full' && fullScope">
<div class="flex flex-wrap items-center gap-1">
<span class="font-semibold text-text">{{ fullScope.label }}</span>
<span class="font-mono text-[13px] text-muted">*</span>
</div>
<p class="text-[13px] text-muted [overflow-wrap:anywhere]">{{ fullScope.description }}</p>
<p data-scope-all-note class="flex items-center gap-2 text-[13px] text-muted">
<Info :size="14" class="shrink-0" aria-hidden="true" />
<span>{{ t('backend::lang.api_tokens.scope_all_includes') }}</span>
</p>
</template>
<template v-else-if="tier === 'read_only' && adminReadScope">
<div class="flex flex-wrap items-center gap-1">
<span class="font-semibold text-text">{{ adminReadScope.label }}</span>
<span class="font-mono text-[13px] text-muted">admin:read</span>
<span
class="inline-flex h-6 items-center rounded-pill border border-border bg-surface px-2.5 text-[13px] font-semibold whitespace-nowrap text-muted"
>{{ t('backend::lang.api_tokens.read_only') }}</span
>
</div>
<p class="text-[13px] text-muted [overflow-wrap:anywhere]">{{ adminReadScope.description }}</p>
</template>
<template v-else-if="tier === 'custom'">
<div class="flex flex-wrap items-center justify-between gap-2">
<p data-scope-summary-count class="text-[13px] text-muted tabular-nums [overflow-wrap:anywhere]">
{{ customSubtitle }}
</p>
<button
ref="customizeButton"
type="button"
data-action="customize-scopes"
:disabled="disabled"
class="inline-flex h-[38px] shrink-0 items-center justify-center gap-2 rounded-control border border-border-strong bg-surface px-4 text-[14px] font-semibold whitespace-nowrap text-text transition-colors duration-150 ease-out hover:bg-hover disabled:cursor-not-allowed disabled:opacity-60"
@click="emit('customize')"
>
<SlidersHorizontal :size="16" aria-hidden="true" />
<span>{{ t('backend::lang.api_tokens.scope_customize') }}</span>
</button>
</div>
<div v-if="summary.visible.length > 0" data-scope-summary-chips class="flex flex-wrap gap-2">
<template v-for="chip in summary.visible" :key="chip.key">
<span
v-if="chip.kind === 'admin_read'"
data-summary-chip="admin:read"
:title="adminReadScope?.label"
class="inline-flex h-6 items-center gap-1 rounded-pill border border-border-strong bg-surface px-2 font-mono text-[13px] font-semibold whitespace-nowrap text-text"
>
<Lock :size="14" class="shrink-0 text-muted" aria-hidden="true" />
<span>admin:read</span>
</span>
<span
v-else-if="chip.kind === 'write'"
:data-summary-chip="chip.name"
:title="chip.name"
class="inline-flex h-6 max-w-full items-center rounded-pill border border-border-strong bg-surface px-2 text-[13px] font-semibold text-text"
>
<span class="truncate">{{ chip.label }}</span>
</span>
<span
v-else
:data-summary-chip="chip.name"
:title="chip.name"
class="inline-flex h-6 max-w-full items-center rounded-pill border border-border bg-surface px-2 text-[13px] font-normal text-muted"
>
<span class="truncate">{{ chip.label }}</span>
</span>
</template>
<button
v-if="summary.overflowCount > 0"
type="button"
data-action="customize-scopes-more"
:disabled="disabled"
class="inline-flex h-6 items-center rounded-pill border border-dashed border-border-strong bg-surface px-2 text-[13px] font-semibold tabular-nums whitespace-nowrap text-text hover:bg-hover disabled:cursor-not-allowed disabled:opacity-60"
@click="emit('customize')"
>
{{ t('backend::lang.api_tokens.scope_more', { count: summary.overflowCount }) }}
</button>
</div>
</template>
</div>
</div>
</template>

View File

@@ -0,0 +1,529 @@
<script setup lang="ts">
import { computed, nextTick, onMounted, ref } from 'vue'
import { ChevronDown, ChevronRight, Lock, Search, X } from '@lucide/vue'
import type { AdminAPITokenScope } from '../../api/types'
import {
buildSummaryModel,
computeDefaultExpanded,
groupPluginScopes,
isEffectivelySelected,
matchesScopeQuery,
partitionCatalog,
type CustomScopeSelection,
type ScopeFilter,
type ScopeGroup,
} from '../../app/scopeCatalog'
import { currentLocale, t, tc } from '../../app/i18n'
import { controlClass } from '../form/control'
// Surface-agnostic sticky-header / flat-accordion / sticky-footer scope picker
// (Phase 36 UI-SPEC C3). Hosted inside ConnectView's light-literal modal and
// ApiTokenCreateDialog's fixed-width swapped view.
const props = defineProps<{
catalog: readonly AdminAPITokenScope[]
draftAdminRead: boolean
draftScopes: ReadonlySet<string>
cancelLabel: string
disabled?: boolean
}>()
const emit = defineEmits<{
'update:draftAdminRead': [value: boolean]
'update:draftScopes': [value: Set<string>]
apply: []
cancel: []
}>()
interface VisibleScopeGroup {
key: string
panelId: string
label: string
allScopes: AdminAPITokenScope[]
visibleScopes: AdminAPITokenScope[]
selectedCount: number
canRead: boolean
canAll: boolean
canClear: boolean
}
const query = ref('')
const filter = ref<ScopeFilter>('all')
const expanded = ref<Set<string>>(new Set())
const searchInput = ref<HTMLInputElement | null>(null)
const partitioned = computed(() => partitionCatalog(props.catalog))
const hasAdminRead = computed(() => partitioned.value.adminReadScope !== null)
const pluginScopes = computed(() => partitioned.value.pluginScopes)
const adminReadActive = computed(() => props.draftAdminRead && hasAdminRead.value)
const draftSelection = computed<CustomScopeSelection>(() => ({
adminRead: adminReadActive.value,
scopes: props.draftScopes,
}))
const allGroups = computed<ScopeGroup[]>(() =>
groupPluginScopes(pluginScopes.value, currentLocale.value, t('backend::lang.permissioneditor.other')),
)
const isSearching = computed(() => query.value.trim() !== '')
const allCount = computed(() => pluginScopes.value.length)
const selectedCount = computed(
() => pluginScopes.value.filter((scope) => isEffectivelySelected(scope, draftSelection.value)).length,
)
const readOnlyCount = computed(() => pluginScopes.value.filter((scope) => scope.read_only).length)
const filterOptions = computed(() => [
{
id: 'all' as const,
label: t('backend::lang.api_tokens.scope_filter_all', { count: allCount.value }),
},
{
id: 'selected' as const,
label: t('backend::lang.api_tokens.scope_filter_selected', { count: selectedCount.value }),
},
{
id: 'read_only' as const,
label: t('backend::lang.api_tokens.scope_filter_read_only', { count: readOnlyCount.value }),
},
])
function isLocked(scope: AdminAPITokenScope): boolean {
return adminReadActive.value && scope.read_only
}
function isEffectiveSelected(scope: AdminAPITokenScope): boolean {
return isEffectivelySelected(scope, draftSelection.value)
}
const visibleGroups = computed<VisibleScopeGroup[]>(() => {
const result: VisibleScopeGroup[] = []
allGroups.value.forEach((group, index) => {
const visibleScopes = group.scopes.filter((scope) => {
if (filter.value === 'selected' && !isEffectiveSelected(scope)) {
return false
}
if (filter.value === 'read_only' && !scope.read_only) {
return false
}
if (isSearching.value && !matchesScopeQuery(scope, group.label, query.value)) {
return false
}
return true
})
if (visibleScopes.length === 0) {
return
}
const readOnlyScopes = group.scopes.filter((scope) => scope.read_only)
const allReadOnlySelected = readOnlyScopes.every((scope) => props.draftScopes.has(scope.name))
const unlockedScopes = group.scopes.filter((scope) => !isLocked(scope))
const allUnlockedSelected = unlockedScopes.every((scope) => props.draftScopes.has(scope.name))
const unlockedSelected = unlockedScopes.filter((scope) => props.draftScopes.has(scope.name))
result.push({
key: group.key,
panelId: `scope-group-panel-${index}`,
label: group.label,
allScopes: group.scopes,
visibleScopes,
selectedCount: group.scopes.filter((scope) => isEffectiveSelected(scope)).length,
canRead: !props.disabled && !adminReadActive.value && readOnlyScopes.length > 0 && !allReadOnlySelected,
canAll: !props.disabled && unlockedScopes.length > 0 && !allUnlockedSelected,
canClear: !props.disabled && unlockedSelected.length > 0,
})
})
return result
})
const draftSummary = computed(() => buildSummaryModel(props.catalog, draftSelection.value))
const canApply = computed(() => adminReadActive.value || draftSummary.value.selectedPluginCount > 0)
const footerSummary = computed(() => {
if (!canApply.value) {
return t('backend::lang.api_tokens.scope_picker_none_selected')
}
if (adminReadActive.value && draftSummary.value.selectedWriteCount === 0) {
return t('backend::lang.api_tokens.scope_summary_admin_read_only')
}
if (adminReadActive.value) {
return t('backend::lang.api_tokens.scope_picker_selected_admin_read', {
scopes: tc('backend::lang.api_tokens.scope_write_count', draftSummary.value.selectedWriteCount),
})
}
return t('backend::lang.api_tokens.scope_picker_selected', {
scopes: tc('backend::lang.api_tokens.scope_count', draftSummary.value.selectedPluginCount),
})
})
const emptyState = computed(() => {
if (isSearching.value) {
return {
kind: 'search' as const,
title: t('backend::lang.api_tokens.scope_empty_search_title'),
body: t('backend::lang.api_tokens.scope_empty_search_body', { query: query.value.trim() }),
actionLabel: t('backend::lang.api_tokens.scope_search_clear'),
}
}
if (filter.value === 'selected') {
return {
kind: 'filter' as const,
title: t('backend::lang.api_tokens.scope_empty_selected_title'),
body: t('backend::lang.api_tokens.scope_empty_selected_body'),
actionLabel: t('backend::lang.api_tokens.scope_filter_reset'),
}
}
return {
kind: 'filter' as const,
title: t('backend::lang.api_tokens.scope_empty_readonly_title'),
body: t('backend::lang.api_tokens.scope_empty_readonly_body'),
actionLabel: t('backend::lang.api_tokens.scope_filter_reset'),
}
})
function isGroupExpanded(key: string): boolean {
return isSearching.value || expanded.value.has(key)
}
function toggleGroup(key: string): void {
if (isSearching.value) {
return
}
const next = new Set(expanded.value)
if (next.has(key)) {
next.delete(key)
} else {
next.add(key)
}
expanded.value = next
}
function toggleScope(name: string, on: boolean): void {
const next = new Set(props.draftScopes)
if (on) {
next.add(name)
} else {
next.delete(name)
}
emit('update:draftScopes', next)
}
function selectGroupRead(group: VisibleScopeGroup): void {
if (!group.canRead) {
return
}
const next = new Set(props.draftScopes)
for (const scope of group.allScopes) {
if (scope.read_only) {
next.add(scope.name)
}
}
emit('update:draftScopes', next)
}
function selectGroupAll(group: VisibleScopeGroup): void {
if (!group.canAll) {
return
}
const next = new Set(props.draftScopes)
for (const scope of group.allScopes) {
if (!isLocked(scope)) {
next.add(scope.name)
}
}
emit('update:draftScopes', next)
}
function clearGroup(group: VisibleScopeGroup): void {
if (!group.canClear) {
return
}
const next = new Set(props.draftScopes)
for (const scope of group.allScopes) {
if (!isLocked(scope)) {
next.delete(scope.name)
}
}
emit('update:draftScopes', next)
}
function clearSearch(): void {
query.value = ''
void nextTick(() => {
searchInput.value?.focus()
})
}
function resetFilter(): void {
filter.value = 'all'
}
function focusSearch(): void {
searchInput.value?.focus()
}
onMounted(() => {
expanded.value = computeDefaultExpanded(
allGroups.value,
pluginScopes.value.length,
draftSelection.value,
false,
)
})
defineExpose({ focusSearch })
</script>
<template>
<div data-scope-picker class="flex min-h-0 flex-1 flex-col">
<!-- Sticky search, filter pills, and opt-in admin:read toggle -->
<div class="flex shrink-0 flex-col gap-2 border-b border-border bg-surface p-4">
<div class="relative flex items-center">
<Search :size="16" class="pointer-events-none absolute left-3 text-muted" aria-hidden="true" />
<input
ref="searchInput"
v-model="query"
type="search"
data-scope-search
autocomplete="off"
:aria-label="t('backend::lang.api_tokens.scope_search_label')"
:placeholder="t('backend::lang.api_tokens.scope_search_placeholder')"
:class="controlClass(false)"
class="h-input pl-10 pr-10"
/>
<button
v-if="query !== ''"
type="button"
data-action="clear-scope-search"
:aria-label="t('backend::lang.api_tokens.scope_search_clear')"
class="absolute right-2 flex size-8 items-center justify-center rounded-tab text-muted hover:bg-hover hover:text-text"
@click="clearSearch"
>
<X :size="16" aria-hidden="true" />
</button>
</div>
<div
role="group"
:aria-label="t('backend::lang.api_tokens.scope_filter_label')"
class="inline-flex flex-wrap gap-1 self-start rounded-inner border border-border bg-subtle p-1"
>
<button
v-for="option in filterOptions"
:key="option.id"
type="button"
:data-scope-filter="option.id"
:aria-pressed="filter === option.id"
:class="
filter === option.id
? 'bg-surface font-semibold text-text shadow-tab'
: 'text-muted hover:text-text'
"
class="inline-flex h-8 items-center rounded-tab px-4 text-[13px] tabular-nums whitespace-nowrap transition-colors duration-150 ease-out"
@click="filter = option.id"
>
{{ option.label }}
</button>
</div>
<div
v-if="hasAdminRead"
data-admin-read-toggle
class="rounded-inner border border-border bg-subtle px-4 py-2"
>
<label class="flex min-h-[44px] cursor-pointer items-start gap-3 py-2">
<input
type="checkbox"
data-admin-read-checkbox
class="mt-0.5 size-5 shrink-0 rounded-[6px] accent-primary"
:checked="draftAdminRead"
:disabled="disabled"
@change="emit('update:draftAdminRead', ($event.target as HTMLInputElement).checked)"
/>
<span class="flex min-w-0 flex-col">
<span class="flex flex-wrap items-center gap-1">
<span class="font-semibold text-text">{{
t('backend::lang.api_tokens.scope_also_admin_read')
}}</span>
<span class="font-mono text-[13px] text-muted">admin:read</span>
</span>
<span class="text-[13px] text-muted [overflow-wrap:anywhere]">{{
t('backend::lang.api_tokens.scope_also_admin_read_hint')
}}</span>
</span>
</label>
</div>
</div>
<!-- Scrollable accordion body -->
<div class="min-h-0 flex-1 overflow-y-auto p-4">
<div
v-if="visibleGroups.length === 0"
data-scope-picker-empty
class="flex flex-col items-center gap-2 rounded-inner border border-dashed border-border-strong px-6 py-8 text-center"
>
<p class="font-semibold text-text">{{ emptyState.title }}</p>
<p class="text-[13px] text-muted [overflow-wrap:anywhere]">{{ emptyState.body }}</p>
<button
v-if="emptyState.kind === 'search'"
type="button"
data-action="reset-scope-search"
class="mt-2 inline-flex h-[38px] items-center justify-center rounded-control border border-border-strong bg-surface px-4 font-semibold text-text transition-colors duration-150 ease-out hover:bg-hover"
@click="clearSearch"
>
{{ emptyState.actionLabel }}
</button>
<button
v-else
type="button"
data-action="reset-scope-filter"
class="mt-2 inline-flex h-[38px] items-center justify-center rounded-control border border-border-strong bg-surface px-4 font-semibold text-text transition-colors duration-150 ease-out hover:bg-hover"
@click="resetFilter"
>
{{ emptyState.actionLabel }}
</button>
</div>
<div v-else class="flex flex-col gap-2">
<section
v-for="group in visibleGroups"
:key="group.key"
data-scope-group
class="overflow-hidden rounded-inner border border-border bg-surface"
>
<div class="flex flex-wrap items-center justify-between gap-2 bg-subtle px-4 py-2">
<button
type="button"
data-scope-group-toggle
:aria-expanded="isGroupExpanded(group.key)"
:aria-controls="group.panelId"
class="flex min-h-[38px] min-w-0 flex-1 items-center gap-2 text-left"
@click="toggleGroup(group.key)"
>
<ChevronDown
v-if="isGroupExpanded(group.key)"
:size="16"
class="shrink-0 text-muted"
aria-hidden="true"
/>
<ChevronRight v-else :size="16" class="shrink-0 text-muted" aria-hidden="true" />
<span class="font-semibold text-text [overflow-wrap:anywhere]">{{ group.label }}</span>
<span
data-scope-group-count
:class="group.selectedCount > 0 ? 'border-border-strong text-text' : 'border-border text-muted'"
class="inline-flex h-6 shrink-0 items-center rounded-pill border bg-surface px-2 text-[13px] font-semibold tabular-nums"
>
{{ group.selectedCount }}/{{ group.allScopes.length }}
</span>
</button>
<div class="flex shrink-0 items-center gap-1">
<button
type="button"
data-group-action="read"
:disabled="!group.canRead"
:aria-label="t('backend::lang.api_tokens.group_select_read_named', { group: group.label })"
class="inline-flex h-8 items-center rounded-tab px-2 text-[13px] font-semibold text-muted transition-colors duration-150 ease-out hover:bg-hover hover:text-text disabled:cursor-not-allowed disabled:opacity-40"
@click="selectGroupRead(group)"
>
{{ t('backend::lang.api_tokens.group_select_read') }}
</button>
<button
type="button"
data-group-action="all"
:disabled="!group.canAll"
:aria-label="t('backend::lang.api_tokens.group_select_all_named', { group: group.label })"
class="inline-flex h-8 items-center rounded-tab px-2 text-[13px] font-semibold text-muted transition-colors duration-150 ease-out hover:bg-hover hover:text-text disabled:cursor-not-allowed disabled:opacity-40"
@click="selectGroupAll(group)"
>
{{ t('backend::lang.api_tokens.group_select_all') }}
</button>
<button
type="button"
data-group-action="clear"
:disabled="!group.canClear"
:aria-label="t('backend::lang.api_tokens.group_clear_named', { group: group.label })"
class="inline-flex h-8 items-center rounded-tab px-2 text-[13px] font-semibold text-muted transition-colors duration-150 ease-out hover:bg-hover hover:text-text disabled:cursor-not-allowed disabled:opacity-40"
@click="clearGroup(group)"
>
{{ t('backend::lang.api_tokens.group_clear') }}
</button>
</div>
</div>
<div
v-show="isGroupExpanded(group.key)"
:id="group.panelId"
role="group"
:aria-label="group.label"
>
<label
v-for="scope in group.visibleScopes"
:key="scope.name"
:data-scope="scope.name"
:class="isLocked(scope) ? 'cursor-not-allowed bg-subtle' : 'cursor-pointer hover:bg-hover'"
class="flex min-h-[56px] items-start gap-3 border-t border-border px-4 py-3"
>
<input
type="checkbox"
class="mt-0.5 size-5 shrink-0 rounded-[6px] accent-primary"
:checked="isEffectiveSelected(scope)"
:disabled="disabled || isLocked(scope)"
@change="toggleScope(scope.name, ($event.target as HTMLInputElement).checked)"
/>
<span class="flex min-w-0 flex-1 flex-col">
<span class="flex flex-wrap items-center gap-1">
<span class="font-semibold text-text [overflow-wrap:anywhere]">{{ scope.label }}</span>
<span class="font-mono text-[13px] text-muted [overflow-wrap:anywhere]">{{
scope.name
}}</span>
<span
v-if="scope.read_only && adminReadActive"
data-via-admin-read
class="inline-flex h-6 items-center gap-1 rounded-pill border border-border-strong bg-surface px-2 text-[13px] font-semibold whitespace-nowrap text-muted"
>
<Lock :size="14" aria-hidden="true" />
<span>{{ t('backend::lang.api_tokens.via_admin_read') }}</span>
</span>
<span
v-else-if="scope.read_only"
class="inline-flex h-6 items-center rounded-pill bg-subtle px-2.5 text-[13px] font-semibold whitespace-nowrap text-muted"
>{{ t('backend::lang.api_tokens.read_only') }}</span
>
</span>
<span class="text-[13px] text-muted [overflow-wrap:anywhere]">{{ scope.description }}</span>
</span>
</label>
</div>
</section>
</div>
</div>
<!-- Sticky footer -->
<div class="flex shrink-0 flex-wrap items-center justify-between gap-2 border-t border-border bg-surface p-4">
<p data-scope-picker-summary class="text-[13px] text-muted tabular-nums" aria-live="polite">
{{ footerSummary }}
</p>
<div class="flex items-center gap-2 max-sm:w-full">
<button
type="button"
data-action="scope-picker-discard"
:disabled="disabled"
class="inline-flex h-button items-center justify-center gap-2 rounded-control border border-border-strong bg-surface px-4 font-semibold whitespace-nowrap text-text transition-colors duration-150 ease-out hover:bg-hover disabled:cursor-not-allowed disabled:opacity-60 max-sm:flex-1"
@click="emit('cancel')"
>
{{ cancelLabel }}
</button>
<button
type="button"
data-action="scope-picker-apply"
:disabled="disabled || !canApply"
class="inline-flex h-button items-center justify-center gap-2 rounded-control bg-primary px-4 font-semibold whitespace-nowrap text-on-primary transition-colors duration-150 ease-out hover:opacity-90 disabled:cursor-not-allowed disabled:opacity-60 max-sm:flex-1"
@click="emit('apply')"
>
{{ t('backend::lang.api_tokens.scope_picker_apply') }}
</button>
</div>
</div>
</div>
</template>

View File

@@ -0,0 +1,107 @@
// Reactive 3-tier access state, tier memory, auto-open empty Custom, and
// draft-until-apply lifecycle for ConnectView and ApiTokenCreateDialog
// (Phase 36 D-03, D-09, D-11).
import { computed, ref, type Ref } from 'vue'
import type { AdminAPITokenScope } from '../../api/types'
import {
buildSummaryModel,
computeSubmittedScopes,
partitionCatalog,
resolveInitialState,
type AccessTier,
} from '../../app/scopeCatalog'
export function useScopeSelection(catalog: Ref<readonly AdminAPITokenScope[]>, mode: 'connect' | 'token') {
const initial = resolveInitialState(catalog.value, mode)
const tier = ref<AccessTier>(initial.tier)
const custom = ref<{ adminRead: boolean; scopes: Set<string> }>(initial.custom)
const pickerOpen = ref(false)
const draftAdminRead = ref(false)
const draftScopes = ref<Set<string>>(new Set())
let autoOpenedFrom: AccessTier | null = null
const partitioned = computed(() => partitionCatalog(catalog.value))
const availableTiers = computed<AccessTier[]>(() => {
const tiers: AccessTier[] = []
if (partitioned.value.fullScope !== null) {
tiers.push('full')
}
if (partitioned.value.adminReadScope !== null) {
tiers.push('read_only')
}
if (partitioned.value.pluginScopes.length > 0) {
tiers.push('custom')
}
return tiers
})
const submittedScopes = computed(() => computeSubmittedScopes(tier.value, catalog.value, custom.value))
const summary = computed(() => buildSummaryModel(catalog.value, custom.value))
const canSubmitScopes = computed(() => submittedScopes.value.length > 0)
function reset(nextCatalog?: readonly AdminAPITokenScope[]): void {
const state = resolveInitialState(nextCatalog ?? catalog.value, mode)
tier.value = state.tier
custom.value = state.custom
pickerOpen.value = false
draftAdminRead.value = false
draftScopes.value = new Set()
autoOpenedFrom = null
}
function openPicker(): void {
draftAdminRead.value = custom.value.adminRead
draftScopes.value = new Set(custom.value.scopes)
pickerOpen.value = true
}
function selectTier(next: AccessTier): void {
if (next === tier.value || !availableTiers.value.includes(next)) {
return
}
const previous = tier.value
tier.value = next
if (next === 'custom' && !custom.value.adminRead && custom.value.scopes.size === 0) {
autoOpenedFrom = previous
openPicker()
}
}
function applyPicker(): void {
custom.value = {
adminRead: draftAdminRead.value,
scopes: new Set(draftScopes.value),
}
tier.value = 'custom'
autoOpenedFrom = null
pickerOpen.value = false
}
function discardPicker(): void {
draftAdminRead.value = custom.value.adminRead
draftScopes.value = new Set(custom.value.scopes)
pickerOpen.value = false
if (autoOpenedFrom !== null) {
tier.value = autoOpenedFrom
autoOpenedFrom = null
}
}
return {
availableTiers,
tier,
custom,
pickerOpen,
draftAdminRead,
draftScopes,
submittedScopes,
summary,
canSubmitScopes,
reset,
selectTier,
openPicker,
applyPicker,
discardPicker,
}
}