/**
* 展示组件:stamp、metric、series、table、chart、report、text、notice、field、status(组件目录 v1 §3.5)。
*
* 数都是服务端渲染好的字(render_metric / render_cell),这里不格式化、不合计、不算差值(叙述层无算术):表格只排序,
* 图表只把已有的列画出来。点数字打开出处;没有出处的数照实标「无证据」。
*/
import { useEffect, useMemo, useState } from 'react'
import { ArrowDown, ArrowUp, RefreshCw, ShieldCheck } from 'lucide-react'
import clsx from 'clsx'
import { api } from '../../api/client'
import { FileLink } from '../../components/FileInput'
import { Notice, Spinner, StatusPill, useToast } from '../../components/ui'
import { isFileRef } from '../../lib/schemaForm'
import { Markdown } from '../../run/Markdown'
import { EvidenceDoc } from '../../run/EvidenceDoc'
import { useEvidence } from '../../store/evidence'
import { formatDay } from '../../lib/format'
import { STAMP_TEXT, VIEW_TEXT } from '../../lib/termsApp'
import type { ViewEvidence } from '../../typesApp'
import { Stamp } from '../Stamp'
import { Plot, type PlotSeries } from '../Plot'
import { blockState, evidenceOf, sourceOf, str, useView, type NodeProps } from '../context'
import { useViewParams } from './input'
/** 这一块没有数据时说为什么:还没有可读的运行、正在生成、过期、出错。服务端给了原话就用原话 */
export function BlockState({ state, message }: { state: string; message?: string }) {
const text = message || (state === 'empty' ? VIEW_TEXT.empty : state === 'running' ? VIEW_TEXT.running
: state === 'stale' ? VIEW_TEXT.stale : state === 'withheld' ? VIEW_TEXT.withheld : VIEW_TEXT.error)
return (
{state === 'running' && }
{text}
)
}
/** 数字:有出处的是按钮(点开出处),没有的照实标「无证据」 */
function Num({ text, evidence, label, className }: {
text: string; evidence: ViewEvidence | null; label: string; className?: string
}) {
const { openEvidence } = useView()
if (!evidence) {
return (
{text}
{VIEW_TEXT.noEvidence}
)
}
return (
)
}
/**
* 印章。按需视图(来源的 kind 为 on_demand)旁边给「刷新」:按页面当前的参数发起一次(同一组参数命中缓存就不新运行),
* 预览里不给
*/
export function StampBlock({ node }: NodeProps) {
const { model, appId, preview, onReload } = useView()
const toast = useToast()
const params = useViewParams()
const [busy, setBusy] = useState(false)
const view = str(node.data?.view)
const src = sourceOf(model, node)
const stamp = node.data?.stamp ?? src?.stamp
const state = blockState(node)
const onDemand = src?.kind === 'on_demand'
const refresh = async () => {
setBusy(true)
try {
const res = await api.apps.refreshView(appId, view, params)
toast(res.cached ? VIEW_TEXT.cached : VIEW_TEXT.refreshed, 'ok')
onReload?.()
} catch (e) {
toast.error(e)
} finally {
setBusy(false)
}
}
return (
{stamp ?
: }
{onDemand && view && !preview && (
)}
)
}
export function MetricBlock({ node }: NodeProps) {
const data = node.data ?? {}
const label = str(node.props.label) || str(data.name) || node.id
const state = blockState(node)
const text = str(data.text)
// 单位只写一处。口径卡写了单位的,服务端交来的字已经带着它(render_metric:「18,218.54元」),data.unit 只是说明是哪个
// 单位,不再在下面另写一行;口径卡没写单位(data.unit 是空串)、绑定单元格(字不带单位,render_cell)的,写搭建者在
// 属性里填的单位
const unit = str(data.unit) ? '' : str(node.props.unit)
return (
{label}
{state ?
: data.state === 'masked' ? {text || VIEW_TEXT.masked}
: }
{unit &&
{unit}
}
)
}
/** x 轴上的一个点写成日期(同一天的几次写到分钟):趋势按封存运行的结束时间排 */
function pointLabel(asOf: string, sameDay: boolean): string {
if (!asOf) return '—'
if (!sameDay) return formatDay(asOf)
const d = new Date(asOf)
return Number.isNaN(d.getTime()) ? asOf : `${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`
}
/**
* 趋势:服务端按处置给好线和断点——dual 两条线(旧版本、新版本),incomparable 两段之间断开并写明前后不可比,
* recompute 只给新版本、写一句说明;试运行预览里只有一个点,服务端写在 note 里的说明照写。点一个点打开那一次运行里
* 这个指标的出处
*/
export function SeriesBlock({ node }: NodeProps) {
const { openEvidence } = useView()
const data = node.data ?? {}
const label = str(node.props.label) || node.id
const state = blockState(node)
const { categories, series, breaks, notes } = useMemo(() => {
const raw: Record[] = Array.isArray(data.series) ? data.series : []
const points = raw.map((l) => (Array.isArray(l.points) ? l.points : []) as Record[])
// 横轴是封存运行的结束时间,几条线的点合在一起按时间排
const keys = [...new Set(points.flat().map((p) => str(p.as_of) || str(p.run_id)))]
.sort((a, b) => (Date.parse(a) || 0) - (Date.parse(b) || 0))
const days = new Set(keys.map((k) => k.slice(0, 10)))
const sameDay = days.size === 1 && keys.length > 1
const lines: PlotSeries[] = raw.map((l, li) => {
const at = new Map(points[li].map((p) => [str(p.as_of) || str(p.run_id), p]))
return {
name: str(l.label) || label,
values: keys.map((k) => (typeof at.get(k)?.value === 'number' ? at.get(k)!.value : null)),
texts: keys.map((k) => str(at.get(k)?.text) || undefined),
evidence: keys.map((k) => evidenceOf(at.get(k)?.evidence)),
}
})
const brk: { index: number; label: string }[] = []
const extra: string[] = []
for (const b of Array.isArray(data.breaks) ? data.breaks : []) {
const after = points.flat().find((p) => str(p.run_id) === str(b?.after_run))
const index = after ? keys.indexOf(str(after.as_of) || str(after.run_id)) + 1 : -1
if (index > 0 && index < keys.length) brk.push({ index, label: VIEW_TEXT.incomparable })
if (str(b?.text)) extra.push(str(b.text))
}
// 服务端照实写的说明:试运行预览里只有这一次试运行一个点,写明没有趋势
if (str(data.note)) extra.push(str(data.note))
return { categories: keys.map((k) => pointLabel(k, sameDay)), series: lines, breaks: brk, notes: extra }
}, [data, label])
const unit = str(data.unit)
return (
{label}{unit && `(${unit})`}
{state ?
: categories.length
?
{
const ev = series[s].evidence?.[i]
if (ev) openEvidence(ev, `${label} · ${categories[i]}`, series[s].texts?.[i] ?? undefined)
}} />
: }
{notes.map((n, i) => {n}
)}
)
}
const asNumber = (v: unknown): number | null => {
if (typeof v === 'number' && Number.isFinite(v)) return v
if (typeof v === 'string' && /^[-+]?\d[\d,]*(\.\d+)?$/.test(v.trim())) return Number(v.replace(/,/g, ''))
return null
}
const cellText = (v: unknown) => (v == null ? '' : typeof v === 'object' ? JSON.stringify(v) : String(v))
const rowsOf = (data: Record): unknown[][] =>
(Array.isArray(data.rows) ? data.rows : []).map((r: unknown) => (Array.isArray(r) ? r : []))
/** 表格:只排序,不出合计行(叙述层无算术)。样式同运行成果里的查询结果表;「查看出处」打开整份查询快照 */
export function TableBlock({ node }: NodeProps) {
const { openEvidence } = useView()
const data = node.data ?? {}
const state = blockState(node)
const title = str(node.props.title)
const columns: string[] = Array.isArray(data.columns) ? data.columns.map(str) : []
const all = useMemo(() => rowsOf(data), [data])
const masked = new Set(Array.isArray(data.masked) ? data.masked.map(str) : [])
// 服务端已按 props.sort 排过;这里只管人点表头换的排序
const [sort, setSort] = useState<{ col: number; desc: boolean } | null>(null)
const rows = useMemo(() => {
if (!sort) return all
return [...all].sort((a, b) => {
const x = a[sort.col]
const y = b[sort.col]
const nx = asNumber(x)
const ny = asNumber(y)
const c = nx != null && ny != null ? nx - ny : cellText(x).localeCompare(cellText(y), 'zh-CN')
return sort.desc ? -c : c
})
}, [all, sort])
const total = typeof data.total_rows === 'number' ? data.total_rows : all.length
const evidence = evidenceOf(data.evidence)
const numeric = columns.map((_, j) => rows.some((r) => asNumber(r[j]) != null) && !masked.has(columns[j]))
return (
{title &&
{title}
}
{state || !columns.length ?
: (
<>
{columns.map((c, j) => {
const on = sort?.col === j
return (
|
|
)
})}
{rows.map((r, i) => (
{columns.map((c, j) => (
|
{masked.has(c) ? {cellText(r[j]) || VIEW_TEXT.masked}
: r[j] == null ? null : cellText(r[j])}
|
))}
))}
{rows.length < total ? VIEW_TEXT.rowsOf(rows.length, total) : VIEW_TEXT.rows(total)}
{data.truncated && rows.length >= total && {VIEW_TEXT.truncated}}
{evidence?.artifact && (
)}
>
)}
)
}
/** 图表:按表格的列画折线或柱状,不聚合。x 是一列,y 最多四列;遮罩的列不画 */
export function ChartBlock({ node }: NodeProps) {
const data = node.data ?? {}
const state = blockState(node)
const title = str(node.props.title) || node.id
const columns: string[] = Array.isArray(data.columns) ? data.columns.map(str) : []
const rows = useMemo(() => rowsOf(data), [data])
const masked = new Set(Array.isArray(data.masked) ? data.masked.map(str) : [])
const x = str(node.props.x) || columns[0]
const ys: string[] = (Array.isArray(node.props.y) ? node.props.y.map(str) : columns.slice(1, 2))
.filter((c: string) => columns.includes(c) && c !== x && !masked.has(c)).slice(0, 4)
const xi = columns.indexOf(x)
const shown = rows.slice(0, 60)
return (
{title}
{state ?
: xi >= 0 && ys.length && shown.length
? (
<>
cellText(r[xi]))}
series={ys.map((c) => {
const at = columns.indexOf(c)
return { name: c, values: shown.map((r) => asNumber(r[at])), texts: shown.map((r) => cellText(r[at])) }
})} />
{rows.length > shown.length && {VIEW_TEXT.rowsOf(shown.length, rows.length)}
}
>
)
: }
)
}
/** 报告:同运行成果里的报告,逐段可点开证据(EvidenceDoc)。文档按工件 id 取,取不到照实说 */
export function ReportBlock({ node }: NodeProps) {
const data = node.data ?? {}
const state = blockState(node)
const title = str(node.props.title)
const artifact = str(data.doc_artifact)
const runId = evidenceOf(data.evidence)?.run_id
return (
{title &&
{title}
}
{state ?
: artifact ?
:
}
)
}
export function ReportDoc({ artifact, runId, label }: { artifact: string; runId?: string; label: string }) {
const slot = useEvidence((s) => s.docs[artifact])
const loadDoc = useEvidence((s) => s.loadDoc)
useEffect(() => { void loadDoc(artifact) }, [artifact, loadDoc])
if (!slot || slot.status === 'loading') return
if (slot.status === 'error' || !slot.data) return
return
}
export function TextBlock({ node }: NodeProps) {
return
}
export function NoticeBlock({ node }: NodeProps) {
const tone = node.props.tone === 'error' ? 'err' : node.props.tone === 'warn' ? 'warn' : 'info'
return {str(node.props.text)}
}
/** 字段:运行输入、成果字段、运行信息。输入和逐字取自报告的成果能点开出处 */
export function FieldBlock({ node }: NodeProps) {
const data = node.data ?? {}
const label = str(node.props.label) || node.id
const state = blockState(node)
const text = str(data.text)
const evidence = evidenceOf(data.evidence)
return (
{label}
{state ?
: data.state === 'masked' ? {text || VIEW_TEXT.masked}
// 绑定的输入是提交的文件(normal-user-apps N9):写成可下载的链接
: isFileRef(data.value) ?
: !text ? —
: evidence && evidence.kind !== 'report' ?
: evidence ?
: {text}}
)
}
/** 逐字取自报告的成果字段:文字照常写,旁边一个「查看出处」打开那份报告 */
function ReportText({ text, evidence, label }: { text: string; evidence: ViewEvidence; label: string }) {
const { openEvidence } = useView()
return (
{text}
)
}
export function StatusBlock({ node }: NodeProps) {
const data = node.data ?? {}
const label = str(node.props.label)
const state = blockState(node)
const status = str(data.value ?? data.status)
return (
{label && {label}}
{state ? : status ? : —}
)
}