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

372 行
8.5 KiB
Vue

<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 { DashboardSummary } from "../../shared/types/dashboard"
const bootstrap = ref<UiBootstrap | null>(null)
const dashboard = ref<DashboardSummary | null>(null)
const loading = ref(true)
const error = ref("")
const text = {
loadFailed: "\u52a0\u8f7d\u5931\u8d25",
loading: "\u52a0\u8f7d\u4e2d",
refresh: "\u5237\u65b0",
workbench: "\u5de5\u4f5c\u53f0",
profiles: "\u89d2\u8272",
queue: "\u961f\u5217",
running: "\u8fd0\u884c\u4e2d",
failed: "\u5931\u8d25",
storage: "\u5b58\u50a8",
recentImages: "\u6700\u8fd1\u56fe\u7247",
recentJobs: "\u4efb\u52a1",
emptyImages: "\u6682\u65e0\u56fe\u7247",
emptyJobs: "\u6682\u65e0\u4efb\u52a1",
permissions: "\u6743\u9650",
features: "\u529f\u80fd",
quickLinks: "\u5feb\u6377\u5165\u53e3",
openJobs: "\u961f\u5217",
openLogs: "\u65e5\u5fd7",
openSettings: "\u8bbe\u7f6e",
openMaterials: "\u7d20\u6750",
enabled: "\u5df2\u5f00\u542f",
disabled: "\u672a\u5f00\u542f",
} as const
const workbenchUrl = computed(() => `${bootstrap.value?.api_base ?? ""}/web`)
const quickLinks = computed(() => [
{ label: text.openJobs, href: workbenchViewUrl("jobs") },
{ label: text.openLogs, href: workbenchViewUrl("logs") },
{ label: text.openSettings, href: workbenchViewUrl("settings") },
{ label: text.openMaterials, href: workbenchViewUrl("materials") },
])
const queueTotal = computed(() => {
const queue = dashboard.value?.queue
return queue ? queue.pending + queue.running + queue.failed + queue.dead_letter : 0
})
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
} catch (err) {
error.value = err instanceof Error ? err.message : text.loadFailed
} finally {
loading.value = false
}
}
function formatBytes(value = 0) {
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`
}
function workbenchViewUrl(view: string) {
return `${workbenchUrl.value}?view=${encodeURIComponent(view)}`
}
</script>
<template>
<main class="plugin-page-shell">
<header class="topbar">
<div>
<h1>{{ bootstrap?.display_name ?? "ImageStudio" }}</h1>
<p>{{ bootstrap?.dashboard_user ?? "AstrBot" }}</p>
</div>
<div class="actions">
<span class="status" :data-state="dashboard?.plugin_status ?? 'loading'">
{{ dashboard?.plugin_status ?? "loading" }}
</span>
<button type="button" @click="load">{{ text.refresh }}</button>
<a v-if="bootstrap" class="primary" :href="workbenchUrl" target="_blank" rel="noreferrer">
{{ text.workbench }}
</a>
</div>
</header>
<section v-if="error" class="notice">{{ error }}</section>
<section v-else-if="loading" class="notice">{{ text.loading }}</section>
<section v-else class="metrics" aria-label="ImageStudio dashboard">
<article>
<span>{{ text.profiles }}</span>
<strong>{{ dashboard?.active_profiles ?? 0 }}</strong>
</article>
<article>
<span>{{ text.queue }}</span>
<strong>{{ queueTotal }}</strong>
</article>
<article>
<span>{{ text.running }}</span>
<strong>{{ dashboard?.queue.running ?? 0 }}</strong>
</article>
<article>
<span>{{ text.failed }}</span>
<strong>{{ dashboard?.queue.failed ?? 0 }}</strong>
</article>
<article>
<span>{{ text.storage }}</span>
<strong>{{ formatBytes(dashboard?.storage.total_bytes) }}</strong>
</article>
</section>
<section class="activity">
<div>
<h2>{{ text.recentImages }}</h2>
<ul>
<li v-for="image in dashboard?.recent_images ?? []" :key="String((image as any).image_id)">
<span>{{ (image as any).image_id }}</span>
<small>{{ (image as any).created_at }}</small>
</li>
<li v-if="!dashboard?.recent_images?.length" class="empty">{{ text.emptyImages }}</li>
</ul>
</div>
<div>
<h2>{{ text.recentJobs }}</h2>
<ul>
<li v-for="job in dashboard?.recent_jobs ?? []" :key="String((job as any).task_id)">
<span>{{ (job as any).task_id }}</span>
<small>{{ (job as any).status }}</small>
</li>
<li v-if="!dashboard?.recent_jobs?.length" class="empty">{{ text.emptyJobs }}</li>
</ul>
</div>
</section>
<section v-if="bootstrap" class="activity">
<div>
<h2>{{ text.permissions }}</h2>
<ul>
<li v-for="(value, key) in bootstrap.permissions" :key="key">
<span>{{ key }}</span>
<small>{{ value ? text.enabled : text.disabled }}</small>
</li>
</ul>
</div>
<div>
<h2>{{ text.features }}</h2>
<ul>
<li v-for="(value, key) in bootstrap.feature_flags" :key="key">
<span>{{ key }}</span>
<small>{{ value ? text.enabled : text.disabled }}</small>
</li>
</ul>
</div>
</section>
<section v-if="bootstrap" class="quick-links">
<h2>{{ text.quickLinks }}</h2>
<div>
<a v-for="link in quickLinks" :key="link.href" :href="link.href" target="_blank" rel="noreferrer">
{{ link.label }}
</a>
</div>
</section>
</main>
</template>
<style scoped>
.plugin-page-shell {
box-sizing: border-box;
min-height: 100vh;
padding: 24px;
color: #1d2430;
background: #f4f6f8;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
.topbar,
.activity {
display: grid;
gap: 16px;
grid-template-columns: minmax(0, 1fr) auto;
align-items: start;
}
h1,
h2,
p {
margin: 0;
}
h1 {
font-size: 24px;
font-weight: 700;
}
h2 {
font-size: 14px;
}
p,
small,
.empty {
color: #667085;
}
.actions {
display: flex;
gap: 8px;
align-items: center;
}
button,
a.primary {
min-height: 36px;
padding: 0 14px;
border: 1px solid #c7d0da;
border-radius: 6px;
color: #1d2430;
background: #ffffff;
font: inherit;
text-decoration: none;
cursor: pointer;
}
a.primary {
display: inline-flex;
align-items: center;
border-color: #256f5b;
color: #ffffff;
background: #256f5b;
}
.status {
display: inline-flex;
align-items: center;
min-height: 28px;
padding: 0 10px;
border-radius: 999px;
color: #245347;
background: #dff3eb;
font-size: 13px;
}
.status[data-state="loading"] {
color: #6b4f1d;
background: #f7e7c5;
}
.notice {
margin-top: 18px;
padding: 14px;
border: 1px solid #d6dde6;
border-radius: 8px;
background: #ffffff;
}
.metrics {
display: grid;
grid-template-columns: repeat(5, minmax(120px, 1fr));
gap: 12px;
margin-top: 20px;
}
article,
.activity > div {
border: 1px solid #d6dde6;
border-radius: 8px;
background: #ffffff;
}
article {
display: grid;
gap: 8px;
padding: 16px;
}
article span {
color: #667085;
font-size: 13px;
}
article strong {
font-size: 24px;
}
.activity {
grid-template-columns: repeat(2, minmax(0, 1fr));
margin-top: 16px;
}
.activity > div {
padding: 16px;
}
.quick-links {
margin-top: 16px;
padding: 16px;
border: 1px solid #d6dde6;
border-radius: 8px;
background: #ffffff;
}
.quick-links > div {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-top: 12px;
}
.quick-links a {
display: inline-flex;
align-items: center;
min-height: 34px;
padding: 0 12px;
border: 1px solid #c7d0da;
border-radius: 6px;
color: #1d2430;
background: #ffffff;
text-decoration: none;
}
ul {
display: grid;
gap: 10px;
margin: 12px 0 0;
padding: 0;
list-style: none;
}
li {
display: flex;
justify-content: space-between;
gap: 12px;
min-width: 0;
}
li span,
li small {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
@media (max-width: 820px) {
.topbar,
.activity {
grid-template-columns: 1fr;
}
.actions {
flex-wrap: wrap;
}
.metrics {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
</style>