154 lines
8.9 KiB
TypeScript
154 lines
8.9 KiB
TypeScript
/**
|
||
* 顶栏「流程进度控制台」:项目名 + 状态 / 12~13 段节点轨道 + 流程进度 + 当前节点。
|
||
*
|
||
* 数据源不额外发请求:ProjectDetail 每 6 秒把项目详情写进 useProjectStore,
|
||
* 这里只读。但 pctx.id 在请求发出前就被赋值,项目 404(或已删除)时会残留
|
||
* 上一个项目的数据,所以这里认「已经加载成功的那份项目数据自己的 id」,不认路由号。
|
||
*
|
||
* 布局约定:品牌块与工具块各 flex:1 1 0,控制台夹在正中 = 页面正中。
|
||
* 不要改成 margin-inline:auto:那居中的是「品牌右缘↔工具左缘」空档的中点,
|
||
* 工具区比品牌区宽约 140px,整块会偏左 70px。
|
||
*/
|
||
import { NavLink } from 'react-router-dom'
|
||
import { cn } from '../../lib/cn'
|
||
import { statusLabel } from '../../lib/format'
|
||
import { openNode, stageNote } from '../../lib/pipeline'
|
||
import { useProjectStore } from '../../stores/project'
|
||
|
||
/** 12 节点流程的分组,和后端 PIPELINE_STAGES 对齐;驳回时多出来的 revise 归入审核组 */
|
||
const PIPE_GROUP: Record<string, string> = {
|
||
create_project: 'prepare', import_storyboard: 'prepare', bind_assets: 'prepare',
|
||
plan_tasks: 'plan', preflight: 'plan',
|
||
dispatch: 'generate', generate: 'generate', save_output: 'generate',
|
||
quality_check: 'review', review: 'review', revise: 'review',
|
||
compose: 'deliver', export: 'deliver'
|
||
}
|
||
const PIPE_STATE: Record<string, string> = {
|
||
done: '已完成', active: '进行中', failed: '需处理', pending: '待处理'
|
||
}
|
||
/** 「正在干活」的项目状态:节点可能还是 pending,但后端其实在合成 / 生成 */
|
||
const IN_FLIGHT = ['generating', 'composing']
|
||
/** 详情还没回来(或项目已删除)时,轨道仍按 12 段虚位显示,不塌成一根长条 */
|
||
const PIPE_GHOST = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map((i) => ({
|
||
key: 'ghost' + i, label: '', state: 'pending', detail: '', groupEnd: i === 2 || i === 4 || i === 7 || i === 9
|
||
}))
|
||
const PIPE_COLOR: Record<string, string> = {
|
||
active: 'var(--c-accent)', done: 'var(--c-success)', failed: 'var(--c-danger)',
|
||
pending: 'var(--c-text-faint)', idle: 'var(--c-text-faint)'
|
||
}
|
||
const PIPE_TINT: Record<string, string> = {
|
||
active: 'var(--c-accent-tint)', done: 'var(--c-success-tint)', failed: 'var(--c-danger-tint)',
|
||
pending: 'var(--c-bg-sunken)', idle: 'var(--c-bg-sunken)'
|
||
}
|
||
const NODE_STATE: Record<string, string> = {
|
||
done: 'bg-success opacity-100',
|
||
active: 'bg-accent opacity-100 h-[13px] rounded-[3px] animate-pulse-soft',
|
||
failed: 'bg-danger opacity-100 h-[13px] rounded-[3px] animate-pulse-soft',
|
||
pending: 'bg-faint opacity-34'
|
||
}
|
||
|
||
export function PipelineConsole({ projectId }: { projectId: string }) {
|
||
const project = useProjectStore((s) => s.project)
|
||
const workflow = useProjectStore((s) => s.workflow)
|
||
/* 认「加载成功的那份数据自己的 id」,不认路由号 */
|
||
const proj = projectId && project?.id === Number(projectId) ? project : null
|
||
if (!projectId) return null
|
||
|
||
const list = proj ? workflow || [] : []
|
||
/** 轨道节点:正常 12 个,被驳回时后端会插一个 revise 变 13,所以一律按实际长度算 */
|
||
const pipeNodes = list.map((n: any, i: number) => ({
|
||
key: n.key,
|
||
label: n.label || n.key,
|
||
state: n.state || 'pending',
|
||
detail: n.detail || '',
|
||
groupEnd: i < list.length - 1 && PIPE_GROUP[list[i + 1].key] !== PIPE_GROUP[n.key]
|
||
}))
|
||
const pipeTotal = pipeNodes.length
|
||
const pipeDone = pipeNodes.filter((n) => n.state === 'done').length
|
||
/**
|
||
* 控制台的百分比说的是「轨道走到哪了」,直接由节点完成数算出,
|
||
* 和旁边的轨道颜色一一对应。
|
||
* 后端的 stats.progress_ratio 是「镜头审核合格比例」,口径不同,
|
||
* 放在这里会出现「轨道 9 段绿、进度 0%」这种自相矛盾的画面。
|
||
*/
|
||
const pipePct = pipeTotal ? Math.round(pipeDone / pipeTotal * 100) + '%' : '—'
|
||
const pipeRail = pipeNodes.length ? pipeNodes : PIPE_GHOST
|
||
// 口径统一在 lib/pipeline.ts:需处理 → 进行中 → 第一个没做完的;
|
||
// 全部做完时不再有「当前节点」,不能再拿最后一个节点顶上来(那是一句反话)。
|
||
const pipeCurrent = pipeNodes.length
|
||
? openNode(pipeNodes) || { label: '全部 ' + pipeTotal + ' 个节点已完成', state: 'done', detail: '', key: 'all-done' }
|
||
: null
|
||
/**
|
||
* 整体色调跟着流程走:有节点需处理 → 红;全部完成 → 绿;机器正在干活 → accent。
|
||
* 「正在干活」还要看项目状态:例如 12 个节点全绿只差最后导出时,
|
||
* 后端把节点标成 pending、但项目其实是 composing,此时不该显示成灰色的空闲态。
|
||
*/
|
||
const pipeTone = pipeNodes.some((n) => n.state === 'failed') ? 'failed'
|
||
: !pipeNodes.length ? 'idle'
|
||
: pipeNodes.every((n) => n.state === 'done') ? 'done'
|
||
: pipeNodes.some((n) => n.state === 'active') || IN_FLIGHT.includes(proj?.status) ? 'active'
|
||
: 'pending'
|
||
const allDone = pipeTotal > 0 && pipeDone === pipeTotal
|
||
/** 全部走完时不再叫「当前」,否则「当前 · 全部 12 个节点已完成」是句反话 */
|
||
const pipeCurText = pipeCurrent ? (allDone ? pipeCurrent.label : '当前 · ' + pipeCurrent.label) : ''
|
||
const projectTitle = proj?.name || '项目 #' + projectId
|
||
const color = PIPE_COLOR[pipeTone]
|
||
const muted = pipeTone === 'pending' || pipeTone === 'idle'
|
||
|
||
const summary = '《' + projectTitle + '》流程进度 ' + pipePct
|
||
+ ' · 已完成 ' + pipeDone + '/' + pipeTotal + ' 个节点'
|
||
+ (pipeCurrent ? (allDone ? ' · ' : ' · 当前节点:') + pipeCurrent.label : '')
|
||
|
||
return (
|
||
<NavLink to={`/projects/${projectId}/overview`} title={summary + '(点击查看流程)'}
|
||
className={cn('relative grid min-w-[132px] max-w-[620px] flex-[0_1_auto] gap-1 overflow-hidden rounded-block border border-line-bright bg-[linear-gradient(96deg,var(--pipe-tint),var(--c-bg-sunken)_46%)] py-[5px] pl-[15px] pr-[14px] text-ink no-underline transition-[border-color,box-shadow] duration-[180ms] hover:border-[var(--pipe-line)] hover:shadow-[0_0_0_1px_var(--pipe-line)] max-mid:max-w-[452px] max-nav:max-w-[420px] max-nav:pl-[12px] max-nav:pr-[11px] max-tiny:max-w-[258px] max-tiny:min-w-[118px] max-narrow:min-w-[132px] max-narrow:max-w-[196px] max-narrow:gap-[3px]',
|
||
muted ? '' : 'border-[var(--pipe-line)]')}
|
||
style={{ ['--pipe' as any]: color, ['--pipe-tint' as any]: PIPE_TINT[pipeTone], ['--pipe-line' as any]: color }}>
|
||
<span className="absolute inset-y-0 left-0 w-[3px] bg-[var(--pipe)]" aria-hidden="true"></span>
|
||
|
||
<span className="flex h-[17px] min-w-0 items-center gap-[7px]">
|
||
<b className="min-w-0 truncate text-[12.5px] font-bold leading-[17px] tracking-[.2px]">{projectTitle}</b>
|
||
{proj?.status && (
|
||
<em className={cn('flex-none rounded-pill border px-[7px] text-tiny font-bold not-italic',
|
||
muted ? 'border-line bg-hover text-ink-2' : 'border-[var(--pipe-line)] bg-[var(--pipe-tint)] text-[var(--pipe)] max-tiny:hidden')}>
|
||
{statusLabel(proj.status)}
|
||
</em>
|
||
)}
|
||
</span>
|
||
|
||
<span className="flex h-4 min-w-0 items-center gap-2">
|
||
{/* 轨道宽度写死:节点要能数得清。宁可整块按断点换档,也不让 12 段
|
||
被 flex 压到 min-width 之后溢出轨道、盖到右边的百分比上。 */}
|
||
<span className="flex h-2 w-[210px] flex-none items-center gap-[3px] max-mid:w-[176px] max-nav:w-[150px] max-tiny:w-[110px] max-tiny:gap-0.5 max-narrow:w-[78px]" aria-hidden="true">
|
||
{pipeRail.map((n) => (
|
||
<i key={n.key} title={n.label ? n.label + ':' + (n.detail || PIPE_STATE[n.state] || n.state) : undefined}
|
||
className={cn('h-2 min-w-[3px] flex-1 basis-0 rounded-[2px]', NODE_STATE[n.state] || NODE_STATE.pending,
|
||
n.groupEnd && 'mr-[7px] last:mr-0 max-tiny:mr-[3px]')} />
|
||
))}
|
||
</span>
|
||
<small className="flex-none text-tiny text-muted max-tiny:hidden">流程进度</small>
|
||
<b className={cn('flex-none text-ui font-extrabold leading-4 tracking-[.3px]', muted && 'text-ink-2')} style={{ color: muted ? undefined : color }}>{pipePct}</b>
|
||
{pipeCurrent && (
|
||
<small className={cn('min-w-0 flex-[0_1_auto] truncate text-right leading-4 text-[10.5px] text-faint max-mid:hidden', allDone && 'text-[var(--pipe)]')}>
|
||
{pipeCurText}
|
||
</small>
|
||
)}
|
||
</span>
|
||
</NavLink>
|
||
)
|
||
}
|
||
|
||
export default PipelineConsole
|
||
|
||
/**
|
||
* 侧栏项目名下那一行「当前节点」。
|
||
* 和顶栏控制台同一口径(lib/pipeline.ts),两处不能各算各的。
|
||
*/
|
||
export function useCurrentStageNote(projectId: string): string {
|
||
const project = useProjectStore((s) => s.project)
|
||
const workflow = useProjectStore((s) => s.workflow)
|
||
const proj = projectId && project?.id === Number(projectId) ? project : null
|
||
if (!proj) return ''
|
||
return stageNote(proj, workflow && workflow.length ? workflow : proj.workflow)
|
||
}
|