70 lines
2.9 KiB
TypeScript
70 lines
2.9 KiB
TypeScript
import React from 'react';
|
|
import { Kpi, Alert, Screen } from '../../types.ts';
|
|
import { UsersIcon, TrendingUpIcon, AlertTriangleIcon, ThermometerIcon } from '../icons/Icons.tsx';
|
|
import { MobileBottomNav, severityMobileConfig } from './MobileLayout.tsx';
|
|
import { formatNumber, formatTruncatedRatio } from '../../utils/formatters.ts';
|
|
|
|
const kpiIconMapping: { [key: string]: React.ReactNode } = {
|
|
'Populasi Total': <UsersIcon />,
|
|
'FCR (Real-time)': <TrendingUpIcon />,
|
|
'Mortalitas Hari Ini': <AlertTriangleIcon />,
|
|
'Suhu Rata-rata': <ThermometerIcon />,
|
|
};
|
|
|
|
const statusColors = { ok: 'text-green-600', warning: 'text-yellow-600', critical: 'text-red-600' };
|
|
|
|
interface HomeScreenProps {
|
|
kpis: Record<string, Kpi>;
|
|
alerts: Alert[];
|
|
onNavigate: (screen: Screen) => void;
|
|
onAlertClick: (alert: Alert) => void;
|
|
}
|
|
|
|
const HomeScreen: React.FC<HomeScreenProps> = ({ kpis, alerts, onNavigate, onAlertClick }) => (
|
|
<div className="bg-gray-50 h-full">
|
|
<div className="p-3 pt-8 h-full overflow-y-auto pb-16">
|
|
<h1 className="text-xl font-bold text-gray-800">Dashboard</h1>
|
|
<div className="grid grid-cols-2 gap-3 mt-3">
|
|
{/* FIX: Explicitly type 'kpi' as Kpi to resolve type errors. */}
|
|
{Object.values(kpis).map((kpi: Kpi) => (
|
|
// Preserve FCR/EEF precision on the home cards without rounding.
|
|
<div
|
|
key={kpi.title}
|
|
className={`p-3 rounded-lg shadow-sm ${kpi.status === 'critical' ? 'bg-red-100' : 'bg-white'}`}
|
|
>
|
|
<div className={`text-2xl ${statusColors[kpi.status]}`}>
|
|
{kpiIconMapping[kpi.title]}
|
|
</div>
|
|
<p className="text-xs font-semibold text-gray-600 mt-2">{kpi.title}</p>
|
|
<p className={`text-lg font-bold ${statusColors[kpi.status]}`}>
|
|
{kpi.unit === '' && (kpi.title.includes('FCR') || kpi.title.includes('EEF'))
|
|
? formatTruncatedRatio(kpi.value, 3)
|
|
: formatNumber(kpi.value, { maxDecimals: 2 })}{' '}
|
|
<span className="text-sm font-normal">{kpi.unit}</span>
|
|
</p>
|
|
</div>
|
|
))}
|
|
</div>
|
|
<h2 className="text-lg font-bold text-gray-800 mt-4">Peringatan Terbaru</h2>
|
|
<div className="space-y-2 mt-2">
|
|
{alerts.slice(0, 3).map((alert) => (
|
|
<button
|
|
key={alert.id}
|
|
onClick={() => onAlertClick(alert)}
|
|
className={`w-full p-2 rounded-lg flex items-center space-x-3 border-l-4 ${severityMobileConfig[alert.severity].border} bg-white shadow-sm text-left`}
|
|
>
|
|
<div className="text-xl">{severityMobileConfig[alert.severity].icon}</div>
|
|
<div>
|
|
<p className="text-xs font-semibold">{alert.message}</p>
|
|
<p className="text-xs text-gray-500">{alert.timestamp}</p>
|
|
</div>
|
|
</button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
<MobileBottomNav active="home" onNavigate={onNavigate} />
|
|
</div>
|
|
);
|
|
|
|
export default HomeScreen;
|