import React, { useMemo } from 'react'; import dayjs from '@/utils/dayjs'; import CopyButton from '@/components/CopyButton'; import type { UnitType } from './constants'; import { DATE_FORMAT } from './constants'; import { useLazyTranslation } from '@/utils/useLazyTranslation'; import { cn } from '@/lib/utils'; // shadcn 核心类名合并工具 interface ResultViewProps extends React.HTMLAttributes { result: string; mode: 'ts2dt' | 'dt2ts'; unit: UnitType; zone: string; /** 无结果时是否渲染占位(桌面端右栏使用),默认 false */ showEmptyPlaceholder?: boolean; } const ResultView = React.memo( ({ result, mode, unit, zone, showEmptyPlaceholder = false, className, ...props }: ResultViewProps) => { const { t } = useLazyTranslation('timestamp'); // 严谨计算时间衍生的附加时区/相对时间状态 const extraInfo = useMemo(() => { if (!result) return null; const d = mode === 'ts2dt' ? dayjs(result, DATE_FORMAT).tz(zone) : unit === 'ms' ? dayjs(Number(result)) : dayjs.unix(Number(result)); return { relative: d.fromNow(), iso: d.toISOString(), utc: d.utc().format(DATE_FORMAT) + ' UTC', }; }, [result, mode, zone, unit]); // 1. 空状态骨架面板:优雅匹配 shadcn 的中性灰色居中占位 if (!result) { if (!showEmptyPlaceholder) return null; return (
{t('timestamp:resultEmpty')}
); } return (
{/* 顶部小标签 */} {t('timestamp:resultLabel')} {/* 2. 核心结果大卡片: 对齐 shadcn 官方卡片风格,使用 bg-card、border-border 构筑多层级阴影。 核心数值直接拉粗为 text-foreground (在黑夜模式下会自动转为大气的纯白,完美避开刺眼强光) */}
{result}
{/* 3. 衍生的附加参考数据区: 背景改为低饱和度的 bg-muted/40 隔离带。 内部数值降级为 text-muted-foreground,建立教科书般的完美“视觉权重层级”。 */}
{[ { label: t('timestamp:relativeTime'), value: extraInfo?.relative }, { label: t('timestamp:iso8601'), value: extraInfo?.iso, isMono: true }, { label: t('timestamp:utcTime'), value: extraInfo?.utc, isMono: true }, ].map((item) => (
{item.label}
{item.value} {item.value && ( )}
))}
); }, ); ResultView.displayName = 'ResultView'; export default ResultView;