update karung related component to follow cutoff time

This commit is contained in:
Alberto-Audrix committed 2026-10-09 08:46:13 +07:00
1 parent 26c69b340d
commit 03c7dcb646
5 files changed
+84 -53

No files matched your search

+26 -16
View File
@@ -10,6 +10,7 @@ import {
resolveCycleStatus,
todayIso,
} from '../utils/format.ts';
import { throughVisibleDate } from '../utils/visibility.ts';
import { buildFeedSackInsightContext } from '../utils/insightContextBuilders.ts';
import FeedStats from './feedSack/FeedStats.tsx';
import FeedSaldoCard from './feedSack/FeedSaldoCard.tsx';
@@ -91,22 +92,30 @@ const FeedSackCountingPage: React.FC<{ onSettings: () => void }> = ({ onSettings
}, [selectedCycle?.id, reload]);
const sortedRows = useMemo(() => [...rows].sort((a, b) => a.date.localeCompare(b.date)), [rows]);
const latest = sortedRows[sortedRows.length - 1] ?? null;
const sortedManualRows = useMemo(
() => [...manualRows].sort((a, b) => a.date.localeCompare(b.date)),
[manualRows]
);
const visibleThrough = selectedCycle?.visible_through_date;
// Display date follows the latest IoT snapshot; without any snapshot, fall back to today.
// Manual cards only use a same-date row — never fall back to an older day.
const visibleRows = useMemo(
() => throughVisibleDate(sortedRows, visibleThrough),
[sortedRows, visibleThrough]
);
const visibleManualRows = useMemo(
() => throughVisibleDate(sortedManualRows, visibleThrough),
[sortedManualRows, visibleThrough]
);
const latest = visibleRows[visibleRows.length - 1] ?? null;
// Display date follows the latest visible IoT snapshot; without any snapshot, fall back to cutoff date / today.
const displayDate = latest?.date ?? visibleThrough ?? todayIso();
const latestManual = sortedManualRows.find((row) => row.date === displayDate) ?? null;
const latestManual = visibleManualRows.find((row) => row.date === displayDate) ?? null;
const lastRecordedManual = visibleManualRows[visibleManualRows.length - 1] ?? null;
const currentDay = useMemo(() => {
if (!selectedCycle) return 0;
const boundDate = selectedCycle.end_date ?? selectedCycle.visible_through_date ?? todayIso();
const boundDate = selectedCycle.end_date ?? visibleThrough ?? todayIso();
return cycleDayForDate(selectedCycle.start_date, boundDate);
}, [selectedCycle]);
}, [selectedCycle, visibleThrough]);
const handleSync = useCallback(async () => {
if (!selectedCycle) return;
@@ -151,8 +160,8 @@ const FeedSackCountingPage: React.FC<{ onSettings: () => void }> = ({ onSettings
const insightContext = useMemo(() => {
if (!selectedKandang || !selectedCycle) return null;
const manualByDate = new Map(sortedManualRows.map((row) => [row.date, row]));
const dailyRows = sortedRows.map((row) => {
const manualByDate = new Map(visibleManualRows.map((row) => [row.date, row]));
const dailyRows = visibleRows.map((row) => {
const hari = cycleDayForDate(selectedCycle.start_date, row.date);
const manual = manualByDate.get(row.date);
return {
@@ -174,10 +183,10 @@ const FeedSackCountingPage: React.FC<{ onSettings: () => void }> = ({ onSettings
total_karung_masuk_iot: latest?.in_total ?? null,
total_karung_dituang_iot: latest?.feed_use_total ?? null,
total_karung_keluar_iot: latest?.out_total ?? null,
total_karung_masuk_manual: latestManual?.feed_in_manual_total ?? null,
total_karung_dituang_manual: latestManual?.feed_use_manual_total ?? null,
total_karung_masuk_manual: lastRecordedManual?.feed_in_manual_total ?? null,
total_karung_dituang_manual: lastRecordedManual?.feed_use_manual_total ?? null,
});
}, [selectedKandang, selectedCycle, sortedRows, sortedManualRows, latest, latestManual]);
}, [selectedKandang, selectedCycle, visibleRows, visibleManualRows, latest, lastRecordedManual]);
if (!selectedCycle) return <EmptyFarm onSettings={onSettings} />;
@@ -321,7 +330,7 @@ const FeedSackCountingPage: React.FC<{ onSettings: () => void }> = ({ onSettings
{selectedKandang && insightContext ? (
<PageAiInsight
topic="hitung_karung"
cacheKey={`pakan-k${selectedKandang.id}-c${selectedCycle.id}-day${currentDay}-rows${sortedRows.length}`}
cacheKey={`pakan-k${selectedKandang.id}-c${selectedCycle.id}-day${currentDay}-rows${visibleRows.length}`}
contextData={insightContext}
chartSpec={{
type: 'trend',
@@ -399,6 +408,7 @@ const FeedSackCountingPage: React.FC<{ onSettings: () => void }> = ({ onSettings
<FeedStats
latest={latest}
latestManual={latestManual}
lastRecordedManual={lastRecordedManual}
cycleStartDate={selectedCycle.start_date}
displayDate={displayDate}
/>
@@ -413,15 +423,15 @@ const FeedSackCountingPage: React.FC<{ onSettings: () => void }> = ({ onSettings
{/* Tren harian */}
<FeedTrendChart
rows={sortedRows}
rows={visibleRows}
cycleStartDate={selectedCycle.start_date}
currentDay={currentDay}
/>
{/* Rincian harian */}
<FeedDailySummaryTable
rows={sortedRows}
manualRows={manualRows}
rows={visibleRows}
manualRows={visibleManualRows}
cycleStartDate={selectedCycle.start_date}
currentDay={currentDay}
/>