Files
vhaudiquet 2216f37422 feat: pick the room model from a catalog dialog
The room model was a free-text field. It now opens a dialog that lists the provider's live catalog through the same adapter a turn uses, with context, output, price, and vision from models.dev. Rows filter by text, vision, or free price, and a custom id remains available when the provider lists nothing.

The dialog is nested over room settings, so BaseDialog now gives Escape and the focus trap to the top dialog only. Clearing the model stores NULL, which returns the room to the provider's default.
2026-11-28 17:21:48 +01:00

116 lines
3.4 KiB
Vue

<script setup lang="ts">
import { nextTick, onBeforeUnmount, onMounted, ref, useId } from 'vue'
import { X } from 'lucide-vue-next'
import { isTopDialog, pushDialog, removeDialog } from '~/utils/dialogStack'
const props = withDefaults(defineProps<{ title: string; description?: string; wide?: boolean }>(), {
description: '',
wide: false,
})
const emit = defineEmits<{ close: [] }>()
const titleId = useId()
const descId = useId()
const panel = ref<HTMLElement | null>(null)
let previousFocus: HTMLElement | null = null
// A dialog can open another one (the model picker over room settings).
// Only the top of the stack answers Escape and Tab, so closing the
// inner dialog leaves the outer one open. The stack lives in its own
// module because every `<script setup>` binding is per instance.
const dialogId = Symbol('dialog')
function focusable(): HTMLElement[] {
if (!panel.value) return []
const selector = [
'a[href]',
'button:not([disabled])',
'input:not([disabled]):not([type="hidden"])',
'select:not([disabled])',
'textarea:not([disabled])',
'[tabindex]:not([tabindex="-1"])',
].join(',')
return Array.from(panel.value.querySelectorAll<HTMLElement>(selector)).filter(
(element) => element.offsetParent !== null,
)
}
function onKeydown(event: KeyboardEvent) {
if (!isTopDialog(dialogId)) return
if (event.key === 'Escape') {
event.preventDefault()
emit('close')
return
}
if (event.key !== 'Tab') return
const items = focusable()
if (items.length === 0) {
event.preventDefault()
panel.value?.focus()
return
}
const first = items[0]
const last = items[items.length - 1]
const active = document.activeElement as HTMLElement | null
if (event.shiftKey) {
if (active === first || !panel.value?.contains(active)) {
event.preventDefault()
last?.focus()
}
} else if (active === last || !panel.value?.contains(active)) {
event.preventDefault()
first?.focus()
}
}
function onBackdrop(event: MouseEvent) {
if (!isTopDialog(dialogId)) return
if (event.target === event.currentTarget) emit('close')
}
onMounted(async () => {
pushDialog(dialogId)
previousFocus = document.activeElement as HTMLElement | null
document.addEventListener('keydown', onKeydown)
await nextTick()
const preferred = panel.value?.querySelector<HTMLElement>('[autofocus]')
const items = focusable()
;(preferred ?? items[0] ?? panel.value)?.focus()
})
onBeforeUnmount(() => {
removeDialog(dialogId)
document.removeEventListener('keydown', onKeydown)
previousFocus?.focus?.()
})
</script>
<template>
<Teleport to="body">
<div class="dialog-backdrop" @mousedown="onBackdrop">
<section
ref="panel"
class="dialog"
:class="{ 'dialog-wide': wide }"
role="dialog"
aria-modal="true"
:aria-labelledby="titleId"
:aria-describedby="description ? descId : undefined"
tabindex="-1"
>
<div class="dialog-head">
<div>
<h2 :id="titleId" class="dialog-title">{{ props.title }}</h2>
<p v-if="description" :id="descId" class="dialog-sub">{{ description }}</p>
</div>
<button type="button" class="icon-btn" aria-label="Close dialog" title="Close" @click="emit('close')">
<X :size="16" aria-hidden="true" />
</button>
</div>
<slot />
</section>
</div>
</Teleport>
</template>