166 lines
4.7 KiB
Svelte
166 lines
4.7 KiB
Svelte
<script lang="ts">
|
|
import { resolve } from '$app/paths';
|
|
import { createQuery } from '@tanstack/svelte-query';
|
|
import { toStore } from 'svelte/store';
|
|
import { workflows, type Job } from '$lib/api';
|
|
import { parseDbTs } from '$lib/dbtime';
|
|
import { t } from '$lib/i18n.svelte';
|
|
import { jobSourceLabel } from '$lib/jobLabels';
|
|
import { jobStatusInfo, type JobStatusTone } from '$lib/jobStatus';
|
|
import ShellPop from './ShellPop.svelte';
|
|
|
|
/**
|
|
* 顶栏队列胶囊 + 任务托盘 (§二). The dot is cyan only while something is
|
|
* actually generating — queued work stays gray, as on the task page.
|
|
*/
|
|
let { jobs }: { jobs: Job[] } = $props();
|
|
|
|
const TRAY_LIMIT = 6;
|
|
const TAG_CLASS: Record<JobStatusTone, string> = { ok: 'ok', run: 'run', queue: '', fail: 'err' };
|
|
|
|
let open = $state(false);
|
|
let capsule = $state<HTMLButtonElement | null>(null);
|
|
|
|
const workflowsQuery = createQuery(
|
|
toStore(() => ({
|
|
queryKey: ['workflows'],
|
|
queryFn: workflows.list,
|
|
enabled: open,
|
|
staleTime: 60_000,
|
|
})),
|
|
);
|
|
|
|
const active = $derived(jobs.filter((j) => j.status === 'pending' || j.status === 'running'));
|
|
const running = $derived(active.filter((j) => jobStatusInfo(j).tone === 'run').length);
|
|
const queued = $derived(active.length - running);
|
|
const recent = $derived([...jobs].sort((a, b) => b.id - a.id).slice(0, TRAY_LIMIT));
|
|
|
|
const label = $derived.by(() => {
|
|
const parts: string[] = [];
|
|
if (running > 0) parts.push(t('shell.queueRunning', { n: running }));
|
|
if (queued > 0) parts.push(t('shell.queueQueued', { n: queued }));
|
|
return parts.length ? parts.join(' · ') : t('shell.queueIdle');
|
|
});
|
|
|
|
// 任务数变化时胶囊轻弹一次(320ms,只播一次).
|
|
let lastCount: number | null = null;
|
|
$effect(() => {
|
|
const count = active.length;
|
|
if (lastCount !== null && count !== lastCount && capsule) {
|
|
capsule.classList.remove('bump');
|
|
void capsule.offsetWidth;
|
|
capsule.classList.add('bump');
|
|
}
|
|
lastCount = count;
|
|
});
|
|
|
|
function workflowName(job: Job): string {
|
|
return (
|
|
($workflowsQuery.data ?? []).find((w) => w.id === job.workflow_id)?.name ??
|
|
t('studio.jobName', { id: job.id })
|
|
);
|
|
}
|
|
|
|
function timeOf(job: Job): string {
|
|
const ts = parseDbTs(job.created_at);
|
|
if (!Number.isFinite(ts)) return '—';
|
|
const d = new Date(ts);
|
|
const p = (n: number) => String(n).padStart(2, '0');
|
|
return `${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}`;
|
|
}
|
|
</script>
|
|
|
|
<div class="tray">
|
|
<button
|
|
bind:this={capsule}
|
|
type="button"
|
|
class="status-chip queue-btn"
|
|
aria-haspopup="dialog"
|
|
aria-expanded={open}
|
|
aria-label={label}
|
|
title={label}
|
|
onclick={() => (open = !open)}
|
|
onanimationend={() => capsule?.classList.remove('bump')}
|
|
>
|
|
<span class="status-chip-dot {running > 0 ? 'run' : 'idle'}" aria-hidden="true"></span>
|
|
<span class="queue-label">{label}</span>
|
|
<span class="queue-count mono">{active.length}</span>
|
|
</button>
|
|
|
|
<ShellPop bind:open label={t('shell.trayTitle')} align="end" width="340px">
|
|
<div class="a-menu-group">{t('shell.trayRecent')}</div>
|
|
{#if recent.length === 0}
|
|
<p class="tray-note">{t('shell.trayEmpty')}</p>
|
|
{:else}
|
|
{#each recent as job (job.id)}
|
|
{@const info = jobStatusInfo(job)}
|
|
<a class="a-menu-item tray-row" href={resolve('/tasks')} title={info.detail} onclick={() => (open = false)}>
|
|
<span class="tray-copy">
|
|
<span class="a-menu-label">{workflowName(job)}</span>
|
|
<span class="tray-meta">{jobSourceLabel(job)} · <span class="mono">{timeOf(job)}</span></span>
|
|
</span>
|
|
<span class="a-tag {TAG_CLASS[info.tone]}">{info.label}</span>
|
|
</a>
|
|
{/each}
|
|
{/if}
|
|
<a class="a-menu-item tray-all" href={resolve('/tasks')} onclick={() => (open = false)}>
|
|
<span class="a-menu-label">{t('shell.trayAll')}</span>
|
|
</a>
|
|
</ShellPop>
|
|
</div>
|
|
|
|
<style>
|
|
.tray {
|
|
position: relative;
|
|
flex-shrink: 0;
|
|
}
|
|
.queue-btn:global(.bump) {
|
|
animation: queue-bump 320ms var(--ease-out);
|
|
}
|
|
@keyframes queue-bump {
|
|
40% {
|
|
transform: scale(1.06);
|
|
}
|
|
}
|
|
.queue-count {
|
|
display: none;
|
|
}
|
|
@media (max-width: 1179px) {
|
|
.queue-label {
|
|
display: none;
|
|
}
|
|
.queue-count {
|
|
display: inline;
|
|
}
|
|
}
|
|
.tray-row {
|
|
align-items: center;
|
|
}
|
|
.tray-copy {
|
|
display: flex;
|
|
flex-direction: column;
|
|
flex: 1;
|
|
min-width: 0;
|
|
line-height: 1.4;
|
|
}
|
|
.tray-meta {
|
|
font-size: var(--fs-mini);
|
|
color: var(--text-3);
|
|
white-space: nowrap;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
}
|
|
.tray-note {
|
|
margin: 0;
|
|
padding: var(--sp-3) var(--sp-4) var(--sp-5);
|
|
font-size: var(--fs-aux);
|
|
color: var(--text-3);
|
|
}
|
|
.tray-all {
|
|
margin-top: var(--sp-1);
|
|
border-top: 1px solid var(--line);
|
|
border-radius: 0 0 var(--r-md) var(--r-md);
|
|
justify-content: center;
|
|
}
|
|
</style>
|