Slop cleanup
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useMemo } from 'react';
|
||||
import { useStats } from '../../hooks/useStats';
|
||||
import { formatMinutes } from '../../utils/time';
|
||||
import {
|
||||
@@ -9,9 +9,9 @@ import {
|
||||
CartesianGrid,
|
||||
Tooltip,
|
||||
ResponsiveContainer,
|
||||
Cell,
|
||||
} from 'recharts';
|
||||
|
||||
|
||||
interface DailySummaryProps {
|
||||
from_date: string;
|
||||
to_date: string;
|
||||
@@ -26,16 +26,19 @@ interface ChartDataItem {
|
||||
total: number;
|
||||
}
|
||||
|
||||
interface AvgStats {
|
||||
avgTotal: number;
|
||||
avgDay: number;
|
||||
avgNight: number;
|
||||
count: number;
|
||||
}
|
||||
|
||||
const DailySummary: React.FC<DailySummaryProps> = ({ from_date, to_date, day_start, day_end }) => {
|
||||
const { dailySummary, isLoading } = useStats(from_date, to_date, day_start, day_end);
|
||||
const [chartData, setChartData] = useState<ChartDataItem[]>([]);
|
||||
const [avgStats, setAvgStats] = useState({ avgTotal: 0, avgDay: 0, avgNight: 0, count: 0 });
|
||||
|
||||
useEffect(() => {
|
||||
const { chartData, avgStats } = useMemo<{ chartData: ChartDataItem[]; avgStats: AvgStats }>(() => {
|
||||
if (!dailySummary?.days) {
|
||||
setChartData([]);
|
||||
setAvgStats({ avgTotal: 0, avgDay: 0, avgNight: 0, count: 0 });
|
||||
return;
|
||||
return { chartData: [] as ChartDataItem[], avgStats: { avgTotal: 0, avgDay: 0, avgNight: 0, count: 0 } };
|
||||
}
|
||||
|
||||
const data = dailySummary.days.map((day) => ({
|
||||
@@ -43,9 +46,9 @@ const DailySummary: React.FC<DailySummaryProps> = ({ from_date, to_date, day_sta
|
||||
daySleep: day.day_minutes / 60,
|
||||
nightSleep: day.night_minutes / 60,
|
||||
total: day.total_minutes / 60,
|
||||
}));
|
||||
})).reverse();
|
||||
|
||||
setChartData(data.reverse());
|
||||
let averages = { avgTotal: 0, avgDay: 0, avgNight: 0, count: 0 };
|
||||
|
||||
if (data.length) {
|
||||
const totals = data.reduce(
|
||||
@@ -57,13 +60,15 @@ const DailySummary: React.FC<DailySummaryProps> = ({ from_date, to_date, day_sta
|
||||
{ total: 0, day: 0, night: 0 }
|
||||
);
|
||||
const count = data.length;
|
||||
setAvgStats({
|
||||
averages = {
|
||||
avgTotal: totals.total / count,
|
||||
avgDay: totals.day / count,
|
||||
avgNight: totals.night / count,
|
||||
count,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
return { chartData: data, avgStats: averages };
|
||||
}, [dailySummary]);
|
||||
|
||||
if (isLoading) return <div className="text-center p-4">Loading summary...</div>;
|
||||
@@ -95,10 +100,10 @@ const DailySummary: React.FC<DailySummaryProps> = ({ from_date, to_date, day_sta
|
||||
width={60}
|
||||
/>
|
||||
<Tooltip
|
||||
formatter={(value: any, name: any) => [
|
||||
formatter={(value: number, name: string) => [
|
||||
formatMinutes(Math.round((value ?? 0) * 60)),
|
||||
name === 'daySleep' ? 'Day' : 'Night',
|
||||
] as any}
|
||||
]}
|
||||
/>
|
||||
<Bar dataKey="nightSleep" stackId="a" fill="#1e3a5f" />
|
||||
<Bar dataKey="daySleep" stackId="a" fill="#fbbf24" />
|
||||
|
||||
Reference in New Issue
Block a user