488 lines
12 KiB
Svelte
488 lines
12 KiB
Svelte
<script lang="ts">
|
|
import { parseDbTs } from "$lib/dbtime";
|
|
import { resolve } from "$app/paths";
|
|
import { goto } from "$app/navigation";
|
|
import { createMutation, useQueryClient } from "@tanstack/svelte-query";
|
|
import { assetUrl, projects, type Project } from "$lib/api";
|
|
import { toast } from "$lib/toast";
|
|
import Button from "$lib/components/ui/Button.svelte";
|
|
import ConfirmDialog from "$lib/components/ui/ConfirmDialog.svelte";
|
|
import Icon from "$lib/components/ui/Icon.svelte";
|
|
import Modal from "$lib/components/ui/Modal.svelte";
|
|
import { t } from "$lib/i18n.svelte";
|
|
|
|
/**
|
|
* A「片场暗房」project card for the home overview (a1, HEL-608).
|
|
* All numbers come from the real project record — no sample progress.
|
|
*/
|
|
|
|
interface Props {
|
|
project: Project;
|
|
}
|
|
|
|
let { project }: Props = $props();
|
|
|
|
const client = useQueryClient();
|
|
|
|
const stats = $derived(
|
|
project.stats ?? {
|
|
scene_count: 0,
|
|
character_count: 0,
|
|
asset_ready_count: 0,
|
|
asset_total_count: 0,
|
|
stage_confirmed: 0,
|
|
stage_status: {} as Record<string, string>,
|
|
active_jobs: 0,
|
|
},
|
|
);
|
|
|
|
// Five-stage creation progress (HEL-614) — real stage rows only.
|
|
const STAGE_SEGMENTS = [
|
|
"script",
|
|
"style",
|
|
"storyboard",
|
|
"assets",
|
|
"shot",
|
|
] as const;
|
|
function segClass(key: string): string {
|
|
const state = stats.stage_status?.[key];
|
|
if (state === "confirmed") return "seg-confirmed";
|
|
if (state === "running") return "seg-running";
|
|
if (state === "awaiting_confirm") return "seg-draft";
|
|
return "";
|
|
}
|
|
|
|
// Spec §2.1 allows the five avatar gradients as cover placeholders.
|
|
const placeholder = "var(--bg-3)";
|
|
// HEL-680 灰阶封面占位:1–2 亮阶配深图标,3–5 暗阶配浅图标
|
|
const placeholderIconColor = $derived("var(--text-2)");
|
|
|
|
const coverUrl = $derived(assetUrl(project.cover_path));
|
|
|
|
let coverFailed = $state(false);
|
|
|
|
// Cover tag reflects the real five-stage state (a1: 「分镜设计 · 进行中」):
|
|
// all confirmed → 已完成; otherwise the first unconfirmed stage + its state.
|
|
const cover = $derived.by(() => {
|
|
if (stats.stage_confirmed >= STAGE_SEGMENTS.length) {
|
|
return { cls: "st-done", label: t("home.statusDone") };
|
|
}
|
|
const current = STAGE_SEGMENTS.find(
|
|
(k) => stats.stage_status?.[k] !== "confirmed",
|
|
);
|
|
const state = current ? stats.stage_status?.[current] : undefined;
|
|
const stageName = current ? t(`workbench.stage.${current}`) : "";
|
|
if (state === "running") {
|
|
return {
|
|
cls: "st-progress",
|
|
label: `${stageName} · ${t("workbench.state.running")}`,
|
|
};
|
|
}
|
|
if (state === "awaiting_confirm") {
|
|
return {
|
|
cls: "st-pending",
|
|
label: `${stageName} · ${t("workbench.state.draft")}`,
|
|
};
|
|
}
|
|
if (stats.stage_confirmed > 0) {
|
|
return {
|
|
cls: "st-progress",
|
|
label: `${stageName} · ${t("home.statusProgress")}`,
|
|
};
|
|
}
|
|
return { cls: "st-draft", label: t("home.statusDraft") };
|
|
});
|
|
|
|
function relativeTime(iso: string): string {
|
|
const then = parseDbTs(iso);
|
|
if (Number.isNaN(then)) return "";
|
|
const mins = Math.max(0, Math.round((Date.now() - then) / 60000));
|
|
if (mins < 1) return t("projectCard.timeJustNow");
|
|
if (mins < 60) return t("projectCard.timeM", { count: mins });
|
|
const hours = Math.round(mins / 60);
|
|
if (hours < 24) return t("projectCard.timeH", { count: hours });
|
|
const days = Math.round(hours / 24);
|
|
if (days < 30) return t("projectCard.timeD", { count: days });
|
|
const months = Math.round(days / 30);
|
|
if (months < 12) return t("projectCard.timeMo", { count: months });
|
|
return t("projectCard.timeY", { count: Math.round(months / 12) });
|
|
}
|
|
|
|
const updatedLabel = $derived(relativeTime(project.updated_at));
|
|
|
|
// ---- overflow menu (rename / delete), pattern from ProjectCard ----
|
|
let menuOpen = $state(false);
|
|
let menuEl = $state<HTMLDivElement | null>(null);
|
|
|
|
$effect(() => {
|
|
if (!menuOpen) return;
|
|
const onDocClick = (e: MouseEvent) => {
|
|
if (menuEl && !menuEl.contains(e.target as Node)) menuOpen = false;
|
|
};
|
|
const onKey = (e: KeyboardEvent) => {
|
|
if (e.key === "Escape") menuOpen = false;
|
|
};
|
|
window.addEventListener("click", onDocClick);
|
|
window.addEventListener("keydown", onKey);
|
|
return () => {
|
|
window.removeEventListener("click", onDocClick);
|
|
window.removeEventListener("keydown", onKey);
|
|
};
|
|
});
|
|
|
|
let renameOpen = $state(false);
|
|
let renameValue = $state("");
|
|
|
|
function openRename() {
|
|
menuOpen = false;
|
|
renameValue = project.title;
|
|
renameOpen = true;
|
|
}
|
|
|
|
const renameMutation = createMutation({
|
|
mutationFn: (title: string) => projects.update(project.id, { title }),
|
|
onSuccess: () => {
|
|
client.invalidateQueries({ queryKey: ["projects"] });
|
|
toast.success(t("projectCard.renamedTo", { title: renameValue }));
|
|
},
|
|
onError: (err) => {
|
|
toast.error(
|
|
err instanceof Error ? err.message : t("projectCard.renameFailed"),
|
|
);
|
|
},
|
|
});
|
|
|
|
function submitRename() {
|
|
const title = renameValue.trim();
|
|
renameOpen = false;
|
|
if (!title || title === project.title) return;
|
|
$renameMutation.mutate(title);
|
|
}
|
|
|
|
let deleteOpen = $state(false);
|
|
|
|
const deleteMutation = createMutation({
|
|
mutationFn: () => projects.delete(project.id),
|
|
onSuccess: () => {
|
|
client.invalidateQueries({ queryKey: ["projects"] });
|
|
toast.success(t("projectCard.deleted", { title: project.title }));
|
|
},
|
|
onError: (err) => {
|
|
toast.error(
|
|
err instanceof Error ? err.message : t("projectCard.deleteFailed"),
|
|
);
|
|
},
|
|
});
|
|
</script>
|
|
|
|
<article class="a-card">
|
|
<button
|
|
type="button"
|
|
class="card-main"
|
|
aria-label={t("projectCard.open", { title: project.title })}
|
|
onclick={() => goto(resolve(`/workbench/${project.id}`))}
|
|
>
|
|
<span class="cover">
|
|
{#if coverUrl && !coverFailed}
|
|
<img
|
|
class="cover-img"
|
|
src={coverUrl}
|
|
alt=""
|
|
loading="lazy"
|
|
onerror={() => (coverFailed = true)}
|
|
/>
|
|
{:else}
|
|
<span
|
|
class="cover-ph"
|
|
style:background={placeholder}
|
|
style:color={placeholderIconColor}
|
|
aria-hidden="true"
|
|
>
|
|
<span class="cover-type"><small>暂无封面</small></span>
|
|
</span>
|
|
{/if}
|
|
{#if stats.active_jobs}<span class="active-count"
|
|
>{stats.active_jobs} 个任务进行中</span
|
|
>{/if}
|
|
<span class="status-tag {cover.cls}">{cover.label}</span>
|
|
</span>
|
|
<span class="body">
|
|
<span class="title-row">
|
|
<span class="title">{project.title}</span>
|
|
<span class="arrow" aria-hidden="true">→</span>
|
|
</span>
|
|
<span class="stage-bar" aria-label={t("home.stageProgress")}>
|
|
{#each STAGE_SEGMENTS as key (key)}
|
|
<span class="seg {segClass(key)}" title={t(`workbench.stage.${key}`)}
|
|
></span>
|
|
{/each}
|
|
</span>
|
|
<span class="meta">
|
|
<span>场景 {stats.scene_count} · 角色 {stats.character_count}</span>
|
|
<span class="meta-time">{updatedLabel}</span>
|
|
</span>
|
|
</span>
|
|
</button>
|
|
|
|
<div class="menu-wrap" bind:this={menuEl}>
|
|
<button
|
|
type="button"
|
|
class="menu-btn"
|
|
aria-label={t("projectCard.actions", { title: project.title })}
|
|
aria-haspopup="menu"
|
|
aria-expanded={menuOpen}
|
|
onclick={() => (menuOpen = !menuOpen)}
|
|
>
|
|
⋯
|
|
</button>
|
|
{#if menuOpen}
|
|
<div class="menu" role="menu">
|
|
<button
|
|
type="button"
|
|
role="menuitem"
|
|
class="menu-item"
|
|
onclick={openRename}
|
|
>
|
|
<Icon name="edit" size={14} />
|
|
{t("common.rename")}
|
|
</button>
|
|
<button
|
|
type="button"
|
|
role="menuitem"
|
|
class="menu-item danger"
|
|
onclick={() => {
|
|
menuOpen = false;
|
|
deleteOpen = true;
|
|
}}
|
|
>
|
|
<Icon name="trash" size={14} />
|
|
{t("common.delete")}
|
|
</button>
|
|
</div>
|
|
{/if}
|
|
</div>
|
|
</article>
|
|
|
|
<Modal bind:open={renameOpen} title={t("projectCard.renameTitle")}>
|
|
<form
|
|
onsubmit={(e) => {
|
|
e.preventDefault();
|
|
submitRename();
|
|
}}
|
|
>
|
|
<label class="field">
|
|
<span class="field-label">{t("common.title")}</span>
|
|
<input class="field-input" bind:value={renameValue} required />
|
|
</label>
|
|
</form>
|
|
{#snippet footer()}
|
|
<Button variant="ghost" onclick={() => (renameOpen = false)}
|
|
>{t("common.cancel")}</Button
|
|
>
|
|
<Button
|
|
variant="primary"
|
|
disabled={!renameValue.trim() || $renameMutation.isPending}
|
|
onclick={submitRename}
|
|
>
|
|
{t("common.rename")}
|
|
</Button>
|
|
{/snippet}
|
|
</Modal>
|
|
|
|
<ConfirmDialog
|
|
bind:open={deleteOpen}
|
|
title={t("projectCard.deleteTitle")}
|
|
message={t("projectCard.deleteMessage", { title: project.title })}
|
|
confirmLabel={t("common.delete")}
|
|
danger
|
|
onconfirm={() => $deleteMutation.mutate()}
|
|
/>
|
|
|
|
<style>
|
|
.a-card,
|
|
.a-card :global(*) {
|
|
box-sizing: border-box;
|
|
}
|
|
.a-card {
|
|
position: relative;
|
|
background: var(--bg-2);
|
|
border: 1px solid var(--line);
|
|
border-radius: var(--project-card-radius, var(--r-lg));
|
|
overflow: hidden;
|
|
transition:
|
|
transform var(--t-base) var(--ease-out),
|
|
border-color var(--t-base);
|
|
}
|
|
.a-card:hover {
|
|
transform: translateY(-1px);
|
|
border-color: var(--line-3);
|
|
}
|
|
.card-main {
|
|
display: flex;
|
|
flex-direction: column;
|
|
width: 100%;
|
|
padding: 0;
|
|
border: 0;
|
|
background: transparent;
|
|
color: inherit;
|
|
font: inherit;
|
|
text-align: left;
|
|
cursor: pointer;
|
|
}
|
|
.cover {
|
|
position: relative;
|
|
display: block;
|
|
aspect-ratio: 16/9;
|
|
width: 100%;
|
|
background: var(--bg-3);
|
|
overflow: hidden;
|
|
}
|
|
.cover-img {
|
|
position: absolute;
|
|
inset: 0;
|
|
width: 100%;
|
|
height: 100%;
|
|
object-fit: cover;
|
|
}
|
|
.cover-ph {
|
|
position: absolute;
|
|
inset: 0;
|
|
display: grid;
|
|
place-items: center;
|
|
}
|
|
.cover-type {
|
|
font-size: 32px;
|
|
text-align: center;
|
|
}
|
|
.cover-type small {
|
|
display: block;
|
|
font: 9px var(--mono);
|
|
}
|
|
.status-tag {
|
|
position: absolute;
|
|
left: 8px;
|
|
top: 8px;
|
|
padding: 0 6px;
|
|
height: 18px;
|
|
border-radius: 4px;
|
|
font-size: 11px;
|
|
background: var(--media-scrim);
|
|
color: var(--media-text);
|
|
}
|
|
.active-count {
|
|
position: absolute;
|
|
right: 6px;
|
|
bottom: 6px;
|
|
font-size: 10px;
|
|
padding: 2px 5px;
|
|
border-radius: 4px;
|
|
background: var(--media-scrim);
|
|
color: var(--media-text);
|
|
}
|
|
.body {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 6px;
|
|
padding: 10px 12px 12px;
|
|
width: 100%;
|
|
}
|
|
.title-row {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
}
|
|
.title {
|
|
font-size: 13px;
|
|
font-weight: 600;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
display: -webkit-box;
|
|
-webkit-line-clamp: 2;
|
|
line-clamp: 2;
|
|
-webkit-box-orient: vertical;
|
|
min-height: 35px;
|
|
line-height: 1.35;
|
|
}
|
|
.arrow {
|
|
margin-left: auto;
|
|
color: var(--tx-3);
|
|
}
|
|
.stage-bar {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 3px;
|
|
}
|
|
.seg {
|
|
flex: 1;
|
|
height: 3px;
|
|
border-radius: 2px;
|
|
background: rgb(var(--ink) / 0.1);
|
|
}
|
|
.seg-confirmed {
|
|
background: var(--gold);
|
|
}
|
|
.seg-draft {
|
|
background: var(--sc-agent);
|
|
}
|
|
.seg-running {
|
|
background: var(--sc-video);
|
|
}
|
|
.meta {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
gap: 8px;
|
|
flex-wrap: wrap;
|
|
color: var(--tx-3);
|
|
font-size: 11px;
|
|
}
|
|
.menu-wrap {
|
|
position: absolute;
|
|
top: 8px;
|
|
right: 8px;
|
|
}
|
|
.menu-btn {
|
|
border: 0;
|
|
background: var(--bg-2);
|
|
color: var(--tx-2);
|
|
border-radius: var(--r-sm);
|
|
width: 24px;
|
|
height: 24px;
|
|
cursor: pointer;
|
|
}
|
|
.menu {
|
|
position: absolute;
|
|
top: 28px;
|
|
right: 0;
|
|
min-width: 132px;
|
|
background: var(--bg-2);
|
|
border: 1px solid var(--line-2);
|
|
border-radius: var(--r-sm);
|
|
padding: 4px;
|
|
display: flex;
|
|
flex-direction: column;
|
|
box-shadow: var(--sh-2);
|
|
z-index: 2;
|
|
}
|
|
.menu-item {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
width: 100%;
|
|
padding: 7px 9px;
|
|
border: 0;
|
|
border-radius: var(--r-sm);
|
|
background: transparent;
|
|
color: var(--tx-1);
|
|
font: inherit;
|
|
font-size: 12.5px;
|
|
cursor: pointer;
|
|
text-align: left;
|
|
}
|
|
.menu-item:hover {
|
|
background: var(--bg-3);
|
|
}
|
|
.menu-item.danger {
|
|
color: var(--err);
|
|
}
|
|
</style>
|