Files
vHost/frontend/src/components/Overview.tsx
T

67 lines
5.0 KiB
TypeScript

import {PageHeader} from './UI';
import { RefreshCw, Activity, ArrowRight, Cpu, Database, HardDrive, Network } from 'lucide-react';
import { formatBytes, usagePercent } from '../format';
import type { ActivityEntry, Plan, Project, Usage } from '../types';
import { MetricCard } from './MetricCard';
import { ProjectTable } from './ProjectTable';
const resourceLabels: Record<string, string> = { file: '文件管理', database: '数据库', project: '虚拟主机', domain: '域名', ftp: 'FTP', session:'会话', site:'网站', startup:'启动设置', website_settings:'网站设置', schedule:'计划任务', run_path:'运行目录', host_password:'主机密码', account_password:'账号密码', account_mfa:'双重认证', deployment:'部署', manual_certificate:'手动证书', automatic_certificate:'自动证书', certificate:'证书', dns_certificate:'DNS 证书', dns_credential:'DNS 凭据', domain_dns:'域名解析', oidc_identity:'第三方绑定', oidc_session:'第三方会话', project_network:'主机网络', panel_site:'站点接管', host_subscription:'主机套餐' };
interface OverviewProps {
onRefresh?:()=>void;refreshing?:boolean;
onEnterHost:(project:Project)=>void;
onSchedules?:()=>void;
projects: Project[];
plan: Plan;
usage: Usage;
activities: ActivityEntry[];
hostScoped: boolean;
onCreate: () => void;
canCreate: boolean;
onLogs?: () => void;
}
export function Overview({ onRefresh,refreshing,projects, plan, usage, activities, hostScoped, onCreate, onEnterHost, onSchedules, canCreate, onLogs }: OverviewProps) {
const resources = [
{ label: '存储空间', value: usage.storageBytes, limit: plan.storageBytes },
{ label: '本月流量', value: usage.bandwidthBytes, limit: plan.bandwidthBytes },
{ label: '进程数量', value: usage.processCount, limit: plan.processLimit, count: true },
{ label: '数据库大小', value: usage.databaseBytes, limit: plan.databaseBytes },
];
return (
<div className="page">
<PageHeader title="总览" actions={<div className="overview-actions">{onSchedules?<button className="secondary" onClick={onSchedules}>计划任务</button>:null}{onRefresh?<button className="secondary" disabled={refreshing} onClick={onRefresh}><RefreshCw size={15}/>{refreshing?'正在刷新…':'刷新状态'}</button>:null}</div>}/>
{!hostScoped ? <div className="metrics-grid">
<MetricCard icon={HardDrive} label="存储空间" value={formatBytes(usage.storageBytes)} limit={formatBytes(plan.storageBytes)} percent={usagePercent(usage.storageBytes, plan.storageBytes)} />
<MetricCard icon={Network} label="本月流量" value={formatBytes(usage.bandwidthBytes)} limit={formatBytes(plan.bandwidthBytes)} percent={usagePercent(usage.bandwidthBytes, plan.bandwidthBytes)} />
<MetricCard icon={Cpu} label="进程数量" value={String(usage.processCount)} limit={String(plan.processLimit)} percent={usagePercent(usage.processCount, plan.processLimit)} />
<MetricCard icon={Database} label="数据库大小" value={formatBytes(usage.databaseBytes)} limit={formatBytes(plan.databaseBytes)} percent={usagePercent(usage.databaseBytes, plan.databaseBytes)} />
</div> : null}
<ProjectTable projects={projects} canCreate={canCreate} onCreate={onCreate} onEnterHost={onEnterHost} />
{!hostScoped ? <div className="lower-grid">
<section className="activity-panel">
<div className="section-title"><h2>最近操作日志</h2>{onLogs?<button onClick={onLogs}>查看全部 <ArrowRight size={15} /></button>:null}</div>
<div className="activity-list">
{activities.map((entry) => <div className="activity-row" key={`${entry.createdAt}-${entry.action}-${entry.detail}`}><span>{formatTime(entry.createdAt)}</span><b>{resourceLabels[entry.resourceType] ?? entry.resourceType}</b><p>{entry.detail}</p><em className={entry.success ? '' : 'failed'}><i />{entry.success ? '成功' : '失败'}</em></div>)}
{activities.length === 0 ? <div className="activity-empty">暂无操作记录</div> : null}
</div>
</section>
<section className="usage-panel">
<div className="section-title"><h2>资源使用详情</h2><Activity size={18} /></div>
{resources.map((resource) => {
const percent = usagePercent(resource.value, resource.limit);
const value = resource.count ? `${resource.value} / ${resource.limit}` : `${formatBytes(resource.value)} / ${formatBytes(resource.limit)}`;
return <div className="usage-row" key={resource.label}><span>{resource.label}</span><b>{value}</b><div className="progress"><i style={{ width: `${percent}%` }} /></div><em>{percent}%</em></div>;
})}
</section>
</div> : null}
</div>
);
}
function formatTime(value: string) {
const date = new Date(value);
if (Number.isNaN(date.getTime())) return value;
return new Intl.DateTimeFormat('zh-CN', { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false }).format(date).replace('/', '-');
}