From 6d7325e6d87e9b11abca481053e91d37bdb2d0f8 Mon Sep 17 00:00:00 2001 From: Jakub Zych Date: Tue, 24 Nov 2026 19:40:02 +0100 Subject: [PATCH] feat(36-02): add shared scope picker module and wire ConnectView --- admin/src/app/scopeCatalog.ts | 291 ++++++++++ .../components/scopes/ScopeAccessControl.vue | 210 +++++++ admin/src/components/scopes/ScopePicker.vue | 529 ++++++++++++++++++ .../components/scopes/useScopeSelection.ts | 107 ++++ admin/src/styles/main.css | 43 ++ admin/src/views/ConnectView.vue | 244 ++++---- 6 files changed, 1330 insertions(+), 94 deletions(-) create mode 100644 admin/src/app/scopeCatalog.ts create mode 100644 admin/src/components/scopes/ScopeAccessControl.vue create mode 100644 admin/src/components/scopes/ScopePicker.vue create mode 100644 admin/src/components/scopes/useScopeSelection.ts diff --git a/admin/src/app/scopeCatalog.ts b/admin/src/app/scopeCatalog.ts new file mode 100644 index 00000000..79bc8369 --- /dev/null +++ b/admin/src/app/scopeCatalog.ts @@ -0,0 +1,291 @@ +// Pure domain logic for the 3-tier access selector and grouped ScopePicker +// shared by ConnectView and ApiTokenCreateDialog (Phase 36 UI-SPEC C1). +import type { AdminAPITokenScope } from '../api/types' + +export type AccessTier = 'full' | 'read_only' | 'custom' +export type ScopeFilter = 'all' | 'selected' | 'read_only' + +export interface ScopeGroup { + key: string + pluginId: string + label: string + scopes: AdminAPITokenScope[] +} + +export interface CustomScopeSelection { + adminRead: boolean + scopes: ReadonlySet +} + +export interface ScopeSummaryChip { + key: string + name: string + label: string + kind: 'admin_read' | 'write' | 'read_only' +} + +export interface PartitionedCatalog { + fullScope: AdminAPITokenScope | null + adminReadScope: AdminAPITokenScope | null + pluginScopes: AdminAPITokenScope[] +} + +export interface SummaryModel { + chips: ScopeSummaryChip[] + visible: ScopeSummaryChip[] + overflowCount: number + selectedWriteCount: number + selectedPluginCount: number + distinctPluginCount: number +} + +export interface InitialScopeState { + tier: AccessTier + custom: { + adminRead: boolean + scopes: Set + } +} + +/** Splits a scope catalog into framework tiers (*, admin:read) and plugin scopes. */ +export function partitionCatalog(catalog: readonly AdminAPITokenScope[]): PartitionedCatalog { + let fullScope: AdminAPITokenScope | null = null + let adminReadScope: AdminAPITokenScope | null = null + const pluginScopes: AdminAPITokenScope[] = [] + + for (const scope of catalog) { + if (scope.name === '*') { + fullScope ??= scope + } else if (scope.name === 'admin:read') { + adminReadScope ??= scope + } else { + pluginScopes.push(scope) + } + } + + return { fullScope, adminReadScope, pluginScopes } +} + +/** + * Groups plugin scopes into flat sections keyed by (plugin_id, group) so two + * plugins never collide even when they resolve to the same translated label + * (D-17). Intra-group order preserves the server catalog order; groups sort + * alphabetically by label using locale-aware collation, tie-broken on plugin_id. + */ +export function groupPluginScopes( + pluginScopes: readonly AdminAPITokenScope[], + locale: string, + fallbackGroupLabel: string, +): ScopeGroup[] { + const byKey = new Map() + + for (const scope of pluginScopes) { + const key = `${scope.plugin_id}\0${scope.group}` + let group = byKey.get(key) + if (!group) { + const label = scope.group.trim() || scope.plugin_id.trim() || fallbackGroupLabel + group = { + key, + pluginId: scope.plugin_id, + label, + scopes: [], + } + byKey.set(key, group) + } + group.scopes.push(scope) + } + + let collator: Intl.Collator + try { + collator = new Intl.Collator(locale || undefined, { sensitivity: 'base' }) + } catch { + collator = new Intl.Collator(undefined, { sensitivity: 'base' }) + } + + return [...byKey.values()].sort((a, b) => { + const byLabel = collator.compare(a.label, b.label) + if (byLabel !== 0) { + return byLabel + } + const byPlugin = a.pluginId.localeCompare(b.pluginId) + if (byPlugin !== 0) { + return byPlugin + } + return a.key.localeCompare(b.key) + }) +} + +/** Case-insensitive substring search across scope code, label, description, plugin_id, and group label. */ +export function matchesScopeQuery(scope: AdminAPITokenScope, groupLabel: string, query: string): boolean { + const normalized = query.trim().toLowerCase() + if (normalized === '') { + return true + } + return ( + scope.name.toLowerCase().includes(normalized) || + scope.label.toLowerCase().includes(normalized) || + scope.description.toLowerCase().includes(normalized) || + scope.plugin_id.toLowerCase().includes(normalized) || + groupLabel.toLowerCase().includes(normalized) + ) +} + +/** True when a plugin scope is ticked directly or covered via the opt-in admin:read toggle (D-04). */ +export function isEffectivelySelected(scope: AdminAPITokenScope, selection: CustomScopeSelection): boolean { + return selection.scopes.has(scope.name) || (selection.adminRead && scope.read_only) +} + +/** + * Smart accordion expand defaults (D-07): + * - Searching: expand every matching group. + * - Small catalog (<= 3 groups or <= 12 plugin scopes): expand all groups. + * - Larger catalog: expand only groups with at least one effectively selected scope. + */ +export function computeDefaultExpanded( + groups: readonly ScopeGroup[], + totalPluginScopes: number, + selection: CustomScopeSelection, + searching: boolean, +): Set { + if (searching || groups.length <= 3 || totalPluginScopes <= 12) { + return new Set(groups.map((group) => group.key)) + } + const expanded = new Set() + for (const group of groups) { + if (group.scopes.some((scope) => isEffectivelySelected(scope, selection))) { + expanded.add(group.key) + } + } + return expanded +} + +/** + * Builds the elevated-first Custom summary model (D-06): + * 1. admin:read chip (if enabled and present in catalog) + * 2. Selected write scopes (non-read_only) in catalog order + * 3. Selected read-only scopes in catalog order, only when admin:read is OFF + */ +export function buildSummaryModel( + catalog: readonly AdminAPITokenScope[], + selection: CustomScopeSelection, + maxVisible = 6, +): SummaryModel { + const { adminReadScope, pluginScopes } = partitionCatalog(catalog) + const adminReadActive = selection.adminRead && adminReadScope !== null + const chips: ScopeSummaryChip[] = [] + + if (adminReadActive) { + chips.push({ + key: 'admin:read', + name: 'admin:read', + label: 'admin:read', + kind: 'admin_read', + }) + } + + const selectedWrites: AdminAPITokenScope[] = [] + const selectedReads: AdminAPITokenScope[] = [] + for (const scope of pluginScopes) { + if (!selection.scopes.has(scope.name)) { + continue + } + if (!scope.read_only) { + selectedWrites.push(scope) + } else if (!adminReadActive) { + selectedReads.push(scope) + } + } + + for (const scope of selectedWrites) { + chips.push({ + key: scope.name, + name: scope.name, + label: scope.label, + kind: 'write', + }) + } + for (const scope of selectedReads) { + chips.push({ + key: scope.name, + name: scope.name, + label: scope.label, + kind: 'read_only', + }) + } + + const countedScopes = adminReadActive ? selectedWrites : [...selectedWrites, ...selectedReads] + const pluginKeys = new Set() + for (const scope of countedScopes) { + pluginKeys.add(scope.plugin_id || scope.group || scope.name) + } + + const cap = Math.max(0, maxVisible) + const visible = chips.slice(0, cap) + const overflowCount = Math.max(0, chips.length - cap) + + return { + chips, + visible, + overflowCount, + selectedWriteCount: selectedWrites.length, + selectedPluginCount: countedScopes.length, + distinctPluginCount: pluginKeys.size, + } +} + +/** + * Serializes the active tier/custom selection into the exact scope list submitted + * to POST /mcp/consent/{request} or POST /account/tokens (D-04, T-36-04). + */ +export function computeSubmittedScopes( + tier: AccessTier, + catalog: readonly AdminAPITokenScope[], + custom: CustomScopeSelection, +): string[] { + const { fullScope, adminReadScope, pluginScopes } = partitionCatalog(catalog) + if (tier === 'full') { + return fullScope ? ['*'] : [] + } + if (tier === 'read_only') { + return adminReadScope ? ['admin:read'] : [] + } + if (custom.adminRead && adminReadScope) { + return [ + 'admin:read', + ...pluginScopes.filter((scope) => !scope.read_only && custom.scopes.has(scope.name)).map((scope) => scope.name), + ] + } + return pluginScopes.filter((scope) => custom.scopes.has(scope.name)).map((scope) => scope.name) +} + +/** Resolves the initial tier and Custom selection for ConnectView (D-03) or ApiTokenCreateDialog (D-08). */ +export function resolveInitialState( + catalog: readonly AdminAPITokenScope[], + mode: 'connect' | 'token', +): InitialScopeState { + const { fullScope, adminReadScope, pluginScopes } = partitionCatalog(catalog) + + if (mode === 'connect') { + if (adminReadScope !== null) { + return { tier: 'read_only', custom: { adminRead: false, scopes: new Set() } } + } + if (pluginScopes.length > 0) { + return { + tier: 'custom', + custom: { adminRead: false, scopes: new Set(pluginScopes.map((scope) => scope.name)) }, + } + } + if (fullScope !== null) { + return { tier: 'full', custom: { adminRead: false, scopes: new Set() } } + } + return { tier: 'read_only', custom: { adminRead: false, scopes: new Set() } } + } + + if (adminReadScope !== null) { + return { tier: 'read_only', custom: { adminRead: false, scopes: new Set() } } + } + if (fullScope !== null) { + return { tier: 'full', custom: { adminRead: false, scopes: new Set() } } + } + return { tier: 'custom', custom: { adminRead: false, scopes: new Set() } } +} diff --git a/admin/src/components/scopes/ScopeAccessControl.vue b/admin/src/components/scopes/ScopeAccessControl.vue new file mode 100644 index 00000000..e245c2d6 --- /dev/null +++ b/admin/src/components/scopes/ScopeAccessControl.vue @@ -0,0 +1,210 @@ + + + diff --git a/admin/src/components/scopes/ScopePicker.vue b/admin/src/components/scopes/ScopePicker.vue new file mode 100644 index 00000000..b8861f1a --- /dev/null +++ b/admin/src/components/scopes/ScopePicker.vue @@ -0,0 +1,529 @@ + + + diff --git a/admin/src/components/scopes/useScopeSelection.ts b/admin/src/components/scopes/useScopeSelection.ts new file mode 100644 index 00000000..f3be526a --- /dev/null +++ b/admin/src/components/scopes/useScopeSelection.ts @@ -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, mode: 'connect' | 'token') { + const initial = resolveInitialState(catalog.value, mode) + const tier = ref(initial.tier) + const custom = ref<{ adminRead: boolean; scopes: Set }>(initial.custom) + const pickerOpen = ref(false) + const draftAdminRead = ref(false) + const draftScopes = ref>(new Set()) + let autoOpenedFrom: AccessTier | null = null + + const partitioned = computed(() => partitionCatalog(catalog.value)) + + const availableTiers = computed(() => { + 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, + } +} diff --git a/admin/src/styles/main.css b/admin/src/styles/main.css index f4f24c02..a41b7923 100644 --- a/admin/src/styles/main.css +++ b/admin/src/styles/main.css @@ -528,6 +528,49 @@ .summer-diff__ctx { color: var(--c-text); } + + /* Standalone light-literal surface for ConnectView card and portaled + Customize modal (Phase 36 D-12): re-declares both --c-* and --color-* + aliases so Tailwind v4 @theme utilities stay light even under + . */ + .connect-light-surface { + color-scheme: light; + --c-bg: #f4f6f9; + --c-surface: #ffffff; + --c-subtle: #f3f5f8; + --c-border: #e6e9ef; + --c-border-strong: #d2d8e2; + --c-text: #141b2d; + --c-muted: #566175; + --c-placeholder: #6b7588; + --c-primary: #22304d; + --c-on-primary: #ffffff; + --c-danger: #c62828; + --c-danger-soft: #fdf0f0; + --c-ring: rgba(252, 196, 40, 0.55); + --c-hover: #f1f3f7; + --c-sel: #fdf3cf; + --c-skel: #eceff4; + --c-overlay: rgba(20, 27, 45, 0.5); + + --color-bg: var(--c-bg); + --color-surface: var(--c-surface); + --color-subtle: var(--c-subtle); + --color-border: var(--c-border); + --color-border-strong: var(--c-border-strong); + --color-text: var(--c-text); + --color-muted: var(--c-muted); + --color-placeholder: var(--c-placeholder); + --color-primary: var(--c-primary); + --color-on-primary: var(--c-on-primary); + --color-danger: var(--c-danger); + --color-danger-soft: var(--c-danger-soft); + --color-ring: var(--c-ring); + --color-hover: var(--c-hover); + --color-sel: var(--c-sel); + --color-skel: var(--c-skel); + --color-overlay: var(--c-overlay); + } } @media (prefers-reduced-motion: reduce) { diff --git a/admin/src/views/ConnectView.vue b/admin/src/views/ConnectView.vue index 95446910..0403801a 100644 --- a/admin/src/views/ConnectView.vue +++ b/admin/src/views/ConnectView.vue @@ -1,20 +1,33 @@