249 lines
11 KiB
TypeScript
249 lines
11 KiB
TypeScript
import React from 'react';
|
|
import type { HarvestPredictionResult } from '../../types/api.ts';
|
|
|
|
export interface HarvestPredictionWidgetProps {
|
|
prediction: HarvestPredictionResult | null;
|
|
isLoading?: boolean;
|
|
}
|
|
|
|
const STATUS_CONFIG: Record<
|
|
string,
|
|
{ label: string; badgeCls: string; borderCls: string; icon: string }
|
|
> = {
|
|
growing: {
|
|
label: 'Fase Pertumbuhan Aktif',
|
|
badgeCls: 'bg-blue-50 text-blue-700 border-blue-200',
|
|
borderCls: 'border-blue-100 bg-gradient-to-br from-blue-50/50 via-white to-white',
|
|
icon: 'fa-solid fa-seedling text-blue-500',
|
|
},
|
|
approaching_window: {
|
|
label: 'Mendekati Jendela Panen',
|
|
badgeCls: 'bg-cyan-50 text-cyan-700 border-cyan-200',
|
|
borderCls: 'border-cyan-100 bg-gradient-to-br from-cyan-50/50 via-white to-white',
|
|
icon: 'fa-solid fa-clock-rotate-left text-cyan-500',
|
|
},
|
|
optimal_window: {
|
|
label: 'Jendela Panen Terbaik (Puncak EEF)',
|
|
badgeCls: 'bg-emerald-50 text-emerald-700 border-emerald-200',
|
|
borderCls: 'border-emerald-200 bg-gradient-to-br from-emerald-50/60 via-white to-white',
|
|
icon: 'fa-solid fa-trophy text-emerald-600',
|
|
},
|
|
post_optimal: {
|
|
label: 'Melewati Titik Efisien (FCR Meningkat)',
|
|
badgeCls: 'bg-amber-50 text-amber-700 border-amber-200',
|
|
borderCls: 'border-amber-200 bg-gradient-to-br from-amber-50/50 via-white to-white',
|
|
icon: 'fa-solid fa-triangle-exclamation text-amber-500',
|
|
},
|
|
};
|
|
|
|
const DEFAULT_STATUS_META = {
|
|
label: 'Fase Pertumbuhan Aktif',
|
|
badgeCls: 'bg-blue-50 text-blue-700 border-blue-200',
|
|
borderCls: 'border-blue-100 bg-gradient-to-br from-blue-50/50 via-white to-white',
|
|
icon: 'fa-solid fa-seedling text-blue-500',
|
|
};
|
|
|
|
const WAITING_STATUS_META = {
|
|
label: 'Siap Generate AI Insight',
|
|
badgeCls: 'bg-blue-50 text-blue-700 border-blue-200',
|
|
borderCls: 'border-blue-100 bg-gradient-to-br from-blue-50/40 via-white to-white',
|
|
icon: 'fa-solid fa-wand-magic-sparkles text-blue-500',
|
|
};
|
|
|
|
export const HarvestPredictionWidget: React.FC<HarvestPredictionWidgetProps> = ({
|
|
prediction,
|
|
isLoading = false,
|
|
}) => {
|
|
const statusMeta = prediction?.efficiency_status
|
|
? (STATUS_CONFIG[prediction.efficiency_status] ?? DEFAULT_STATUS_META)
|
|
: WAITING_STATUS_META;
|
|
|
|
const recommendationLabel =
|
|
prediction?.recommendation_label ??
|
|
(prediction?.harvest_type === 'thinning'
|
|
? 'Panen Penjarangan / Seleksi Awal'
|
|
: 'Panen Utama / Panen Raya');
|
|
|
|
return (
|
|
<div
|
|
className={`rounded-xl border p-4 sm:p-5 shadow-xs mb-4 transition-all ${statusMeta.borderCls}`}
|
|
>
|
|
{/* Header with Title and Status Phase Badge */}
|
|
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 pb-3 border-b border-gray-100">
|
|
<div className="flex items-center gap-2.5">
|
|
<div className="w-8 h-8 rounded-lg bg-emerald-100 flex items-center justify-center shrink-0">
|
|
<i className="fa-solid fa-calendar-check text-emerald-700 text-sm" />
|
|
</div>
|
|
<div>
|
|
<h3 className="text-sm font-bold text-gray-900 flex items-center gap-2">
|
|
Estimasi Waktu Panen Terbaik
|
|
<span className="text-[10px] font-semibold text-emerald-700 bg-emerald-50 px-2 py-0.5 rounded-full border border-emerald-200">
|
|
CP 707
|
|
</span>
|
|
</h3>
|
|
<p className="text-xs text-gray-500">
|
|
Optimalisasi otomatis berbasis efisiensi pakan (FCR) dan puncak Indeks Performa (EEF)
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex items-center gap-2">
|
|
{isLoading ? (
|
|
<span
|
|
role="status"
|
|
className="inline-flex items-center gap-1.5 px-3 py-1 rounded-full text-xs font-semibold border bg-emerald-50 text-emerald-700 border-emerald-200 animate-pulse"
|
|
>
|
|
<i className="fa-solid fa-arrows-rotate fa-spin" />
|
|
Menghitung Proyeksi…
|
|
</span>
|
|
) : (
|
|
<span
|
|
className={`inline-flex items-center gap-1.5 px-3 py-1 rounded-full text-xs font-semibold border ${statusMeta.badgeCls}`}
|
|
>
|
|
<i className={statusMeta.icon} />
|
|
{statusMeta.label}
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Main Countdown & Projection Metrics / Status message */}
|
|
{isLoading ? (
|
|
<div
|
|
className="grid grid-cols-2 sm:grid-cols-4 gap-2.5 pt-3"
|
|
aria-busy="true"
|
|
data-testid="harvest-prediction-loading"
|
|
>
|
|
{/* Tile 1: Countdown & Target Day Skeleton */}
|
|
<div className="bg-white/90 p-3 rounded-lg border border-gray-200/80 shadow-xs flex flex-col justify-between animate-pulse">
|
|
<span className="text-[11px] font-medium text-gray-400 uppercase tracking-wide">
|
|
Waktu Panen Optimal
|
|
</span>
|
|
<div className="mt-2 space-y-2">
|
|
<div className="h-6 w-24 bg-emerald-100/80 rounded" />
|
|
<div className="h-3.5 w-20 bg-gray-200 rounded" />
|
|
</div>
|
|
</div>
|
|
|
|
{/* Tile 2: Estimated Harvest Weight Skeleton */}
|
|
<div className="bg-white/90 p-3 rounded-lg border border-gray-200/80 shadow-xs flex flex-col justify-between animate-pulse">
|
|
<span className="text-[11px] font-medium text-gray-400 uppercase tracking-wide">
|
|
Estimasi Bobot Panen
|
|
</span>
|
|
<div className="mt-2 space-y-2">
|
|
<div className="h-6 w-20 bg-emerald-100/80 rounded" />
|
|
<div className="h-3.5 w-24 bg-gray-200 rounded" />
|
|
</div>
|
|
</div>
|
|
|
|
{/* Tile 3: Projected FCR Skeleton */}
|
|
<div className="bg-white/90 p-3 rounded-lg border border-gray-200/80 shadow-xs flex flex-col justify-between animate-pulse">
|
|
<span className="text-[11px] font-medium text-gray-400 uppercase tracking-wide">
|
|
Proyeksi FCR
|
|
</span>
|
|
<div className="mt-2 space-y-2">
|
|
<div className="h-6 w-16 bg-emerald-100/80 rounded" />
|
|
<div className="h-3.5 w-24 bg-gray-200 rounded" />
|
|
</div>
|
|
</div>
|
|
|
|
{/* Tile 4: Projected EEF Skeleton */}
|
|
<div className="bg-white/90 p-3 rounded-lg border border-gray-200/80 shadow-xs flex flex-col justify-between animate-pulse">
|
|
<span className="text-[11px] font-medium text-gray-400 uppercase tracking-wide">
|
|
Proyeksi EEF / IP
|
|
</span>
|
|
<div className="mt-2 space-y-2">
|
|
<div className="h-6 w-14 bg-emerald-100/80 rounded" />
|
|
<div className="h-3.5 w-24 bg-gray-200 rounded" />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
) : prediction ? (
|
|
<div className="grid grid-cols-2 sm:grid-cols-4 gap-2.5 pt-3">
|
|
{/* Tile 1: Countdown & Target Day */}
|
|
<div className="bg-white/90 p-3 rounded-lg border border-gray-200/80 shadow-xs flex flex-col justify-between">
|
|
<span className="text-[11px] font-medium text-gray-500 uppercase tracking-wide">
|
|
Waktu Panen Optimal
|
|
</span>
|
|
<div className="mt-1">
|
|
<span className="text-xl font-bold text-gray-900">
|
|
Hari ke-{prediction.optimal_harvest_day}
|
|
</span>
|
|
<p className="text-xs font-semibold text-emerald-700 mt-0.5">
|
|
{prediction.days_remaining > 0
|
|
? `± ${prediction.days_remaining} hari lagi`
|
|
: 'Siap Panen Hari Ini'}
|
|
{prediction.optimal_harvest_date ? ` (${prediction.optimal_harvest_date})` : ''}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Tile 2: Estimated Harvest Weight */}
|
|
<div className="bg-white/90 p-3 rounded-lg border border-gray-200/80 shadow-xs flex flex-col justify-between">
|
|
<span className="text-[11px] font-medium text-gray-500 uppercase tracking-wide">
|
|
Estimasi Bobot Panen
|
|
</span>
|
|
<div className="mt-1">
|
|
<span className="text-xl font-bold text-gray-900">
|
|
~{Math.round(prediction.projected_bw_gram).toLocaleString('id-ID')} g
|
|
</span>
|
|
<p className="text-xs text-gray-500 mt-0.5">{recommendationLabel}</p>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Tile 3: Projected FCR */}
|
|
<div className="bg-white/90 p-3 rounded-lg border border-gray-200/80 shadow-xs flex flex-col justify-between">
|
|
<span className="text-[11px] font-medium text-gray-500 uppercase tracking-wide">
|
|
Proyeksi FCR
|
|
</span>
|
|
<div className="mt-1">
|
|
<span className="text-xl font-bold text-gray-900">
|
|
{prediction.projected_fcr.toFixed(3)}
|
|
</span>
|
|
<p className="text-xs text-gray-500 mt-0.5">
|
|
Jendela: Hari {prediction.harvest_window_days[0]}-
|
|
{prediction.harvest_window_days[1]}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Tile 4: Projected EEF */}
|
|
<div className="bg-white/90 p-3 rounded-lg border border-gray-200/80 shadow-xs flex flex-col justify-between">
|
|
<span className="text-[11px] font-medium text-gray-500 uppercase tracking-wide">
|
|
Proyeksi EEF / IP
|
|
</span>
|
|
<div className="mt-1">
|
|
<span className="text-xl font-bold text-emerald-700">
|
|
{Math.round(prediction.projected_eef)}
|
|
</span>
|
|
<p className="text-xs text-gray-500 mt-0.5">Puncak Efisiensi Pakan</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
) : (
|
|
<div className="mt-3 py-3.5 px-4 text-center text-xs bg-blue-50/50 rounded-lg border border-blue-100 text-blue-800 flex items-center justify-center gap-2">
|
|
<i className="fa-solid fa-wand-magic-sparkles text-blue-600 shrink-0" />
|
|
<span>
|
|
Klik tombol <strong>Generate Insight</strong> untuk menghitung estimasi waktu panen
|
|
terbaik berdasarkan kurva efisiensi pakan (FCR) dan puncak EEF.
|
|
</span>
|
|
</div>
|
|
)}
|
|
|
|
{/* Early Harvest Risk Banner */}
|
|
{!isLoading && prediction?.early_harvest_risk && (
|
|
<div className="mt-3 p-3 rounded-lg bg-amber-50 border border-amber-200 text-amber-800 text-xs flex items-center gap-2">
|
|
<i className="fa-solid fa-triangle-exclamation text-amber-600 text-sm shrink-0" />
|
|
<span>
|
|
<strong>Perhatian Panen Dini:</strong> Terdeteksi tren kenaikan mortalitas atau deviasi
|
|
lingkungan mendekati panen. Pertimbangkan percepatan panen untuk mitigasi risiko
|
|
kerugian pakan dan kematian.
|
|
</span>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default HarvestPredictionWidget;
|