Compare commits

..

4 Commits

3 changed files with 260 additions and 139 deletions

View File

@@ -126,17 +126,8 @@ export default function Dashboard() {
</Box> </Box>
<Grid container spacing={4} direction="row"> <Grid container spacing={4} direction="row">
{/* LEFT → 1/3 */}
<Grid size={4}>
<LatestItemsList
title={`Recent ${mode === "expense" ? "Expenses" : "Income"}`}
items={currentLatest}
onViewAll={() => {}}
/>
</Grid>
{/* RIGHT → 2/3 */} <Grid size={12}>
<Grid size={8}>
<HistoryChart <HistoryChart
header={`${mode === "expense" ? "Expense" : "Income"} Breakdown`} header={`${mode === "expense" ? "Expense" : "Income"} Breakdown`}
summary="Interactive chronological tracking" summary="Interactive chronological tracking"
@@ -148,6 +139,15 @@ export default function Dashboard() {
setComparison={setComparison} setComparison={setComparison}
/> />
</Grid> </Grid>
<Grid size={12}>
<LatestItemsList
title={`Recent ${mode === "expense" ? "Expenses" : "Income"}`}
items={currentLatest}
onViewAll={() => {}}
/>
</Grid>
</Grid> </Grid>
</Container> </Container>
); );

View File

@@ -11,6 +11,9 @@ import {
HistoryChartProps, HistoryChartProps,
ChartData, ChartData,
} from "../types/historyChart"; } from "../types/historyChart";
import IconButton from "@mui/material/IconButton";
import ChevronLeftIcon from "@mui/icons-material/ChevronLeft";
import ChevronRightIcon from "@mui/icons-material/ChevronRight";
const formatDisplay = ( const formatDisplay = (
point: ChartDataPoint, point: ChartDataPoint,
@@ -99,6 +102,37 @@ export default function HistoryChart({
) )
: 1; : 1;
const [startIndex, setStartIndex] = React.useState(0);
const visibleCountDataTabMapping = {
daily: 7,
weekly: 6,
monthly: 4,
}
const visibleCount = visibleCountDataTabMapping[activeDataKey];
const total = currentData.length;
// clamp startIndex so we always show full 5 (when possible)
const clampedStartIndex = Math.min(
startIndex,
Math.max(total - visibleCount, 0)
);
const visibleData = currentData.slice(
clampedStartIndex,
clampedStartIndex + visibleCount
);
const canGoLeft = startIndex > 0;
const canGoRight = startIndex + visibleCount < currentData.length;
const handlePrev = () => {
if (canGoLeft) setStartIndex((prev) => prev - visibleCount);
};
const handleNext = () => {
if (canGoRight) setStartIndex((prev) => prev + visibleCount);
};
return ( return (
<Paper <Paper
sx={{ sx={{
@@ -134,141 +168,214 @@ export default function HistoryChart({
))} ))}
</ToggleButtonGroup> </ToggleButtonGroup>
<ToggleButtonGroup <Box
value={period} sx={{
exclusive display: "flex",
onChange={(_, v) => v && onPeriodChange(v)} alignItems: "center",
size="small" justifyContent: "space-between",
sx={{ mb: 2 }} mb: 3
}}
> >
<ToggleButton value="rolling">Rolling</ToggleButton> {/* Rolling / Calendar */}
<ToggleButton value="calendar" disabled={activeDataKey === "daily"}> <ToggleButtonGroup
Calendar value={period}
</ToggleButton> exclusive
</ToggleButtonGroup> onChange={(_, v) => v && onPeriodChange(v)}
size="small"
>
<ToggleButton value="rolling">Rolling</ToggleButton>
<ToggleButton
value="calendar"
disabled={activeDataKey === "daily"}
>
Calendar
</ToggleButton>
</ToggleButtonGroup>
<ToggleButtonGroup {/* Compare toggle */}
value={comparison ? "on" : "off"} <ToggleButton
exclusive value="compare"
onChange={(_, v) => setComparison(v === "on")} selected={comparison}
size="small" onChange={() => setComparison(!comparison)}
sx={{ mb: 2 }} size="small"
> sx={{
<ToggleButton value="off">Single</ToggleButton> textTransform: "none",
<ToggleButton value="on">Compare</ToggleButton> borderRadius: 2,
</ToggleButtonGroup> px: 2,
// OFF
color: "text.secondary",
border: "1px solid",
borderColor: "divider",
// ON
"&.Mui-selected": {
color: "white",
bgcolor: "success.main",
borderColor: "success.main"
},
"&.Mui-selected:hover": {
bgcolor: "success.dark"
}
}}
>
Compare
</ToggleButton>
</Box>
{currentData.length > 0 ? ( {currentData.length > 0 ? (
<Box sx={{ display: "flex", alignItems: "flex-end", height: 220, mt: 4 }}> <Box sx={{ position: "relative", mt: 4 }}>
{currentData.map((point) => {
const currentHeight = (point.amount / maxAmount) * 100;
const compareHeight = comparison
? ((point.compareAmount || 0) / maxAmount) * 100
: 0;
const labelHeight = Math.max(currentHeight, compareHeight);
return ( {/* LEFT ARROW */}
<Box {canGoLeft && (
key={point.id} <IconButton
sx={{ onClick={handlePrev}
flex: 1, size="small"
display: "flex", sx={{
flexDirection: "column", position: "absolute",
alignItems: "center", left: 0,
justifyContent: "flex-end", top: "50%",
height: "100%" transform: "translateY(-50%)",
}} zIndex: 2,
> bgcolor: "background.paper",
boxShadow: 1
}}
>
<ChevronLeftIcon fontSize="small" />
</IconButton>
)}
{/* CHART */}
<Box sx={{ display: "flex", alignItems: "flex-end", height: 220, mt: 4 }}>
{visibleData.map((point) => {
const currentHeight = (point.amount / maxAmount) * 100;
const compareHeight = comparison
? ((point.compareAmount || 0) / maxAmount) * 100
: 0;
const labelHeight = Math.max(currentHeight, compareHeight);
return (
<Box <Box
key={point.id}
sx={{ sx={{
display: "flex", flex: 1,
alignItems: "flex-end",
gap: comparison ? 0.5 : 0,
height: "100%",
position: "relative"
}}
>
<Typography
variant="caption"
sx={{
position: "absolute",
bottom: `${labelHeight}%`,
left: "50%",
transform: "translate(-50%, -6px)",
fontSize: "0.65rem",
whiteSpace: "nowrap",
pointerEvents: "none"
}}
>
{formatDisplay(point, activeTab.toLowerCase(), comparison)}
</Typography>
{/* Compare */}
{comparison && (
<Box
sx={{
width: 6,
height: `${compareHeight}%`,
bgcolor: "grey.400",
borderRadius: 2
}}
/>
)}
{/* Spacer */}
<Box sx={{ width: 4 }} />
{/* Current */}
<Box
sx={{
width: 10,
height: `${currentHeight}%`,
bgcolor: point.highlighted ? "error.main" : "primary.main",
borderRadius: 2
}}
/>
</Box>
<Box
sx={{
mt: 1,
textAlign: "center",
display: "flex", display: "flex",
flexDirection: "column", flexDirection: "column",
alignItems: "center", alignItems: "center",
lineHeight: 1.1 justifyContent: "flex-end",
height: "100%"
}} }}
> >
<Typography <Box
variant="caption"
sx={{ sx={{
fontSize: "0.7rem", display: "flex",
opacity: 0.7, alignItems: "flex-end",
color: "text.primary", gap: comparison ? 0.5 : 0,
height: "100%",
position: "relative"
}} }}
> >
{formatLabel(point.id, activeDataKey)} <Typography
</Typography> variant="caption"
sx={{
position: "absolute",
bottom: `${labelHeight}%`,
left: "50%",
transform: "translate(-50%, -6px)",
fontSize: "0.65rem",
whiteSpace: "nowrap",
pointerEvents: "none"
}}
>
{formatDisplay(point, activeTab.toLowerCase(), comparison)}
</Typography>
<Typography {/* Compare */}
variant="caption" {comparison && (
<Box
sx={{
width: 6,
height: `${compareHeight}%`,
bgcolor: "grey.400",
borderRadius: 2
}}
/>
)}
{/* Spacer */}
<Box sx={{ width: 4 }} />
{/* Current */}
<Box
sx={{
width: 10,
height: `${currentHeight}%`,
bgcolor: point.highlighted ? "error.main" : "primary.main",
borderRadius: 2
}}
/>
</Box>
<Box
sx={{ sx={{
fontSize: "0.65rem", mt: 1,
color: "grey.400", textAlign: "center",
visibility: display: "flex",
comparison && point.compareLabel && activeDataKey !== "daily" flexDirection: "column",
? "visible" alignItems: "center",
: "hidden" lineHeight: 1.1
}} }}
> >
{point.compareLabel <Typography
? formatLabel(point.compareLabel, activeDataKey) variant="caption"
: "placeholder"} sx={{
</Typography> fontSize: "0.7rem",
opacity: 0.7,
color: "text.primary",
}}
>
{formatLabel(point.id, activeDataKey)}
</Typography>
<Typography
variant="caption"
sx={{
fontSize: "0.65rem",
color: "grey.400",
visibility:
comparison && point.compareLabel && activeDataKey !== "daily"
? "visible"
: "hidden"
}}
>
{point.compareLabel
? formatLabel(point.compareLabel, activeDataKey)
: "placeholder"}
</Typography>
</Box>
</Box> </Box>
</Box> );
); })}
})} </Box>
{/* RIGHT ARROW */}
{canGoRight && (
<IconButton
onClick={handleNext}
size="small"
sx={{
position: "absolute",
right: 0,
top: "50%",
transform: "translateY(-50%)",
zIndex: 2,
bgcolor: "background.paper",
boxShadow: 1
}}
>
<ChevronRightIcon fontSize="small" />
</IconButton>
)}
</Box> </Box>
) : ( ) : (
<Box sx={{ height: 200, display: "flex", alignItems: "center", justifyContent: "center" }}> <Box sx={{ height: 200, display: "flex", alignItems: "center", justifyContent: "center" }}>

View File

@@ -1,6 +1,6 @@
import { api } from "../../react-openapi"; import { api } from "../../react-openapi";
import { LatestItem } from "../components/LatestItemsList"; import { LatestItem } from "../components/LatestItemsList";
import { ChartDataPoint } from "../components/HistoryChart"; import { ChartDataPoint } from "../types/historyChart";
import * as React from "react"; import * as React from "react";
import { format } from "./dateUtils"; import { format } from "./dateUtils";
import MonetizationOnIcon from "@mui/icons-material/MonetizationOn"; import MonetizationOnIcon from "@mui/icons-material/MonetizationOn";
@@ -126,15 +126,29 @@ export async function fetchAggregatedData(
apply(monthlyCalendar); apply(monthlyCalendar);
} }
const toPoints = (arr: any[]): ChartDataPoint[] => const toPoints = (arr: any[], type: "weekly" | "monthly"): ChartDataPoint[] =>
arr.map((x) => ({ arr.map((x) => {
id: x.label, let compareLabel: string | undefined;
amount: x.amount,
compareAmount: x.compare, if (x.prevStart && x.prevEnd) {
compareLabel: x.prevStart && x.prevEnd if (type === "monthly") {
? `${format(x.prevStart)} - ${format(x.prevEnd)}` const year = String(x.prevStart.getFullYear()).slice(2);
: undefined compareLabel = `${x.prevStart.toLocaleString("default", {
})); month: "short"
})}-${year}`;
} else {
const year = String(x.prevEnd.getFullYear()).slice(2);
compareLabel = `${format(x.prevStart)} - ${format(x.prevEnd)} ${year}`;
}
}
return {
id: x.label,
amount: x.amount,
compareAmount: x.compare,
compareLabel
};
});
const chartData = { const chartData = {
daily: Object.entries(dailyBuckets).map(([k, v]: any) => ({ daily: Object.entries(dailyBuckets).map(([k, v]: any) => ({
@@ -143,12 +157,12 @@ export async function fetchAggregatedData(
compareAmount: v.compare compareAmount: v.compare
})), })),
weekly: { weekly: {
rolling: toPoints(weeklyRolling), rolling: toPoints(weeklyRolling, "weekly"),
calendar: toPoints(weeklyCalendar) calendar: toPoints(weeklyCalendar, "weekly")
}, },
monthly: { monthly: {
rolling: toPoints(monthlyRolling), rolling: toPoints(monthlyRolling, "monthly"),
calendar: toPoints(monthlyCalendar) calendar: toPoints(monthlyCalendar, "monthly")
} }
}; };