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

454 行
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 { DashboardSummary } from "../../shared/types/dashboard"
import type { UiAction } from "../../shared/types/ui"
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("")
2026-07-05 15:10:49 +08:00
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",
2026-07-05 15:10:49 +08:00
enabled: "\u5df2\u5f00\u542f",
disabled: "\u672a\u5f00\u542f",
} as const
const statusLabels: Record<string, string> = {
loading: "\u52a0\u8f7d\u4e2d",
running: "\u8fd0\u884c\u4e2d",
created: "\u5df2\u521b\u5efa",
pending: "\u7b49\u5f85\u4e2d",
queued: "\u6392\u961f\u4e2d",
completed: "\u5df2\u5b8c\u6210",
succeeded: "\u5df2\u6210\u529f",
failed: "\u5931\u8d25",
cancelled: "\u5df2\u53d6\u6d88",
dead_letter: "\u6b7b\u4fe1",
}
const permissionLabels: Record<string, string> = {
can_read: "\u8bfb\u53d6",
can_write: "\u5199\u5165",
can_manage_config: "\u914d\u7f6e\u7ba1\u7406",
can_manage_materials: "\u7d20\u6750\u7ba1\u7406",
can_retry_tasks: "\u91cd\u8bd5\u4efb\u52a1",
}
const featureLabels: Record<string, string> = {
quick_profile: "\u5feb\u901f\u5efa\u6863",
workspace: "\u5de5\u4f5c\u53f0",
material_library: "\u7d20\u6750\u5e93",
wardrobe: "\u8863\u6a71",
api_discovery: "\u80fd\u529b\u63a2\u6d4b",
private_companion_integration: "\u79c1\u4f34\u63d2\u4ef6\u96c6\u6210",
}
2026-07-05 09:24:00 +08:00
const workbenchUrl = computed(() => `${bootstrap.value?.api_base ?? ""}/web`)
const quickLinks = computed(() => {
const actionLinks = uiActions.value.map(actionToLink).filter((link): link is { label: string; href: string } => Boolean(link))
return actionLinks.length ? actionLinks : fallbackQuickLinks()
})
2026-07-05 09:24:00 +08:00
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
try {
uiActions.value = await getJson<UiAction[]>(endpoints.uiActions)
} catch {
uiActions.value = []
}
2026-07-05 09:24:00 +08:00
} catch (err) {
2026-07-05 15:10:49 +08:00
error.value = err instanceof Error ? err.message : text.loadFailed
2026-07-05 09:24:00 +08:00
} 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)}`
}
function fallbackQuickLinks() {
return [
{ label: text.openJobs, href: workbenchViewUrl("jobs") },
{ label: text.openLogs, href: workbenchViewUrl("logs") },
{ label: text.openSettings, href: workbenchViewUrl("settings") },
{ label: text.openMaterials, href: workbenchViewUrl("materials") },
]
}
function actionToLink(action: UiAction) {
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 actionLabel(action: UiAction) {
const labels: Record<string, string> = {
open_workbench: text.workbench,
quick_profile: text.profiles,
open_logs: text.openLogs,
open_settings: text.openSettings,
}
return labels[action.action_id] ?? action.label
}
function statusLabel(value: unknown) {
const key = String(value || "").trim()
return statusLabels[key] ?? key
}
function permissionLabel(value: unknown) {
const key = String(value || "").trim()
return permissionLabels[key] ?? key
}
function featureLabel(value: unknown) {
const key = String(value || "").trim()
return featureLabels[key] ?? key
}
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>
<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'">
{{ statusLabel(dashboard?.plugin_status ?? "loading") }}
2026-07-05 09:24:00 +08:00
</span>
2026-07-05 15:10:49 +08:00
<button type="button" @click="load">{{ text.refresh }}</button>
<a v-if="bootstrap" class="primary" :href="workbenchUrl" target="_blank" rel="noreferrer">
{{ text.workbench }}
</a>
2026-07-05 09:24:00 +08:00
</div>
</header>
<section v-if="error" class="notice">{{ error }}</section>
2026-07-05 15:10:49 +08:00
<section v-else-if="loading" class="notice">{{ text.loading }}</section>
2026-07-05 09:24:00 +08:00
<section v-else class="metrics" aria-label="ImageStudio dashboard">
<article>
2026-07-05 15:10:49 +08:00
<span>{{ text.profiles }}</span>
2026-07-05 09:24:00 +08:00
<strong>{{ dashboard?.active_profiles ?? 0 }}</strong>
</article>
<article>
2026-07-05 15:10:49 +08:00
<span>{{ text.queue }}</span>
2026-07-05 09:24:00 +08:00
<strong>{{ queueTotal }}</strong>
</article>
<article>
2026-07-05 15:10:49 +08:00
<span>{{ text.running }}</span>
2026-07-05 09:24:00 +08:00
<strong>{{ dashboard?.queue.running ?? 0 }}</strong>
</article>
<article>
2026-07-05 15:10:49 +08:00
<span>{{ text.failed }}</span>
2026-07-05 09:24:00 +08:00
<strong>{{ dashboard?.queue.failed ?? 0 }}</strong>
</article>
<article>
2026-07-05 15:10:49 +08:00
<span>{{ text.storage }}</span>
2026-07-05 09:24:00 +08:00
<strong>{{ formatBytes(dashboard?.storage.total_bytes) }}</strong>
</article>
</section>
<section class="activity">
<div>
2026-07-05 15:10:49 +08:00
<h2>{{ text.recentImages }}</h2>
2026-07-05 09:24:00 +08:00
<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>
2026-07-05 15:10:49 +08:00
<li v-if="!dashboard?.recent_images?.length" class="empty">{{ text.emptyImages }}</li>
2026-07-05 09:24:00 +08:00
</ul>
</div>
<div>
2026-07-05 15:10:49 +08:00
<h2>{{ text.recentJobs }}</h2>
2026-07-05 09:24:00 +08:00
<ul>
<li v-for="job in dashboard?.recent_jobs ?? []" :key="String((job as any).task_id)">
<span>{{ (job as any).task_id }}</span>
<small>{{ statusLabel((job as any).status) }}</small>
2026-07-05 09:24:00 +08:00
</li>
2026-07-05 15:10:49 +08:00
<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>{{ permissionLabel(key) }}</span>
2026-07-05 15:10:49 +08:00
<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>{{ featureLabel(key) }}</span>
2026-07-05 15:10:49 +08:00
<small>{{ value ? text.enabled : text.disabled }}</small>
</li>
2026-07-05 09:24:00 +08:00
</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>
2026-07-04 22:51:52 +08:00
</main>
</template>
<style scoped>
.plugin-page-shell {
box-sizing: border-box;
min-height: 100vh;
padding: 24px;
2026-07-05 09:24:00 +08:00
color: #1d2430;
background: #f4f6f8;
2026-07-04 22:51:52 +08:00
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
2026-07-05 09:24:00 +08:00
.topbar,
.activity {
display: grid;
gap: 16px;
grid-template-columns: minmax(0, 1fr) auto;
align-items: start;
}
h1,
h2,
p {
margin: 0;
}
2026-07-04 22:51:52 +08:00
h1 {
font-size: 24px;
font-weight: 700;
}
2026-07-05 09:24:00 +08:00
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;
}
2026-07-05 09:24:00 +08:00
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));
}
2026-07-04 22:51:52 +08:00
}
</style>