StepsTimeline.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // 七步漏斗链路进度时间线组件
  2. import { STAGE_LIST } from '../types'
  3. import type { ProgressEvent, StageKey, StepStatus } from '../types'
  4. interface StepsTimelineProps {
  5. /** 已收到的进度事件,按 stage 聚合保留最新一条 */
  6. eventsByStage: Record<StageKey, ProgressEvent | undefined>
  7. }
  8. /** 状态文案映射 */
  9. const STATUS_LABEL: Record<StepStatus, string> = {
  10. pending: '待执行',
  11. running: '运行中',
  12. done: '完成',
  13. failed: '失败',
  14. degraded: '降级',
  15. cached: '复用缓存',
  16. }
  17. /** 步骤序号图标:未开始显示数字,已完成显示对勾,失败显示叉,降级显示感叹号,缓存显示勾号 */
  18. function StepIcon({ status, index }: { status: StepStatus; index: number }) {
  19. if (status === 'done') return <span aria-hidden>✓</span>
  20. if (status === 'failed') return <span aria-hidden>✕</span>
  21. if (status === 'degraded') return <span aria-hidden>!</span>
  22. if (status === 'cached') return <span aria-hidden>↻</span>
  23. return <span aria-hidden>{index}</span>
  24. }
  25. /** 根据事件状态推导步骤展示状态 */
  26. function deriveStatus(event: ProgressEvent | undefined): StepStatus {
  27. if (!event) return 'pending'
  28. if (event.status === 'running') return 'running'
  29. if (event.status === 'done') return 'done'
  30. if (event.status === 'failed') return 'failed'
  31. if (event.status === 'degraded') return 'degraded'
  32. if (event.status === 'cached') return 'cached'
  33. return 'pending'
  34. }
  35. export default function StepsTimeline({ eventsByStage }: StepsTimelineProps) {
  36. return (
  37. <div className="timeline">
  38. {STAGE_LIST.map((stage, idx) => {
  39. const event = eventsByStage[stage.key]
  40. const status = deriveStatus(event)
  41. const isActive = status === 'running'
  42. return (
  43. <div
  44. key={stage.key}
  45. className={`timeline-step${isActive ? ' active' : ''}`}
  46. >
  47. <div className="step-marker">
  48. <div className={`step-icon ${status}`} aria-label={`步骤 ${idx + 1} 状态: ${STATUS_LABEL[status]}`}>
  49. <StepIcon status={status} index={idx + 1} />
  50. </div>
  51. <div className="step-line" />
  52. </div>
  53. <div className="step-body">
  54. <div className="step-header">
  55. <span className="step-name">{stage.label}</span>
  56. <span className={`step-status-tag ${status}`}>{STATUS_LABEL[status]}</span>
  57. </div>
  58. <p className="step-desc">{stage.description}</p>
  59. {event?.message ? (
  60. <div className={`step-message ${status}`}>{event.message}</div>
  61. ) : null}
  62. </div>
  63. </div>
  64. )
  65. })}
  66. </div>
  67. )
  68. }