165 lines
5.9 KiB
React
165 lines
5.9 KiB
React
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
import React from 'react';
|
|
import useCountUp from '../../../hooks/common/useCountUp';
|
|
|
|
/**
|
|
* 企业中心统一统计卡(KPI 卡,方案 A+ 底盘层)。
|
|
* 实底、紧凑、tabular-nums 等宽数字;图标品牌色浅底(不再渐变实底);
|
|
* 数值可选 count-up 滚动(签名层动效,respect prefers-reduced-motion)。
|
|
*
|
|
* @param {React.ComponentType} icon lucide 图标组件
|
|
* @param {React.ReactNode} label 标题(兼容旧用法的 title)
|
|
* @param {React.ReactNode} title label 的别名
|
|
* @param {React.ReactNode} value 主数值(直接展示)
|
|
* @param {number} [countTo] 数值 count-up 目标;提供时优先于 value 渲染
|
|
* @param {(n:number)=>React.ReactNode} [formatter] countTo 的格式化函数(默认 toLocaleString)
|
|
* @param {number} [decimals] countTo 保留小数位,默认 0
|
|
* @param {React.ReactNode} [hint] 副说明(兼容旧用法的 subtitle)
|
|
* @param {React.ReactNode} [subtitle] hint 的别名
|
|
* @param {React.ReactNode} [extra] 底部扩展槽(进度条等)
|
|
* @param {string} [tone] 'brand' | 'amber' | 'rose' | 'sky' | 'violet'
|
|
* @param {boolean} [danger] 危险态(红色强调),等价于 tone='rose'
|
|
* @param {boolean} [loading] 骨架加载态
|
|
* @param {boolean} [compact] 紧凑横向布局(用于密集 KPI 行)
|
|
* @param {string} [className] 追加类名
|
|
*/
|
|
|
|
// tone -> 图标浅底色/数值强调色。底盘层原则:色彩只用于图标与语义强调,不再渐变铺装饰。
|
|
const TONES = {
|
|
brand: {
|
|
icon: 'bg-brand/10 text-brand',
|
|
value: 'text-[var(--text-primary)]',
|
|
},
|
|
amber: {
|
|
icon: 'bg-amber-500/10 text-amber-600 dark:text-amber-400',
|
|
value: 'text-[var(--text-primary)]',
|
|
},
|
|
rose: {
|
|
icon: 'bg-rose-500/10 text-rose-600 dark:text-rose-400',
|
|
value: 'text-rose-600 dark:text-rose-400',
|
|
},
|
|
sky: {
|
|
icon: 'bg-sky-500/10 text-sky-600 dark:text-sky-400',
|
|
value: 'text-[var(--text-primary)]',
|
|
},
|
|
violet: {
|
|
icon: 'bg-violet-500/10 text-violet-600 dark:text-violet-400',
|
|
value: 'text-[var(--text-primary)]',
|
|
},
|
|
};
|
|
|
|
const OrgStatCard = ({
|
|
icon: Icon,
|
|
label,
|
|
title,
|
|
value,
|
|
countTo,
|
|
formatter,
|
|
decimals = 0,
|
|
hint,
|
|
subtitle,
|
|
extra,
|
|
tone = 'brand',
|
|
danger = false,
|
|
loading = false,
|
|
compact = false,
|
|
className = '',
|
|
}) => {
|
|
const resolvedTone = danger ? 'rose' : tone;
|
|
const palette = TONES[resolvedTone] || TONES.brand;
|
|
const heading = label ?? title;
|
|
const sub = hint ?? subtitle;
|
|
|
|
const hasCountUp = typeof countTo === 'number';
|
|
const animated = useCountUp(hasCountUp ? countTo : 0, { decimals });
|
|
const displayValue = hasCountUp
|
|
? formatter
|
|
? formatter(animated)
|
|
: animated.toLocaleString()
|
|
: value;
|
|
|
|
return (
|
|
<div
|
|
className={`org-stat-card rounded-xl border border-[var(--border-subtle)] bg-[var(--surface-card)] shadow-[var(--elevation-xs)] ${compact ? 'p-3' : 'p-4'} ${className}`}
|
|
>
|
|
<div className={`flex ${compact ? 'items-center gap-3' : 'flex-col'}`}>
|
|
<div
|
|
className={`org-stat-label flex items-center gap-2.5 ${compact ? '' : 'mb-2.5'}`}
|
|
>
|
|
{Icon && (
|
|
<span
|
|
className={`org-stat-icon inline-flex flex-shrink-0 items-center justify-center rounded-lg ${palette.icon} ${compact ? 'h-8 w-8' : 'h-9 w-9'}`}
|
|
>
|
|
<Icon size={compact ? 15 : 17} />
|
|
</span>
|
|
)}
|
|
{compact && (
|
|
<div className='flex min-w-0 flex-col'>
|
|
<span className='truncate text-xs font-medium text-gray-500 dark:text-gray-400'>
|
|
{heading}
|
|
</span>
|
|
{loading ? (
|
|
<span className='org-skeleton mt-1 h-4 w-16 rounded bg-gray-200 dark:bg-neutral-700' />
|
|
) : (
|
|
<span
|
|
className={`truncate text-sm font-semibold tabular-nums ${palette.value}`}
|
|
>
|
|
{displayValue}
|
|
</span>
|
|
)}
|
|
</div>
|
|
)}
|
|
{!compact && (
|
|
<span className='text-[13px] font-medium text-gray-500 dark:text-gray-400'>
|
|
{heading}
|
|
</span>
|
|
)}
|
|
</div>
|
|
|
|
{!compact &&
|
|
(loading ? (
|
|
<div className='space-y-2'>
|
|
<div className='org-skeleton h-7 w-24 rounded bg-gray-200 dark:bg-neutral-700' />
|
|
{(sub || extra) && (
|
|
<div className='org-skeleton h-4 w-28 rounded bg-gray-100 dark:bg-neutral-800' />
|
|
)}
|
|
</div>
|
|
) : (
|
|
<>
|
|
<div
|
|
className={`org-stat-value text-2xl font-semibold tracking-tight tabular-nums ${palette.value}`}
|
|
>
|
|
{displayValue}
|
|
</div>
|
|
{sub && (
|
|
<div className='org-stat-hint mt-1 text-xs text-gray-500 dark:text-gray-400'>
|
|
{sub}
|
|
</div>
|
|
)}
|
|
{extra && <div className='mt-3'>{extra}</div>}
|
|
</>
|
|
))}
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default OrgStatCard;
|