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

@@ -1,20 +1,33 @@
<script setup lang="ts">
import { computed, nextTick, onMounted, ref, watch } from 'vue'
import { RouterLink, useRoute, useRouter } from 'vue-router'
import { Bot, CircleAlert, Clock, Info, LoaderCircle, Sun } from '@lucide/vue'
import {
DialogContent,
DialogDescription,
DialogOverlay,
DialogPortal,
DialogRoot,
DialogTitle,
} from 'reka-ui'
import { Bot, CircleAlert, Clock, Info, LoaderCircle, Sun, X } from '@lucide/vue'
import { api } from '../api/client'
import type { MCPConsent } from '../api/types'
import { t } from '../app/i18n'
import type { AccessTier } from '../app/scopeCatalog'
import ScopeAccessControl from '../components/scopes/ScopeAccessControl.vue'
import ScopePicker from '../components/scopes/ScopePicker.vue'
import { useScopeSelection } from '../components/scopes/useScopeSelection'
import { currentUser, expireSession, logout } from '../state/useAuth'
// The MCP consent card (32.2 UI-SPEC C1). A standalone screen in the login
// flow: the same navy page and white card as LoginView, with light literal
// colours inside the card so it looks the same in light and dark mode. The
// decision is posted with fetch and the browser then leaves for the
// The MCP consent card (32.2 UI-SPEC C1 + Phase 36 UI-SPEC C4). A standalone
// screen in the login flow: the same navy page and white card as LoginView,
// pinned to light-literal colours via .connect-light-surface so both the card
// and the portaled Customize scopes modal look the same in light and dark mode.
// The decision is posted with fetch and the browser then leaves for the
// server-issued redirect_uri with window.location.assign: never the router,
// never safeRedirect, never a native form POST (CSP form-action 'self').
// Nothing is posted without a click, even for a client allowed before, and
// neither button gets initial focus or a hotkey (T-32.2-12).
// neither Allow nor Deny gets initial focus or a hotkey (T-32.2-12, T-36-05).
type State = 'loading' | 'ready' | 'expired' | 'load_error' | 'returning'
type InlineError = 'scopes_required' | 'decide_failed'
@@ -22,7 +35,8 @@ type InlineError = 'scopes_required' | 'decide_failed'
const route = useRoute()
const router = useRouter()
const CARD = 'flex flex-col gap-[18px] rounded-card bg-white p-8 text-[#141b2d] shadow-login'
const CARD =
'connect-light-surface flex flex-col gap-[18px] rounded-card bg-white p-8 text-[#141b2d] shadow-login'
const HEADING = 'text-[22px] font-bold tracking-[-0.02em] [overflow-wrap:anywhere]'
const ALERT = 'flex items-center gap-2.5 rounded-control bg-[#fdf0f0] px-3.5 py-3 text-[#c62828]'
const SECONDARY_BUTTON =
@@ -30,7 +44,6 @@ const SECONDARY_BUTTON =
const state = ref<State>('loading')
const consent = ref<MCPConsent | null>(null)
const checked = ref<Set<string>>(new Set())
const offline = ref(true)
const busy = ref<'allow' | 'deny' | null>(null)
const switching = ref(false)
@@ -38,6 +51,8 @@ const inlineError = ref<InlineError | null>(null)
const returnUri = ref('')
const heading = ref<HTMLElement | null>(null)
const alertBlock = ref<HTMLElement | null>(null)
const scopeAccessRef = ref<InstanceType<typeof ScopeAccessControl> | null>(null)
const scopePickerRef = ref<InstanceType<typeof ScopePicker> | null>(null)
let loadSeq = 0
let settled = false
@@ -48,15 +63,25 @@ const requestId = computed(() => {
const scopes = computed(() => consent.value?.scopes ?? [])
const grantable = computed(() => scopes.value.length > 0)
const starChecked = computed(() => checked.value.has('*'))
const selected = computed(() => {
if (starChecked.value) {
return ['*']
}
return scopes.value.map((scope) => scope.name).filter((name) => checked.value.has(name))
})
const {
availableTiers,
tier,
custom,
pickerOpen,
draftAdminRead,
draftScopes,
submittedScopes,
canSubmitScopes,
reset: resetScopeSelection,
selectTier,
openPicker,
applyPicker,
discardPicker,
} = useScopeSelection(scopes, 'connect')
const locked = computed(() => busy.value !== null || switching.value)
const canAllow = computed(() => !locked.value && grantable.value && selected.value.length > 0)
const canAllow = computed(() => !locked.value && grantable.value && canSubmitScopes.value)
const clientName = computed(() => consent.value?.client.name || consent.value?.client.id || '')
const redirectHost = computed(() => consent.value?.client.redirect_host ?? '')
@@ -114,20 +139,12 @@ const returnLink = computed(() => {
}
})
/** Every shown scope starts ticked except *, unless * is the only one. */
function initialTicks(view: MCPConsent): Set<string> {
const names = view.scopes.map((scope) => scope.name)
if (names.length === 1 && names[0] === '*') {
return new Set(names)
}
return new Set(names.filter((name) => name !== '*'))
}
async function load(): Promise<void> {
const seq = ++loadSeq
state.value = 'loading'
inlineError.value = null
consent.value = null
resetScopeSelection([])
returnUri.value = ''
const id = requestId.value
if (id === '') {
@@ -149,7 +166,7 @@ async function load(): Promise<void> {
if (result.data) {
const view = result.data.data
consent.value = view
checked.value = initialTicks(view)
resetScopeSelection(view.scopes)
offline.value = true
state.value = 'ready'
return
@@ -162,30 +179,56 @@ async function load(): Promise<void> {
state.value = status === 404 ? 'expired' : 'load_error'
}
function toggleScope(name: string, on: boolean): void {
const next = new Set(checked.value)
if (name === '*') {
if (on) {
next.clear()
next.add('*')
} else {
next.delete('*')
}
} else if (!starChecked.value) {
if (on) {
next.add(name)
} else {
next.delete(name)
}
function onSelectTier(next: AccessTier): void {
if (locked.value) {
return
}
checked.value = next
selectTier(next)
if (inlineError.value === 'scopes_required') {
inlineError.value = null
}
}
function scopeDisabled(name: string): boolean {
return starChecked.value && name !== '*'
function onOpenPicker(): void {
if (locked.value) {
return
}
openPicker()
}
function onApplyPicker(): void {
applyPicker()
if (inlineError.value === 'scopes_required') {
inlineError.value = null
}
}
function onPickerOpenChange(open: boolean): void {
if (!open) {
discardPicker()
}
}
function onPickerOpenAutoFocus(event: Event): void {
event.preventDefault()
if (scopePickerRef.value) {
scopePickerRef.value.focusSearch()
return
}
void nextTick(() => {
scopePickerRef.value?.focusSearch()
})
}
function onPickerCloseAutoFocus(event: Event): void {
event.preventDefault()
if (scopeAccessRef.value) {
scopeAccessRef.value.focusPrimaryControl()
return
}
void nextTick(() => {
scopeAccessRef.value?.focusPrimaryControl()
})
}
/** Schemes a consent redirect never uses: script, inline content and local files. */
@@ -254,7 +297,7 @@ async function decide(decision: 'allow' | 'deny'): Promise<void> {
decision === 'allow'
? {
decision,
scopes: selected.value,
scopes: submittedScopes.value,
offline_access: consent.value.offline_access ? offline.value : false,
}
: { decision, scopes: [], offline_access: false },
@@ -363,14 +406,11 @@ watch(requestId, () => {
<span class="h-3 w-56 rounded-[6px] bg-[#eceff4]" />
<span class="h-3 w-40 rounded-[6px] bg-[#eceff4]" />
</div>
<div class="overflow-hidden rounded-inner border border-[#e6e9ef]" aria-hidden="true">
<div
v-for="index in 3"
:key="index"
class="flex min-h-[56px] items-center gap-3 border-t border-[#e6e9ef] px-4 py-3 first:border-t-0"
>
<span class="size-5 shrink-0 rounded-[6px] bg-[#eceff4]" />
<span class="h-3 w-40 rounded-[6px] bg-[#eceff4]" />
<div class="flex flex-col gap-2" aria-hidden="true">
<span class="h-10 rounded-inner bg-[#eceff4]" />
<div class="flex flex-col gap-2 rounded-inner border border-[#e6e9ef] bg-[#f3f5f8] p-4">
<span class="h-3 w-48 rounded-[6px] bg-[#eceff4]" />
<span class="h-3 w-32 rounded-[6px] bg-[#eceff4]" />
</div>
</div>
</div>
@@ -483,47 +523,18 @@ watch(requestId, () => {
<fieldset v-else :disabled="locked" class="flex min-w-0 flex-col gap-[18px]">
<div class="flex min-w-0 flex-col gap-1.5">
<span id="connect-scopes-label" class="font-semibold">{{ t('backend::lang.api_tokens.scopes') }}</span>
<div
role="group"
aria-labelledby="connect-scopes-label"
data-scope-picker
:aria-invalid="inlineError === 'scopes_required' ? 'true' : undefined"
:class="inlineError === 'scopes_required' ? 'border-[#c62828]' : 'border-[#e6e9ef]'"
class="overflow-hidden rounded-inner border"
>
<label
v-for="scope in scopes"
:key="scope.name"
:data-scope="scope.name"
:class="scopeDisabled(scope.name) ? 'cursor-not-allowed opacity-60' : 'cursor-pointer'"
class="flex min-h-[56px] items-start gap-3 border-t border-[#e6e9ef] px-4 py-3 first:border-t-0"
>
<input
type="checkbox"
class="mt-0.5 size-5 shrink-0 rounded-[6px] accent-[#22304d]"
:checked="checked.has(scope.name)"
:disabled="scopeDisabled(scope.name)"
@change="toggleScope(scope.name, ($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 [overflow-wrap:anywhere]">{{ scope.label }}</span>
<span class="font-mono text-[13px] text-[#566175] [overflow-wrap:anywhere]">{{ scope.name }}</span>
<span
v-if="scope.read_only"
class="inline-flex h-6 items-center rounded-pill bg-[#f3f5f8] px-2.5 text-[13px] font-semibold whitespace-nowrap text-[#566175]"
>{{ t('backend::lang.api_tokens.read_only') }}</span
>
</span>
<span class="text-[13px] text-[#566175] [overflow-wrap:anywhere]">{{ scope.description }}</span>
</span>
</label>
</div>
<p v-if="starChecked" data-scope-all-note class="flex items-center gap-2 text-[13px] text-[#566175]">
<Info :size="14" class="shrink-0" aria-hidden="true" />
<span>{{ t('backend::lang.api_tokens.scope_all_includes') }}</span>
</p>
<ScopeAccessControl
ref="scopeAccessRef"
id-prefix="connect"
:catalog="scopes"
:available-tiers="availableTiers"
:tier="tier"
:custom="custom"
:disabled="locked"
:invalid="inlineError === 'scopes_required'"
@update:tier="onSelectTier"
@customize="onOpenPicker"
/>
<div
v-if="inlineError === 'scopes_required'"
ref="alertBlock"
@@ -587,5 +598,50 @@ watch(requestId, () => {
</div>
</form>
</div>
<!-- Customize scopes modal (D-10, D-11, D-12): light-literal surface even in dark mode. -->
<DialogRoot :open="pickerOpen" @update:open="onPickerOpenChange">
<DialogPortal>
<DialogOverlay data-dialog-overlay class="connect-light-surface fixed inset-0 z-40 bg-overlay" />
<DialogContent
data-scope-picker-dialog
data-dialog-content
aria-modal="true"
class="connect-light-surface fixed inset-0 z-50 flex h-[100dvh] w-full flex-col overflow-hidden bg-surface text-text sm:inset-auto sm:top-1/2 sm:left-1/2 sm:h-auto sm:max-h-[85dvh] sm:w-[calc(100%-32px)] sm:max-w-[640px] sm:-translate-x-1/2 sm:-translate-y-1/2 sm:rounded-modal sm:shadow-pop"
@open-auto-focus="onPickerOpenAutoFocus"
@close-auto-focus="onPickerCloseAutoFocus"
>
<div class="flex shrink-0 items-start gap-4 border-b border-border bg-surface p-4">
<div class="flex min-w-0 flex-1 flex-col gap-1">
<DialogTitle class="text-[20px] font-bold tracking-[-0.01em] text-text">{{
t('backend::lang.api_tokens.scope_picker_title')
}}</DialogTitle>
<DialogDescription class="text-[13px] text-muted [overflow-wrap:anywhere]">{{
t('backend::lang.mcp_connect.scope_picker_description', { client: clientName })
}}</DialogDescription>
</div>
<button
type="button"
data-action="close-scope-picker"
:aria-label="t('backend::lang.form.close')"
class="flex size-[34px] shrink-0 items-center justify-center rounded-tab bg-subtle text-muted hover:bg-hover hover:text-text"
@click="discardPicker"
>
<X :size="16" aria-hidden="true" />
</button>
</div>
<ScopePicker
ref="scopePickerRef"
:catalog="scopes"
v-model:draft-admin-read="draftAdminRead"
v-model:draft-scopes="draftScopes"
:cancel-label="t('backend::lang.api_tokens.scope_picker_discard')"
:disabled="locked"
@apply="onApplyPicker"
@cancel="discardPicker"
/>
</DialogContent>
</DialogPortal>
</DialogRoot>
</main>
</template>