nex_animation/frontend/src/components/ui/Info.tsx

146 lines
7.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 只读回显(阶段 6 自 `Panel.tsx` 拆出,实现原样搬移):
* 「这条记录长什么样」的原子件——值、等宽小字、条件告警、外链、回显表、带语义的数字。
*
* 口径:属性回显一律走 Info(antd Descriptions),不许再写「灰盒 + 标签在上」的假输入框;
* 版面不放成段解释文字(§7 G2),第二层说明挂在 title 上悬浮给读者。
*/
import { type ReactNode } from 'react'
import { Descriptions, Flex, Typography } from 'antd'
import { cn } from '../../lib/cn'
import { IconExternal } from '../icons'
/** 数据回显(.val)与条件告警(.linehint):规范 §7 G2 —— 版面不放成段解释文字 */
export function Value({ children, muted, className, title }: {
children: ReactNode; muted?: boolean; className?: string
/** 悬浮说明:口径 / 数据来源这种「不该占版面但读者会问」的第二层,走原生 title(G2) */
title?: string
}) {
return (
<Typography.Text title={title}
className={cn('!text-xs leading-relaxed wrap-anywhere', muted ? '!text-text-quaternary' : '!text-text-tertiary', className)}
type={muted ? undefined : 'secondary'}>{children}</Typography.Text>
)
}
export function LineHint({ children, className }: { children: ReactNode; className?: string }) {
return (
<Flex align="baseline" gap={6} wrap className={cn('mt-2 text-xs leading-normal text-warning', className)}>
{children}
</Flex>
)
}
/** 等宽小字(路径 / id / 枚举码) */
export function Mono({ children, className, title }: { children: ReactNode; className?: string; title?: string }) {
return <Typography.Text className={cn('font-mono !text-xs', className)} title={title} copyable={false}>{children}</Typography.Text>
}
/** 文字链(.link):导航用 <Link>,这里只给「打开外部资源 / 下载」 */
export function ExtLink({ href, children, className, download }: {
href: string; children: ReactNode; className?: string; download?: boolean | string
}) {
return (
<Typography.Link href={href} download={download} target="_blank" rel="noreferrer"
className={cn('!inline-flex !items-center gap-1 !text-xs', className)}>
{children}<IconExternal className="text-xs" />
</Typography.Link>
)
}
/**
* 只读回显(.nex-info)= antd Descriptions:**站内所有「这条记录长什么样」的
* 属性回显只有这一种写法**(规范 R16.3)。
*
* 症状(用户截图指的那类块,原话「这种设计都是有问题的」):回显写成
* `Block tone=default vertical` 的灰盒——label 在上、值在下、四周描边,
* 和它旁边真正的输入框长得一模一样;用户去找可点的框,框里却没有可改的东西。
* 同一屏里另一批回显写成「label:值」的裸文本行,两种读法各占一半。
* 做法:一律 Descriptions(bordered + size=small + colon=false):
* label 列由 antd 上 colorFillAlter 底纹(底纹来自语义色,不新增色值),
* 值列左对齐长值折行,多属性并排走 column 分栏——一屏放得下的绝不多占一行。
* 为什么不用灰盒:灰盒在 antd 语言里是「输入区的底色」,回显用它是借错了语义;
* Descriptions 就是 antd 为「详情回显」准备的组件,明暗两档、圆角、分隔线
* 全都跟主题走,不需要再养一套私有皮肤(度量集中在 app.css `.nex-info`)。
*/
export type InfoItem = {
key?: string
label: ReactNode
value: ReactNode
/** 等宽回显(路径 / id / 枚举码 / 密钥),走 kit 的 Mono */
mono?: boolean
/** 跨几列(与 antd Descriptions 的 span 同名同义):长地址、一段说明用 span 顶满 */
span?: number
}
/** 分栏数:固定几列,或按断点降列(窄屏/抽屉里 3 列会挤成竖排,交给 antd 自己降) */
export type InfoColumns = number | Partial<Record<'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl', number>>
const INFO_BP: ('xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl')[] = ['xs', 'sm', 'md', 'lg', 'xl', 'xxl']
/**
* 档位向上补齐:写到的那一档=它右边所有档的值。
*
* 症状:antd Descriptions 内部把 column 与内置 {xs:1,sm:2,md:3,lg:3,xl:3,xxl:3} 合并,
* 再由宽到窄取第一个命中的档位。于是只写 `{ xs: 2, sm: 3, lg: 5 }` 的回显,
* 1440 命中的是内置的 xl=3——**声明的 5 列在桌面上永远不生效**;
* 卡片里的 `{ xs: 1, sm: 2 }` 同理,宽屏变成没声明过的 3 列,挤成竖排。
* 做法:把没写的右档填成最近一次声明的值,页面只需要写「从哪一档开始降到几列」。
* 为什么在容器补:12 个页面写的都是「窄屏少列、宽屏多列」这一个意图,
* 让每个页面记住 antd 的默认表并要求它写满六档,是规范在给自己找麻烦。
*/
export function fillInfoColumns(column: InfoColumns): InfoColumns {
if (typeof column === 'number') return column
const out: Partial<Record<'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl', number>> = { ...column }
let last: number | undefined
INFO_BP.forEach((bp) => {
if (out[bp] !== undefined) last = out[bp]
else if (last !== undefined) out[bp] = last
})
return out
}
export function Info({ items, column = 1, className, title, extra }: {
items: (InfoItem | null | undefined | false)[]
column?: InfoColumns
className?: string
/** 这一组回显的标题(antd Descriptions 的 title,走主题字号,不用页面自己写 h4) */
title?: ReactNode
/** 标题行右端动作(antd 5.29 Descriptions.extra):与标题同一行,不再另起一排 */
extra?: ReactNode
}) {
const real = items.filter(Boolean) as InfoItem[]
if (!real.length) return null
return (
<Descriptions className={cn('nex-info', className)} size="small" bordered colon={false}
column={fillInfoColumns(column)} title={title} extra={extra}
items={real.map((it, i) => ({
key: it.key ?? String(i), label: it.label, span: it.span,
children: it.mono ? <Mono className="wrap-anywhere">{it.value}</Mono> : it.value
}))} />
)
}
/**
* Info 里的计数值(.nex-info 的 number 档):等宽数字 + 可选单位。
*
* 症状:回显表里的 0 和 3 一样粗一样亮,一屏八个数字读不出哪个要管;
* 这套「非零才上语义色」的口径原先写在调度中心页面里(局部函数 cnt),
* 质检页要做同一个块只能再抄一遍——规范 §13.1 要求口径只有一份。
* 做法:0 一律降到 colorTextTertiary(「没有要处理的」),非零才按 tone 上
* success / warning / error;单位(条 / 次)比数字小一档、跟着降灰。
* 为什么是组件不是常量函数:它出现在 12 个页面的 items 里,JSX 写法与 Info 一致。
*/
const COUNT_TONE: Record<'ok' | 'warn' | 'bad', string> = { ok: 'text-success', warn: 'text-warning', bad: 'text-error' }
export function Count({ n, unit, tone }: { n?: number; unit?: string; tone?: 'ok' | 'warn' | 'bad' }) {
const num = Number(n) || 0
return (
<span className={`font-mono text-xs font-semibold ${num ? (tone ? COUNT_TONE[tone] : '') : 'text-text-tertiary'}`}>
{num}
{unit ? <span className="ml-0.5 text-xs font-normal text-text-tertiary">{unit}</span> : null}
</span>
)
}