208 lines
8.3 KiB
TypeScript
208 lines
8.3 KiB
TypeScript
import React, { useState, useEffect } from 'react';
|
|
import { CycleData, CyclePhase, CycleStatus } from '../types.ts';
|
|
import { useAppContext } from '../context/AppContext.tsx';
|
|
import CycleCreateModal from './cycle/CycleCreateModal.tsx';
|
|
|
|
const PhaseSegment: React.FC<{ phase: CyclePhase; totalDays: number }> = ({ phase, totalDays }) => {
|
|
const widthPercentage = ((phase.endDay - phase.startDay + 1) / totalDays) * 100;
|
|
return (
|
|
<div
|
|
className={`h-full ${phase.color} transition-all duration-300 hover:brightness-110 flex items-center justify-center text-white text-sm font-bold`}
|
|
style={{ width: `${widthPercentage}%` }}
|
|
title={`${phase.name} (Hari ${phase.startDay}-${phase.endDay}): ${phase.description}`}
|
|
>
|
|
<span className="hidden sm:inline opacity-75 group-hover:opacity-100">{phase.name}</span>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
const statusConfig: Record<CycleStatus, { bg: string; text: string; icon: string }> = {
|
|
Completed: { bg: 'bg-green-100', text: 'text-green-800', icon: 'fa-check-circle' },
|
|
Active: { bg: 'bg-red-100', text: 'text-red-800', icon: 'fa-running' },
|
|
Upcoming: { bg: 'bg-blue-100', text: 'text-blue-800', icon: 'fa-calendar-alt' },
|
|
};
|
|
|
|
const CycleDisplayHeader: React.FC<{
|
|
cycle: CycleData;
|
|
}> = ({ cycle }) => {
|
|
const getHarvestDate = () => {
|
|
const harvestDate = new Date(cycle.startDate);
|
|
harvestDate.setDate(harvestDate.getDate() + cycle.totalDays - 1);
|
|
return harvestDate.toLocaleDateString('id-ID', {
|
|
day: 'numeric',
|
|
month: 'long',
|
|
year: 'numeric',
|
|
});
|
|
};
|
|
|
|
return (
|
|
<div className="flex flex-col sm:flex-row justify-between sm:items-center mb-4 gap-4">
|
|
<div>
|
|
<p className="text-sm text-gray-500">
|
|
ID Siklus: <span className="font-mono">{cycle.id}</span>
|
|
</p>
|
|
</div>
|
|
<div className="text-sm sm:text-right">
|
|
<p className="font-semibold text-gray-700">Estimasi Panen</p>
|
|
<p className="text-red-600 font-bold">{getHarvestDate()}</p>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
const SingleCycleView: React.FC<{ cycle: CycleData }> = ({ cycle }) => {
|
|
const { totalDays, startDate, phases, status } = cycle;
|
|
|
|
// Hitung currentDay dinamis berdasarkan tanggal hari ini dan tanggal mulai siklus
|
|
const today = new Date();
|
|
const start = new Date(startDate);
|
|
start.setHours(0, 0, 0, 0);
|
|
const todayMs = today.setHours(0, 0, 0, 0);
|
|
const diffMsToday = todayMs - start.getTime();
|
|
|
|
let derivedCurrentDay = 0;
|
|
if (diffMsToday >= 0) {
|
|
derivedCurrentDay = Math.floor(diffMsToday / (1000 * 60 * 60 * 24)) + 1;
|
|
if (derivedCurrentDay > totalDays) derivedCurrentDay = totalDays;
|
|
}
|
|
|
|
const progressPercentage = (derivedCurrentDay / totalDays) * 100;
|
|
|
|
const getHarvestDate = () => {
|
|
const harvestDate = new Date(startDate);
|
|
harvestDate.setDate(harvestDate.getDate() + totalDays - 1);
|
|
return harvestDate.toLocaleDateString('id-ID', {
|
|
day: 'numeric',
|
|
month: 'long',
|
|
year: 'numeric',
|
|
});
|
|
};
|
|
|
|
return (
|
|
<div className="pt-4">
|
|
<CycleDisplayHeader cycle={cycle} />
|
|
<div className="relative pt-8">
|
|
<div className="relative h-8 bg-gray-200 rounded-lg flex items-center shadow-inner">
|
|
{phases.map((phase) => (
|
|
<PhaseSegment key={phase.name} phase={phase} totalDays={totalDays} />
|
|
))}
|
|
</div>
|
|
{status === 'Active' && derivedCurrentDay > 0 && (
|
|
<div
|
|
className="absolute top-0 transition-all duration-500 ease-out"
|
|
style={{ left: `calc(${progressPercentage}% - 2.5rem)` }}
|
|
>
|
|
<div className="relative w-24 text-center">
|
|
<div className="font-bold text-red-600 bg-red-100 px-3 py-1.5 rounded-md shadow-sm border border-red-200 text-base">
|
|
Hari ke-{derivedCurrentDay}
|
|
</div>
|
|
<div className="absolute left-1/2 -translate-x-1/2 w-0 h-0 border-l-8 border-l-transparent border-r-8 border-r-transparent border-t-8 border-t-red-100"></div>
|
|
<div className="absolute top-full left-1/2 -translate-x-1/2 h-2.5 w-0.5 bg-red-400"></div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
<div className="flex justify-between items-center text-sm font-semibold text-gray-500 mt-2 px-1">
|
|
<div className="flex flex-col items-start">
|
|
<span>Hari 1</span>
|
|
<span className="text-xs font-normal text-gray-400">
|
|
{startDate.toLocaleDateString('id-ID', {
|
|
day: 'numeric',
|
|
month: 'short',
|
|
year: 'numeric',
|
|
})}
|
|
</span>
|
|
{cycle.chickInWeight && (
|
|
<span className="text-xs font-normal text-gray-400">
|
|
Berat DOC: {cycle.chickInWeight} gram
|
|
</span>
|
|
)}
|
|
{cycle.docInCount && (
|
|
<span className="text-xs font-normal text-gray-400">
|
|
DOC In: {cycle.docInCount.toLocaleString()} ekor
|
|
</span>
|
|
)}
|
|
</div>
|
|
<div className="flex flex-col items-end">
|
|
<span>Hari {totalDays}</span>
|
|
<span className="text-xs font-normal text-gray-400">{getHarvestDate()}</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
const CycleTimeline: React.FC = () => {
|
|
const { cycles, onCreateCycle } = useAppContext();
|
|
const activeCycle = cycles.find((c) => c.status === 'Active');
|
|
const [selectedCycleId, setSelectedCycleId] = useState<string>(
|
|
activeCycle?.id || cycles[0]?.id || ''
|
|
);
|
|
const [isCreateModalOpen, setIsCreateModalOpen] = useState(false);
|
|
|
|
// Only set to active cycle on initial mount or when cycles change and current selection is empty
|
|
useEffect(() => {
|
|
if (!selectedCycleId) {
|
|
const activeCycle = cycles.find((c) => c.status === 'Active');
|
|
if (activeCycle) setSelectedCycleId(activeCycle.id);
|
|
}
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [cycles]);
|
|
|
|
const selectedCycle = cycles.find((c) => c.id === selectedCycleId);
|
|
|
|
return (
|
|
<div className="bg-white p-6 sm:p-8 rounded-xl shadow-sm mb-8">
|
|
<h2 className="text-2xl font-bold text-gray-800 mb-4">Linimasa Siklus Produksi</h2>
|
|
<div className="border-b border-gray-200 overflow-x-auto">
|
|
<nav className="-mb-px flex space-x-6 min-w-max" aria-label="Tabs">
|
|
{cycles.map((cycle) => {
|
|
const config = statusConfig[cycle.status];
|
|
const isActive = cycle.id === selectedCycleId;
|
|
return (
|
|
<button
|
|
key={cycle.id}
|
|
onClick={() => setSelectedCycleId(cycle.id)}
|
|
className={`flex items-center gap-2 px-3 py-2 text-sm font-semibold rounded-t-lg transition-colors border-b-2 whitespace-nowrap ${
|
|
isActive
|
|
? 'border-red-500 text-red-600'
|
|
: 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300'
|
|
}`}
|
|
>
|
|
<i className={`fa-solid ${config.icon}`}></i>
|
|
<span>
|
|
{cycle.startDate.toLocaleDateString('id-ID', { month: 'short', year: 'numeric' })}{' '}
|
|
({cycle.status})
|
|
</span>
|
|
</button>
|
|
);
|
|
})}
|
|
<button
|
|
onClick={() => setIsCreateModalOpen(true)}
|
|
className="flex items-center gap-2 px-3 py-2 text-sm font-semibold rounded-t-lg transition-colors border-b-2 border-transparent text-green-600 hover:text-green-800 hover:border-green-300 whitespace-nowrap"
|
|
title="Siklus Baru"
|
|
>
|
|
<i className="fa-solid fa-plus-circle"></i>
|
|
<span>Siklus Baru</span>
|
|
</button>
|
|
</nav>
|
|
</div>
|
|
{selectedCycle ? <SingleCycleView cycle={selectedCycle} /> : <p>No cycle selected.</p>}
|
|
<CycleCreateModal
|
|
isOpen={isCreateModalOpen}
|
|
onClose={() => setIsCreateModalOpen(false)}
|
|
onSave={(startDate, docInCount, chickInWeight) => {
|
|
onCreateCycle(startDate, docInCount, chickInWeight);
|
|
setIsCreateModalOpen(false);
|
|
const year = startDate.getFullYear();
|
|
const month = String(startDate.getMonth() + 1).padStart(2, '0');
|
|
const day = String(startDate.getDate()).padStart(2, '0');
|
|
setSelectedCycleId(`CYCLE-JBW-${year}-${month}-${day}`);
|
|
}}
|
|
/>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default CycleTimeline;
|