Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 33 additions & 26 deletions web/app/components/pools/PoolInstanceList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,31 @@
<p v-if="!instances.length" class="text-body-large text-on-surface-variant" data-test="pool-instances-empty">
{{ $t('pools.instances.empty') }}
</p>
<div v-else class="overflow-x-auto rounded-md bg-surface-container-low shadow-elevation-1">
<table class="w-full min-w-[36rem] text-left text-body-medium" data-test="pool-instance-table">
<thead class="text-label-large text-on-surface-variant">
<tr>
<th scope="col" class="px-4 py-3 font-medium">{{ $t('pools.instances.id') }}</th>
<th scope="col" class="px-4 py-3 font-medium">{{ $t('pools.instances.state') }}</th>
<th scope="col" class="px-4 py-3 font-medium">{{ $t('pools.instances.players') }}</th>
<th scope="col" class="px-4 py-3 font-medium">{{ $t('pools.instances.address') }}</th>
<th scope="col" class="px-4 py-3 font-medium">{{ $t('pools.instances.age') }}</th>
</tr>
</thead>
<tbody>
<tr v-for="instance in instances" :key="instance.id" class="border-t border-outline-variant" data-test="pool-instance-row">
<td class="px-4 py-3">
<NuxtLink :to="`/instances/${encodeURIComponent(instance.id)}`" class="font-mono text-primary hover:underline">
{{ instance.id }}
</NuxtLink>
</td>
<td class="px-4 py-3"><StatusChip :state="instance.state" /></td>
<td class="px-4 py-3 tabular-nums">{{ instance.players }} / {{ instance.maxPlayers }}</td>
<td class="px-4 py-3 font-mono">{{ instance.address || instance.host }}</td>
<td class="px-4 py-3 tabular-nums">{{ ageText(instance.createdAt) }}</td>
</tr>
</tbody>
</table>
</div>
<M3DataTable
v-else
:columns="columns"
:items="instances"
row-key="id"
:paginate="false"
:row-attrs="() => ({ 'data-test': 'pool-instance-row' })"
data-test="pool-instance-table"
>
<template #item.id="{ item }">
<NuxtLink :to="`/instances/${encodeURIComponent(item.id)}`" class="font-mono text-primary hover:underline">
{{ item.id }}
</NuxtLink>
</template>
<template #item.state="{ item }"><StatusChip :state="item.state" /></template>
<template #item.players="{ item }">
<span class="tabular-nums">{{ item.players }} / {{ item.maxPlayers }}</span>
</template>
<template #item.address="{ item }">
<span class="font-mono">{{ item.address || item.host }}</span>
</template>
<template #item.createdAt="{ item }">
<span class="tabular-nums">{{ ageText(item.createdAt) }}</span>
</template>
</M3DataTable>
</div>
</template>

Expand All @@ -40,6 +39,14 @@ import { ageOf } from '~/lib/pools/view'
const props = defineProps<{ instances: Instance[], now: number | null }>()
const { t } = useI18n()

const columns = computed(() => [
{ key: 'id', title: t('pools.instances.id'), sortable: false },
{ key: 'state', title: t('pools.instances.state'), sortable: false },
{ key: 'players', title: t('pools.instances.players'), sortable: false },
{ key: 'address', title: t('pools.instances.address'), sortable: false },
{ key: 'createdAt', title: t('pools.instances.age'), sortable: false },
])

function ageText(createdAt: string): string {
if (props.now === null) return '–'
const { unit, value } = ageOf(createdAt, props.now)
Expand Down
60 changes: 21 additions & 39 deletions web/app/components/pools/PoolScalingForm.vue
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
<template>
<form novalidate data-test="scaling-form" @submit.prevent="onSubmit">
<div class="grid gap-4 sm:grid-cols-2">
<div v-for="field in FIELDS" :key="field.key" class="flex min-w-0 flex-col gap-1">
<label :for="idOf(field.key)" class="text-label-large text-on-surface">
{{ $t(`pools.scaling.fields.${field.key}`) }}
</label>
<input
:id="idOf(field.key)"
:value="modelValue[field.key]"
:type="field.type"
:inputmode="field.inputmode"
:step="field.step"
:min="field.min"
:max="field.max"
:name="field.key"
:aria-invalid="shownError(field.key) ? 'true' : 'false'"
:aria-describedby="describedBy(field.key)"
class="w-full rounded-xs border border-outline bg-surface px-3 py-2 text-body-large text-on-surface focus:outline-2 focus:outline-primary aria-[invalid=true]:border-error"
@input="onInput(field.key, ($event.target as HTMLInputElement).value)"
>
<p v-if="shownError(field.key)" :id="errorIdOf(field.key)" class="text-body-small text-error">
{{ $t(`pools.scaling.errors.${shownError(field.key)}`) }}
</p>
<p v-else-if="field.hint" :id="hintIdOf(field.key)" class="text-body-small text-on-surface-variant">
{{ $t(`pools.scaling.hints.${field.hint}`) }}
</p>
</div>
<M3TextField
v-for="field in FIELDS"
:key="field.key"
:model-value="modelValue[field.key]"
:label="$t(`pools.scaling.fields.${field.key}`)"
:type="field.type"
:inputmode="field.inputmode"
:step="field.step"
:min="field.min"
:max="field.max"
:name="field.key"
:error="errorText(field.key)"
:hint="field.hint ? $t(`pools.scaling.hints.${field.hint}`) : ''"
class="min-w-0"
@update:model-value="value => onInput(field.key, String(value))"
/>
</div>

<p v-if="dirty" class="mt-4 text-body-small text-on-surface-variant" data-test="scaling-unsaved">
Expand All @@ -44,7 +35,7 @@
</template>

<script setup lang="ts">
import { reactive, ref, useId } from 'vue'
import { reactive, ref } from 'vue'
import { validateScaling } from '~/lib/pools/scaling'
import type { Scaling, ScalingError, ScalingField, ScalingForm } from '~/lib/pools/scaling'

Expand Down Expand Up @@ -80,28 +71,19 @@ const FIELDS: FieldSpec[] = [
{ key: 'maxSurge', type: 'number', inputmode: 'numeric', step: '1', min: '0', hint: 'optional' },
]

const uid = useId()
const { t } = useI18n()
const touched = reactive<Partial<Record<ScalingField, boolean>>>({})
const submitted = ref(false)

const result = computed(() => validateScaling(props.modelValue, { mode: props.mode }))

function idOf(field: ScalingField) {
return `${uid}-${field}`
}
function errorIdOf(field: ScalingField) {
return `${idOf(field)}-error`
}
function hintIdOf(field: ScalingField) {
return `${idOf(field)}-hint`
}
function shownError(field: ScalingField): ScalingError | undefined {
const error = result.value.errors[field]
return error && (touched[field] || submitted.value) ? error : undefined
}
function describedBy(field: ScalingField): string | undefined {
if (shownError(field)) return errorIdOf(field)
return FIELDS.find(f => f.key === field)?.hint ? hintIdOf(field) : undefined
function errorText(field: ScalingField): string {
const error = shownError(field)
return error ? t(`pools.scaling.errors.${error}`) : ''
}

function onInput(field: ScalingField, value: string) {
Expand Down
77 changes: 41 additions & 36 deletions web/app/components/pools/PoolTable.vue
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
<template>
<div class="overflow-x-auto rounded-md bg-surface-container-low shadow-elevation-1">
<table class="w-full min-w-[44rem] text-left text-body-medium" data-test="pool-table">
<thead class="text-label-large text-on-surface-variant">
<tr>
<th scope="col" class="px-4 py-3 font-medium">{{ $t('pools.table.name') }}</th>
<th scope="col" class="px-4 py-3 font-medium">{{ $t('pools.table.role') }}</th>
<th scope="col" class="px-4 py-3 font-medium">{{ $t('pools.table.mode') }}</th>
<th scope="col" class="px-4 py-3 font-medium">{{ $t('pools.table.driver') }}</th>
<th scope="col" class="px-4 py-3 font-medium">{{ $t('pools.table.scaling') }}</th>
<th scope="col" class="px-4 py-3 font-medium">{{ $t('pools.table.instances') }}</th>
<th scope="col" class="px-4 py-3 font-medium">{{ $t('pools.table.maintenance') }}</th>
</tr>
</thead>
<tbody>
<tr v-for="pool in pools" :key="pool.name" class="border-t border-outline-variant" data-test="pool-row">
<td class="px-4 py-3">
<NuxtLink :to="poolPath(pool.name)" class="font-medium text-primary hover:underline">{{ pool.name }}</NuxtLink>
</td>
<td class="px-4 py-3">{{ pool.spec.role }}</td>
<td class="px-4 py-3">{{ pool.spec.mode }}</td>
<td class="px-4 py-3">{{ pool.spec.driver }}</td>
<td class="px-4 py-3 tabular-nums">
{{ pool.spec.scaling.min }} / {{ pool.spec.scaling.max }} / {{ pool.spec.scaling.buffer ?? 0 }}
</td>
<td class="px-4 py-3 tabular-nums" data-test="pool-instances">
{{ countsOf(pool.name).ready }} / {{ countsOf(pool.name).total }}
</td>
<td class="px-4 py-3">
<M3Chip v-if="pool.maintenance" tone="warning" icon="mdi-wrench-outline" small>
{{ $t('pools.badges.maintenance') }}
</M3Chip>
</td>
</tr>
</tbody>
</table>
</div>
<M3DataTable
:columns="columns"
:items="pools"
row-key="name"
:paginate="false"
:row-attrs="() => ({ 'data-test': 'pool-row' })"
data-test="pool-table"
>
<template #item.name="{ item }">
<NuxtLink :to="poolPath(item.name)" class="font-medium text-primary hover:underline">{{ item.name }}</NuxtLink>
</template>
<template #item.role="{ item }">{{ item.spec.role }}</template>
<template #item.mode="{ item }">{{ item.spec.mode }}</template>
<template #item.driver="{ item }">{{ item.spec.driver }}</template>
<template #item.scaling="{ item }">
<span class="tabular-nums">
{{ item.spec.scaling.min }} / {{ item.spec.scaling.max }} / {{ item.spec.scaling.buffer ?? 0 }}
</span>
</template>
<template #item.instances="{ item }">
<span class="tabular-nums" data-test="pool-instances">
{{ countsOf(item.name).ready }} / {{ countsOf(item.name).total }}
</span>
</template>
<template #item.maintenance="{ item }">
<M3Chip v-if="item.maintenance" tone="warning" icon="mdi-wrench-outline" small>
{{ $t('pools.badges.maintenance') }}
</M3Chip>
</template>
</M3DataTable>
</template>

<script setup lang="ts">
import type { Instance, Pool } from '~/lib/grus'
import { poolCounts, poolPath } from '~/lib/pools/view'

const props = defineProps<{ pools: Pool[], instances: Instance[] }>()
const { t } = useI18n()

const columns = computed(() => [
{ key: 'name', title: t('pools.table.name'), sortable: false },
{ key: 'role', title: t('pools.table.role'), sortable: false },
{ key: 'mode', title: t('pools.table.mode'), sortable: false },
{ key: 'driver', title: t('pools.table.driver'), sortable: false },
{ key: 'scaling', title: t('pools.table.scaling'), sortable: false },
{ key: 'instances', title: t('pools.table.instances'), sortable: false },
{ key: 'maintenance', title: t('pools.table.maintenance'), sortable: false },
])

function countsOf(name: string) {
return poolCounts(props.instances, name)
Expand Down
42 changes: 42 additions & 0 deletions web/app/m3-only.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { readdirSync, readFileSync, statSync } from 'node:fs'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'

// Material 3 is mandatory for OLF UIs: pages and components use the Pavo fields, tables and switches, not the raw
// form and table elements. Pavo's components render the raw elements inside themselves, so only app templates are
// checked here. Only the template part is scanned; a script that mentions a tag does not count.
const APP_DIR = join(__dirname)
const SCANNED = ['pages', 'components'].map(dir => join(APP_DIR, dir))
const RAW_TAGS = ['input', 'select', 'textarea', 'table']
const HINT = 'use M3TextField, M3Select, M3Checkbox, M3Switch or M3DataTable instead'

function vueFiles(dir: string): string[] {
return readdirSync(dir).flatMap((name) => {
const path = join(dir, name)
if (statSync(path).isDirectory()) return vueFiles(path)
return path.endsWith('.vue') ? [path] : []
})
}

function rawTagsIn(source: string): string[] {
const template = source.split('<script')[0] ?? ''
return RAW_TAGS.filter(tag => new RegExp(`<${tag}\\b`).test(template))
}

describe('material 3 elements', () => {
it('finds raw form and table tags in a template', () => {
expect(rawTagsIn('<template><div><input><table></table></div></template>')).toEqual(['input', 'table'])
})

it('ignores raw tags inside the script block', () => {
expect(rawTagsIn('<template><M3Select /></template>\n<script setup>const x = \'<select>\'</script>')).toEqual([])
})

it('uses no raw form or table element in pages and components', () => {
const violations = SCANNED.flatMap(dir => vueFiles(dir)).flatMap((file) => {
const source = readFileSync(file, 'utf8')
return rawTagsIn(source).map(tag => `${file.slice(APP_DIR.length + 1)}: <${tag}>`)
})
expect(violations, `raw elements found (${HINT})`).toEqual([])
})
})
76 changes: 40 additions & 36 deletions web/app/pages/events/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,47 +39,45 @@
{{ type }}
</button>
</div>
<label class="flex max-w-md flex-col gap-1 text-label-medium text-on-surface-variant">
{{ $t('events.filterText') }}
<input
v-model="text"
type="search"
class="rounded-sm border border-outline-variant bg-surface px-3 py-2 text-body-large text-on-surface"
:placeholder="$t('events.filterTextPlaceholder')"
data-test="text-filter"
>
</label>
<M3TextField
v-model="text"
class="max-w-md"
type="search"
:label="$t('events.filterText')"
:placeholder="$t('events.filterTextPlaceholder')"
data-test="text-filter"
/>
<p class="text-label-medium text-on-surface-variant">{{ $t('events.limit', { limit: LOG_LIMIT }) }}</p>
</section>

<M3EmptyState v-if="!log.events.length" :title="$t('events.empty')" icon="mdi-timeline-text-outline" />
<M3EmptyState v-else-if="!rows.length" :title="$t('events.noMatch')" icon="mdi-filter-off-outline" />
<div v-else class="overflow-x-auto rounded-md bg-surface-container-low">
<table class="w-full min-w-[40rem] text-left text-body-medium" data-test="events-table">
<thead class="text-label-large text-on-surface-variant">
<tr>
<th scope="col" class="px-3 py-2 font-medium">{{ $t('events.columns.time') }}</th>
<th scope="col" class="px-3 py-2 font-medium">{{ $t('events.columns.type') }}</th>
<th scope="col" class="px-3 py-2 font-medium">{{ $t('events.columns.subject') }}</th>
<th scope="col" class="px-3 py-2 font-medium">{{ $t('events.columns.summary') }}</th>
</tr>
</thead>
<tbody>
<tr v-for="row in rows" :key="row.id" class="border-t border-outline-variant align-top" data-test="event-row">
<td class="whitespace-nowrap px-3 py-2 tabular-nums">{{ row.time }}</td>
<td class="px-3 py-2"><M3Chip :tone="row.tone" small>{{ row.type }}</M3Chip></td>
<td class="break-all px-3 py-2">{{ row.subject ?? '—' }}</td>
<td class="px-3 py-2">
{{ row.summary }}
<details class="mt-1">
<summary class="cursor-pointer text-label-medium text-primary">{{ $t('events.showRaw') }}</summary>
<pre class="mt-2 max-h-64 overflow-auto rounded-sm bg-surface p-2 text-label-medium">{{ row.raw }}</pre>
</details>
</td>
</tr>
</tbody>
</table>
</div>
<M3DataTable
v-else
:columns="columns"
:items="rows"
row-key="id"
:paginate="false"
:row-attrs="() => ({ 'data-test': 'event-row' })"
data-test="events-table"
>
<template #item.time="{ item }">
<span class="whitespace-nowrap tabular-nums">{{ item.time }}</span>
</template>
<template #item.type="{ item }">
<M3Chip :tone="item.tone" small>{{ item.type }}</M3Chip>
</template>
<template #item.subject="{ item }">
<span class="break-all">{{ item.subject ?? '—' }}</span>
</template>
<template #item.summary="{ item }">
{{ item.summary }}
<details class="mt-1">
<summary class="cursor-pointer text-label-medium text-primary">{{ $t('events.showRaw') }}</summary>
<pre class="mt-2 max-h-64 overflow-auto rounded-sm bg-surface p-2 text-label-medium">{{ item.raw }}</pre>
</details>
</template>
</M3DataTable>
</div>
</template>

Expand All @@ -102,6 +100,12 @@ const log = ref<LiveLog>(emptyLog())
const typeFilter = ref<string[]>([])
const text = ref('')
const paused = computed(() => isPaused(log.value))
const columns = computed(() => [
{ key: 'time', title: t('events.columns.time'), sortable: false },
{ key: 'type', title: t('events.columns.type'), sortable: false },
{ key: 'subject', title: t('events.columns.subject'), sortable: false },
{ key: 'summary', title: t('events.columns.summary'), sortable: false },
])

const filter = computed<EventFilter>(() => ({ types: typeFilter.value, text: text.value }))
const translate: Translate = (key, params) => t(key, params ?? {})
Expand Down
Loading