astrbot_plugin_imagestudio/frontend/plugin-page/src/App.vue

479 行
11 KiB
Vue

2026-07-05 09:24:00 +08:00
<script setup lang="ts">
import { computed, onMounted, ref } from "vue"
import { endpoints } from "../../shared/api/endpoints"
import { getJson } from "../../shared/api/client"
import type { UiBootstrap } from "../../shared/types/bootstrap"
import type { DashboardImage, DashboardSummary } from "../../shared/types/dashboard"
import type { UiAction } from "../../shared/types/ui"
2026-07-05 09:24:00 +08:00
interface QuickLink {
label: string
href: string
}
2026-07-05 09:24:00 +08:00
const bootstrap = ref<UiBootstrap | null>(null)
const dashboard = ref<DashboardSummary | null>(null)
const uiActions = ref<UiAction[]>([])
2026-07-05 09:24:00 +08:00
const loading = ref(true)
const error = ref("")
const workbenchUrl = computed(() => `${bootstrap.value?.api_base ?? ""}/web`)
const queueTotal = computed(() => {
const queue = dashboard.value?.queue
return queue ? queue.pending + queue.running + queue.failed + queue.dead_letter : 0
})
const recentImages = computed(() => dashboard.value?.recent_images.slice(0, 4) ?? [])
const quickLinks = computed(() => {
const actionLinks = uiActions.value.map(actionToLink).filter((link): link is QuickLink => Boolean(link))
return actionLinks.length ? actionLinks : fallbackQuickLinks()
})
const metrics = computed(() => [
{ label: "服务状态", value: statusLabel(dashboard.value?.plugin_status ?? "loading") },
{ label: "队列", value: String(queueTotal.value) },
{ label: "运行中", value: String(dashboard.value?.queue.running ?? 0) },
{ label: "失败", value: String(dashboard.value?.queue.failed ?? 0) },
{ label: "存储", value: formatBytes(dashboard.value?.storage.total_bytes ?? 0) },
])
2026-07-05 09:24:00 +08:00
onMounted(load)
async function load() {
loading.value = true
error.value = ""
try {
const [bootstrapData, dashboardData] = await Promise.all([
getJson<UiBootstrap>(endpoints.uiBootstrap),
getJson<DashboardSummary>(endpoints.dashboardSummary),
])
bootstrap.value = bootstrapData
dashboard.value = dashboardData
try {
uiActions.value = await getJson<UiAction[]>(endpoints.uiActions)
} catch {
uiActions.value = []
}
2026-07-05 09:24:00 +08:00
} catch (err) {
error.value = err instanceof Error ? err.message : "加载失败"
2026-07-05 09:24:00 +08:00
} finally {
loading.value = false
}
}
function fallbackQuickLinks(): QuickLink[] {
return [
{ label: "队列", href: workbenchViewUrl("queue") },
{ label: "图库", href: workbenchViewUrl("gallery") },
{ label: "日志", href: workbenchViewUrl("logs") },
{ label: "设置", href: workbenchViewUrl("settings") },
]
}
function actionToLink(action: UiAction): QuickLink | null {
if (!bootstrap.value) return null
if (action.action_type === "open_workbench") {
return { label: actionLabel(action), href: String(action.payload.url || workbenchUrl.value) }
}
const view = action.payload.view
if (typeof view === "string") {
return { label: actionLabel(action), href: workbenchViewUrl(view) }
}
return null
}
function workbenchViewUrl(view: string) {
return `${workbenchUrl.value}?view=${encodeURIComponent(view)}`
}
function actionLabel(action: UiAction) {
const labels: Record<string, string> = {
open_workbench: "工作台",
quick_profile: "角色",
open_logs: "日志",
open_settings: "设置",
}
return labels[action.action_id] ?? action.label
}
function statusLabel(value: string) {
const labels: Record<string, string> = {
loading: "加载中",
running: "运行中",
degraded: "降级",
stopped: "已停止",
}
return labels[value] ?? value
}
function imageTitle(image: DashboardImage) {
return image.node_id || image.workflow_id || image.profile_id || shortId(image.image_id)
}
function imageUrl(image: DashboardImage) {
return image.thumbnail_url || image.image_url || ""
}
function shortId(value: string) {
return value.length > 10 ? `${value.slice(0, 6)}...${value.slice(-4)}` : value
}
function formatBytes(value: number) {
if (value < 1024) return `${value} B`
if (value < 1024 * 1024) return `${(value / 1024).toFixed(1)} KB`
if (value < 1024 * 1024 * 1024) return `${(value / 1024 / 1024).toFixed(1)} MB`
return `${(value / 1024 / 1024 / 1024).toFixed(1)} GB`
}
2026-07-05 09:24:00 +08:00
</script>
2026-07-04 22:51:52 +08:00
<template>
<main class="plugin-page-shell">
2026-07-05 09:24:00 +08:00
<header class="topbar">
<div class="brand">
<span>IS</span>
<div>
<h1>{{ bootstrap?.display_name ?? "ImageStudio 星图导航" }}</h1>
<p>{{ bootstrap?.dashboard_user ?? "AstrBot" }}</p>
</div>
2026-07-05 09:24:00 +08:00
</div>
<div class="actions">
<button type="button" :disabled="loading" @click="load">{{ loading ? "同步中" : "刷新" }}</button>
<a v-if="bootstrap" class="primary" :href="workbenchUrl" target="_blank" rel="noreferrer">工作台</a>
2026-07-05 09:24:00 +08:00
</div>
</header>
<section v-if="error" class="notice bad">{{ error }}</section>
<section v-else-if="loading" class="notice">加载中</section>
<template v-else>
<section class="summary-strip">
<article v-for="metric in metrics" :key="metric.label">
<span>{{ metric.label }}</span>
<strong>{{ metric.value }}</strong>
</article>
</section>
<section class="content-grid">
<section class="panel queue-panel">
<header>
<span>队列状态</span>
<h2>{{ queueTotal }} 个任务</h2>
</header>
<dl>
<div>
<dt>Pending</dt>
<dd>{{ dashboard?.queue.pending ?? 0 }}</dd>
</div>
<div>
<dt>Running</dt>
<dd>{{ dashboard?.queue.running ?? 0 }}</dd>
</div>
<div>
<dt>Failed</dt>
<dd>{{ dashboard?.queue.failed ?? 0 }}</dd>
</div>
<div>
<dt>Dead</dt>
<dd>{{ dashboard?.queue.dead_letter ?? 0 }}</dd>
</div>
</dl>
</section>
<section class="panel image-panel">
<header>
<span>最近图片</span>
<h2>{{ recentImages.length }} 张</h2>
</header>
<div v-if="recentImages.length" class="image-grid">
<article v-for="image in recentImages" :key="image.image_id">
<div class="thumb">
<img v-if="imageUrl(image)" :src="imageUrl(image)" :alt="imageTitle(image)" />
<span v-else>IMG</span>
</div>
<strong>{{ imageTitle(image) }}</strong>
</article>
</div>
<p v-else class="empty">暂无最近图片</p>
</section>
<section class="panel links-panel">
<header>
<span>快捷入口</span>
<h2>继续工作</h2>
</header>
<div class="quick-links">
<a v-for="link in quickLinks" :key="link.href" :href="link.href" target="_blank" rel="noreferrer">
{{ link.label }}
</a>
</div>
</section>
</section>
</template>
2026-07-04 22:51:52 +08:00
</main>
</template>
<style scoped>
.plugin-page-shell {
box-sizing: border-box;
min-height: 100vh;
padding: 22px;
color: #171923;
background:
linear-gradient(90deg, rgba(255, 255, 255, 0.05) 1px, transparent 1px),
linear-gradient(180deg, #151820 0%, #242730 100%);
background-size: 78px 78px, auto;
2026-07-04 22:51:52 +08:00
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
:global(body) {
margin: 0;
}
:global(#app) {
min-height: 100vh;
2026-07-05 09:24:00 +08:00
}
h1,
h2,
p,
dl,
dd {
2026-07-05 09:24:00 +08:00
margin: 0;
}
.topbar,
.brand,
.actions {
display: flex;
align-items: center;
gap: 12px;
}
.topbar {
justify-content: space-between;
color: #ffffff;
}
.brand {
min-width: 0;
}
.brand > span {
display: grid;
place-items: center;
width: 44px;
height: 44px;
border-radius: 8px;
color: #5a61d6;
background: #ffffff;
font-weight: 800;
}
.brand div {
min-width: 0;
}
2026-07-04 22:51:52 +08:00
h1 {
overflow: hidden;
font-size: 22px;
text-overflow: ellipsis;
white-space: nowrap;
2026-07-04 22:51:52 +08:00
}
2026-07-05 09:24:00 +08:00
h2 {
font-size: 20px;
2026-07-05 09:24:00 +08:00
}
p,
span,
dt,
2026-07-05 09:24:00 +08:00
.empty {
color: #6f7785;
2026-07-05 09:24:00 +08:00
}
.topbar p {
color: rgba(255, 255, 255, 0.68);
2026-07-05 09:24:00 +08:00
}
button,
a.primary,
.quick-links a {
display: inline-flex;
align-items: center;
justify-content: center;
2026-07-05 09:24:00 +08:00
min-height: 36px;
border: 1px solid rgba(255, 255, 255, 0.16);
2026-07-05 09:24:00 +08:00
border-radius: 6px;
padding: 0 14px;
color: #ffffff;
background: rgba(255, 255, 255, 0.1);
2026-07-05 09:24:00 +08:00
font: inherit;
text-decoration: none;
cursor: pointer;
}
button:disabled {
cursor: wait;
opacity: 0.68;
2026-07-05 09:24:00 +08:00
}
a.primary {
border-color: #7668e8;
background: #7668e8;
2026-07-05 09:24:00 +08:00
}
.notice,
.panel,
.summary-strip article {
border: 1px solid rgba(32, 41, 56, 0.14);
border-radius: 8px;
background: #f8f7f2;
box-shadow: 0 18px 45px rgba(9, 13, 29, 0.2);
2026-07-05 09:24:00 +08:00
}
.notice {
margin-top: 18px;
padding: 14px;
}
.notice.bad {
border-color: rgba(212, 71, 95, 0.34);
color: #9e1f3b;
}
.summary-strip {
2026-07-05 09:24:00 +08:00
display: grid;
grid-template-columns: repeat(5, minmax(120px, 1fr));
gap: 12px;
margin-top: 18px;
2026-07-05 09:24:00 +08:00
}
.summary-strip article {
2026-07-05 09:24:00 +08:00
display: grid;
gap: 8px;
padding: 16px;
}
.summary-strip strong {
2026-07-05 09:24:00 +08:00
font-size: 24px;
}
.content-grid {
display: grid;
grid-template-columns: minmax(220px, 0.8fr) minmax(280px, 1.1fr) minmax(220px, 0.8fr);
gap: 12px;
margin-top: 12px;
2026-07-05 09:24:00 +08:00
}
.panel {
display: grid;
align-content: start;
gap: 14px;
min-width: 0;
2026-07-05 09:24:00 +08:00
padding: 16px;
}
.queue-panel {
border-color: rgba(221, 173, 54, 0.42);
}
.image-panel {
border-color: rgba(238, 111, 54, 0.38);
}
.links-panel {
border-color: rgba(118, 104, 232, 0.38);
}
.panel header span {
font-size: 12px;
font-weight: 700;
}
dl {
2026-07-05 09:24:00 +08:00
display: grid;
gap: 8px;
2026-07-05 09:24:00 +08:00
}
dl div {
2026-07-05 09:24:00 +08:00
display: flex;
justify-content: space-between;
gap: 12px;
padding-top: 8px;
border-top: 1px solid rgba(32, 41, 56, 0.12);
}
dd {
font-weight: 800;
}
.image-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 10px;
}
.image-grid article {
display: grid;
gap: 7px;
2026-07-05 09:24:00 +08:00
min-width: 0;
padding: 8px;
border: 1px solid rgba(32, 41, 56, 0.12);
border-radius: 8px;
background: #ebe9e2;
}
.thumb {
display: grid;
place-items: center;
aspect-ratio: 4 / 3;
overflow: hidden;
border-radius: 8px;
color: #6f7785;
background: linear-gradient(135deg, rgba(118, 104, 232, 0.18), transparent), #ffffff;
font-weight: 800;
}
.thumb img {
width: 100%;
height: 100%;
object-fit: cover;
2026-07-05 09:24:00 +08:00
}
.image-grid strong {
2026-07-05 09:24:00 +08:00
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.quick-links {
display: grid;
gap: 8px;
}
.quick-links a {
justify-content: flex-start;
color: #171923;
background: #ebe9e2;
}
@media (max-width: 900px) {
.topbar {
align-items: flex-start;
flex-direction: column;
}
.summary-strip,
.content-grid {
2026-07-05 09:24:00 +08:00
grid-template-columns: 1fr;
}
.actions {
flex-wrap: wrap;
}
2026-07-04 22:51:52 +08:00
}
</style>