Files
web-nuxt/app/pages/control.vue
Noel Sabia 66b80a62fb
ci / check (push) Successful in 1m20s
ci / check (pull_request) Successful in 51s
docs: tighten README quick start and stale comments
- Lead with purpose and exact local/CI commands.
- Remove local absolute paths, stale auth notes and unverified claims.
- Keep comments to the reason or invariant they protect.
2026-11-14 09:16:19 +01:00

270 lines
9.4 KiB
Vue

<script setup lang="ts">
import {
iconArrowRight,
iconCircleCheck,
iconCircleX,
iconPresentionBoard,
iconSync,
iconTriangleWarning,
} from '@sit-onyx/icons'
import { scenarioLabel } from '#shared/live'
import type { ScenarioPreset, ScenarioState } from '#shared/types'
import { apiErrorMessage, apiErrorStatus } from '~/utils/api-error'
import { formatDateTime } from '~/utils/format'
useHead({ title: 'Presenter control — PulseCast' })
const { data: initialScenario } = await useFetch<ScenarioState>('/api/scenario')
const { data: presets, error: presetsError } = await useFetch<ScenarioPreset[]>('/api/scenarios')
const live = useLiveScenario(initialScenario.value)
const submitted = ref<ScenarioState | null>(null)
const busy = ref('')
const message = ref('')
const errorMessage = ref('')
const current = computed(() => {
const liveState = live.scenario
const local = submitted.value
if (!liveState) return local ?? initialScenario.value ?? null
if (!local) return liveState
// A remote presenter can move the stage on; always show the newer version.
return liveState.version >= local.version ? liveState : local
})
const activeId = computed(() => current.value?.scenario.scenario ?? '')
const presetList = computed(() => presets.value ?? [])
function applyBody(preset: ScenarioPreset) {
return {
scenario: preset.id,
weather: preset.weather ?? {},
event: preset.event,
}
}
async function apply(preset: ScenarioPreset) {
if (busy.value) return
busy.value = preset.id
message.value = ''
errorMessage.value = ''
try {
const response = await $fetch<ScenarioState>('/api/scenario', {
method: 'POST',
body: applyBody(preset),
})
submitted.value = response
message.value = `${preset.label} is now live · version ${response.version}.`
} catch (error) {
errorMessage.value =
apiErrorMessage(error, 'The scenario change failed. Check the Go API and control credentials.') +
(apiErrorStatus(error) === 401 ? ' Reload this page to sign in again.' : '')
} finally {
busy.value = ''
}
}
</script>
<template>
<div class="page page-stack control-page">
<header class="page-header">
<div>
<p class="eyebrow">Presenter control · not linked in public navigation</p>
<OnyxHeadline is="h1">Change the scenario once. Every screen follows.</OnyxHeadline>
<p class="lede">
Each button POSTs to this Nuxt origin. Nitro attaches the Go control credentials server-side; the browser
never sees them. <NuxtLink to="/stage">Stage</NuxtLink> and <NuxtLink to="/live">Live</NuxtLink> update through
SSE, with a 5-second polling fallback.
</p>
</div>
<div class="hero-actions">
<LiveStatusBadge :status="live.status" :last-event-at="live.lastEventAt" />
<OnyxBadge v-if="current" color="neutral" density="compact">v{{ current.version }}</OnyxBadge>
</div>
</header>
<div v-if="errorMessage" class="callout callout--warning" role="alert">
<OnyxIcon :icon="iconCircleX" size="16px" color="danger" />
<span>{{ errorMessage }}</span>
</div>
<div v-if="message" class="callout" role="status">
<OnyxIcon :icon="iconCircleCheck" size="16px" color="success" />
<span>{{ message }}</span>
</div>
<div class="split-grid">
<div class="card-stack">
<OnyxCard>
<div class="card-header">
<div>
<OnyxHeadline is="h2">Scenario presets</OnyxHeadline>
<p class="small muted" style="margin: 0">
The enum is fixed by the API contract. Weather and event values match the demo narrative.
</p>
</div>
<OnyxBadge color="info" density="compact">server proxy</OnyxBadge>
</div>
<div v-if="presetList.length" class="scenario-grid">
<button
v-for="preset in presetList"
:key="preset.id"
type="button"
class="scenario-button"
:class="{ 'scenario-button--active': preset.id === activeId }"
:disabled="Boolean(busy)"
:aria-pressed="preset.id === activeId"
@click="apply(preset)"
>
<span class="scenario-button__head">
<strong>{{ preset.label }}</strong>
<OnyxBadge v-if="preset.id === activeId" color="success" density="compact">on stage</OnyxBadge>
<OnyxBadge v-else color="neutral" density="compact">{{ preset.short }}</OnyxBadge>
</span>
<span class="small muted">{{ preset.description }}</span>
<span v-if="busy === preset.id" class="scenario-button__busy">
<OnyxIcon :icon="iconSync" size="16px" color="primary" class="spin" />
Broadcasting…
</span>
<span v-else class="scenario-button__action">
Apply
<OnyxIcon :icon="iconArrowRight" size="16px" color="primary" />
</span>
</button>
</div>
<div v-else class="callout callout--warning">
<OnyxIcon :icon="iconTriangleWarning" size="16px" color="warning" />
Scenario presets could not be loaded.
<span v-if="presetsError" class="muted">{{ presetsError.statusMessage }}</span>
</div>
</OnyxCard>
<OnyxCard>
<div class="card-header">
<OnyxHeadline is="h2">Open the audience views</OnyxHeadline>
<OnyxIcon :icon="iconPresentionBoard" size="24px" color="primary" />
</div>
<div class="hero-actions">
<OnyxButton label="Open /stage" link="/stage" color="primary" />
<OnyxButton label="Open /live" link="/live" mode="outline" />
<OnyxButton label="Open /plan" link="/plan" mode="outline" />
</div>
<p class="small muted table-note">
Put <code>/stage</code> on the projector and let the audience scan the QR code. <code>/live</code> needs no
login and keeps the last known plan if the stream drops.
</p>
</OnyxCard>
</div>
<aside class="card-stack">
<OnyxCard>
<div class="card-header">
<OnyxHeadline is="h2">Current scenario</OnyxHeadline>
<LiveStatusBadge :status="live.status" :last-event-at="live.lastEventAt" compact />
</div>
<p class="control-current">{{ current ? scenarioLabel(current.scenario.scenario) : 'Unavailable' }}</p>
<dl class="metric-list">
<div><dt>Version</dt><dd>{{ current?.version ?? '—' }}</dd></div>
<div><dt>Broadcast at</dt><dd>{{ current ? formatDateTime(current.at) : '—' }}</dd></div>
<div><dt>Last SSE event</dt><dd>{{ live.lastEventAt ? formatDateTime(live.lastEventAt) : '—' }}</dd></div>
<div><dt>Transport</dt><dd>{{ live.statusLabel }}</dd></div>
</dl>
<p v-if="live.error" class="small muted table-note">{{ live.error }}</p>
</OnyxCard>
<OnyxCard>
<div class="card-header">
<OnyxHeadline is="h3">What the presenter says</OnyxHeadline>
</div>
<ul class="why-list">
<li><span><strong>Base week</strong> — normal weather, no event uplift.</span></li>
<li><span><strong>Sunny Saturday + market</strong> — the Saturday plan should add staff.</span></li>
<li><span><strong>Rainy Saturday</strong> — the Saturday plan should shed staff.</span></li>
<li><span><strong>Wednesday event</strong> — the weekday evening block should rise.</span></li>
</ul>
<p class="small muted table-note">
If the numbers do not move on stage, check the <code>cached</code> badge: Go may be serving its last good
forecast because the ML service is down.
</p>
</OnyxCard>
</aside>
</div>
</div>
</template>
<style scoped>
.control-current {
margin: 0 0 var(--onyx-density-md);
color: var(--onyx-color-text-icons-primary-intense);
font-size: clamp(1.6rem, 3vw, 2.4rem);
font-weight: 700;
line-height: 1.1;
}
.scenario-button {
display: flex;
flex-direction: column;
gap: var(--onyx-density-xs);
width: 100%;
padding: var(--onyx-density-md);
border: 1px solid var(--onyx-color-base-neutral-300);
border-radius: var(--onyx-radius-component-container-soft);
background: var(--onyx-color-base-neutral-100);
color: inherit;
font: inherit;
text-align: left;
cursor: pointer;
}
.scenario-button:hover:not(:disabled),
.scenario-button:focus-visible {
border-color: var(--onyx-color-base-primary-500);
outline: 2px solid var(--onyx-color-base-primary-300);
outline-offset: 1px;
}
.scenario-button:disabled {
cursor: progress;
opacity: 0.65;
}
.scenario-button--active {
border-color: var(--onyx-color-base-primary-600);
background: var(--onyx-color-base-primary-100);
}
.scenario-button__head,
.scenario-button__action,
.scenario-button__busy {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: var(--onyx-density-xs);
}
.scenario-button__action,
.scenario-button__busy {
justify-content: flex-start;
margin-top: var(--onyx-density-xs);
color: var(--onyx-color-text-icons-neutral-medium);
font-size: 0.8125rem;
}
.spin {
animation: spin 0.9s linear infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
@media (prefers-reduced-motion: reduce) {
.spin {
animation: none;
}
}
</style>