import * as React from "react"; import { Box, Typography, ToggleButtonGroup, ToggleButton, Paper } from "@mui/material"; import { ChartDataPoint, HistoryChartProps, ChartData, } 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 = ( point: ChartDataPoint, tab: string, comparison: boolean ) => { const base = point.amount; const cmp = point.compareAmount ?? 0; const formatShort = (val: number) => { if (tab === "monthly") { if (val >= 100000) return `${(val / 100000).toFixed(2)}L`; } if (tab === "weekly") { if (val >= 1000) return `${(val / 1000).toFixed(1)}K`; } return val.toLocaleString("en-IN"); }; // Only hide diff when comparison OFF or compare is undefined if (!comparison) { return `₹ ${formatShort(base)}`; } const diff = base - cmp; const sign = diff >= 0 ? "+" : "-"; const absDiff = Math.abs(diff); return `₹ ${formatShort(base)} (${sign}${formatShort(absDiff)})`; }; const formatLabel = (label: string, type: string) => { if (type === "monthly") return label; if (type === "weekly") { const parts = label.split(" - "); if (parts.length === 2) { const [start, end] = parts; const startDay = start.split(" ")[0]; const endParts = end.split(" "); const endDay = endParts[0]; const month = endParts[1]; return `${startDay}–${endDay} ${month}`; } } return label; }; export default function HistoryChart({ header, summary, tabs, data, period, onPeriodChange, comparison, setComparison, colorScheme, }: HistoryChartProps) { const [activeTab, setActiveTab] = React.useState(tabs[0] || ""); const handleTabChange = (_: React.MouseEvent, newTab: string | null) => { if (newTab !== null) setActiveTab(newTab); }; const activeDataKey = activeTab.toLowerCase() as keyof ChartData; let rawData: ChartDataPoint[] = []; if (activeDataKey === "daily") { rawData = data.daily || []; } else { const section = data[activeDataKey]; rawData = section?.[period] || []; } const currentData = rawData; const maxAmount = currentData.length > 0 ? Math.max( ...currentData.flatMap((d) => comparison ? [d.amount, d.compareAmount || 0] : [d.amount] ), 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 ( {header} {summary && ( {summary} )} {tabs.map((tab) => ( {tab} ))} {/* Rolling / Calendar */} v && onPeriodChange(v)} size="small" > Rolling Calendar {/* Compare toggle */} setComparison(!comparison)} size="small" sx={{ textTransform: "none", borderRadius: 2, 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 {currentData.length > 0 ? ( {/* LEFT ARROW */} {canGoLeft && ( )} {/* CHART */} {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 ( {formatDisplay(point, activeTab.toLowerCase(), comparison)} {/* Compare */} {comparison && ( )} {/* Spacer */} {/* Current */} {formatLabel(point.id, activeDataKey)} {point.compareLabel ? formatLabel(point.compareLabel, activeDataKey) : "placeholder"} ); })} {/* RIGHT ARROW */} {canGoRight && ( )} ) : ( No Data Available )} ); }