format.ts 1.42 KB
export function formatTimeLabel(value?: string | null): string {
  if (!value) {
    return '暂无记录'
  }

  const parsed = new Date(value)
  if (Number.isNaN(parsed.getTime())) {
    return value
  }

  return new Intl.DateTimeFormat('zh-CN', {
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
  }).format(parsed)
}

export function joinDisplay(values: string[] | undefined, fallback = '暂无'): string {
  if (!values || values.length === 0) {
    return fallback
  }
  return values.filter(Boolean).join('、')
}

export function statusTone(status: string): string {
  if (['running', 'researching', 'logging_in', 'checking', 'awaiting_scan', 'pending'].includes(status)) {
    return 'running'
  }
  if (['completed', 'reported', 'logged_in', 'ready'].includes(status)) {
    return 'success'
  }
  if (['error', 'cancelled', 'logged_out'].includes(status)) {
    return 'danger'
  }
  return 'neutral'
}

export function appLabel(app: string): string {
  const labels: Record<string, string> = {
    insight: 'Insight 洞察',
    media: 'Media 表达',
    query: 'Query 补证',
    forum: 'Forum 研判',
    report: 'Report 报告',
  }

  return labels[app] ?? app
}

export function buildEngineUrl(port?: number | null): string {
  if (!port) {
    return ''
  }

  const protocol = window.location.protocol
  const host = window.location.hostname || '127.0.0.1'
  return `${protocol}//${host}:${port}`
}