108 lines
4.8 KiB
TypeScript
108 lines
4.8 KiB
TypeScript
import { type ReactNode, type Ref } from 'react'
|
||
import { cn } from '../../lib/cn'
|
||
|
||
/**
|
||
* 流程节点条里的「一格」(全站唯一一份:WorkflowBar 与项目列表、概览共用)。
|
||
*
|
||
* 节点状态口径来自 lib/pipeline.ts:done / active / failed / pending。
|
||
* 「当前节点」只有 openNode() 一个算法——以前五个地方各算各的,草稿项目会喊
|
||
* 「当前 · 导出完整视频」,流程全绿之后还在喊最后一个节点名(明明已经交付)。
|
||
*/
|
||
export const STATE_LABEL: Record<string, string> = {
|
||
done: '完成', failed: '需处理', active: '进行中', pending: '待处理'
|
||
}
|
||
|
||
export function stepIcon(state: string, index: number): string {
|
||
if (state === 'done') return '✓'
|
||
if (state === 'failed') return '!'
|
||
if (state === 'active') return '▶'
|
||
return String(index + 1)
|
||
}
|
||
|
||
/** 后端没有计数的节点显示中文状态;有计数的节点显示 done/total 并夹住分母 */
|
||
export function nodeProgress(node: any): string {
|
||
if (node?.done === null || node?.done === undefined) return STATE_LABEL[node?.state] || String(node?.state ?? '')
|
||
return `${Math.min(node.done, node.total)}/${node.total}`
|
||
}
|
||
|
||
export function nodeHint(node: any): string {
|
||
return [`${node.label}(${STATE_LABEL[node.state] || node.state})`, node.detail].filter(Boolean).join(' — ')
|
||
}
|
||
|
||
const NUM: Record<string, string> = {
|
||
done: 'bg-accent-fill text-on-accent',
|
||
active: 'bg-accent-fill text-on-accent',
|
||
failed: 'bg-danger text-danger-ink',
|
||
pending: 'bg-active text-muted'
|
||
}
|
||
|
||
export function Step({ node, index, current, compact, onClick, className }: {
|
||
node: any
|
||
index: number
|
||
current?: boolean
|
||
compact?: boolean
|
||
onClick?: (node: any) => void
|
||
className?: string
|
||
}) {
|
||
const st = node.state === 'done' ? 'done' : node.state === 'failed' ? 'failed' : node.state === 'active' ? 'active' : 'pending'
|
||
return (
|
||
<div role="listitem" title={nodeHint(node)} data-current={current ? '1' : undefined}
|
||
onClick={onClick ? () => onClick(node) : undefined}
|
||
className={cn('flex-1 basis-[118px] min-w-[96px] bg-panel border rounded-tile p-[9px_10px] relative min-w-0 transition-colors',
|
||
onClick && 'cursor-pointer',
|
||
current && 'shadow-[0_0_0_1px_var(--c-accent-line)]',
|
||
st === 'done' && 'border-success-line bg-raised',
|
||
st === 'active' && 'border-success-line bg-accent-tint shadow-[inset_0_0_0_1px_var(--c-success-line)]',
|
||
st === 'failed' && 'border-danger-line bg-panel',
|
||
st === 'pending' && 'border-[--c-bg-active] opacity-70', className)}>
|
||
<div className={cn('flex items-center gap-[6px] text-[10.5px]', st === 'done' ? 'text-success' : 'text-ink-3')}>
|
||
<span className={cn('grid place-items-center w-4 h-4 rounded-full text-tiny flex-none', NUM[st])}>{stepIcon(node.state, index)}</span>
|
||
<span className="min-w-0 truncate">{nodeProgress(node)}</span>
|
||
</div>
|
||
<div className={cn('text-body font-semibold mt-[6px] leading-[1.35] line-clamp-2',
|
||
st === 'active' ? 'text-accent-bright' : st === 'failed' ? 'text-danger-bright' : 'text-ink-2')}>
|
||
{node.label}
|
||
</div>
|
||
{!compact && (
|
||
<div className="text-tiny text-faint mt-1 leading-[1.4] min-h-[14px] line-clamp-2 break-anywhere">{node.detail}</div>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
export function Steps({ nodes, currentKey, compact, onStep, className, id, scrollSmooth, ref }: {
|
||
nodes: any[]
|
||
currentKey?: string
|
||
compact?: boolean
|
||
onStep?: (node: any, at: number) => void
|
||
className?: string
|
||
id?: string
|
||
scrollSmooth?: boolean
|
||
/** 横向滚动容器:WorkflowBar 需要量它、并把当前节点滚进视野 */
|
||
ref?: Ref<HTMLDivElement>
|
||
}) {
|
||
const list = nodes || []
|
||
return (
|
||
<div ref={ref} id={id} role="list"
|
||
className={cn('flex gap-[6px] overflow-x-auto scrollbar-slim p-[4px_2px_10px]', scrollSmooth && 'scroll-smooth', className)}>
|
||
{(list.length ? list : [{ state: 'pending', label: '暂无流程数据' }]).map((n: any, at: number) => (
|
||
<Step key={n.key ?? at} node={n} index={at} compact={compact} current={!!currentKey && n.key === currentKey}
|
||
onClick={onStep && list.length ? (node) => onStep(node, at) : undefined} />
|
||
))}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** 流程条底部那一行「已完成 x / y · 当前:xxx」;全绿时不再报最后一个节点名 */
|
||
export function FlowCaption({ nodes, current, allDone, className }: {
|
||
nodes: any[]; current?: ReactNode; allDone?: boolean; className?: string
|
||
}) {
|
||
const done = (nodes || []).filter((s: any) => s.state === 'done').length
|
||
return (
|
||
<div className={cn('mt-[2px] text-tiny text-muted', className)}>
|
||
已完成 {done} / {nodes.length} 个节点
|
||
{current ? <> · 当前:{current}</> : allDone ? <> · 流程已全部完成</> : null}
|
||
</div>
|
||
)
|
||
}
|