Compare commits
42 Commits
main
...
71ba700322
| Author | SHA1 | Date | |
|---|---|---|---|
| 71ba700322 | |||
| 089381125f | |||
| 1a5261ab25 | |||
| 55b604f700 | |||
| 04b72d5843 | |||
| b97e350b1b | |||
| df5cf9fbb6 | |||
| 4b046c15a5 | |||
| 02eb55995e | |||
| 4e56d86cdb | |||
| 15f76eb5f0 | |||
| 7470da6d2d | |||
| 34594215f9 | |||
| 0a92126b92 | |||
| 30cf227050 | |||
| a0e62b1bc4 | |||
| ea3b451266 | |||
| 4b4875c3f5 | |||
| 25bd882b75 | |||
| f684083496 | |||
| 0e0928af95 | |||
| 7b0b3fb615 | |||
| 38f7416942 | |||
| e82cad4f21 | |||
| 1daa90d091 | |||
| 2d0b0bc470 | |||
| 5f85abdf86 | |||
| cc7e6509d2 | |||
| 8a3ebdb1be | |||
| a36d9119bb | |||
| 67d4c85146 | |||
| 89ad8e376e | |||
| 71afc157ff | |||
| 5acbb7ccdd | |||
| 3fd20f11ab | |||
| 922d05ae37 | |||
| 1fe44abfde | |||
| 49bdb85088 | |||
| b1509fd5ab | |||
| 175ca64d1f | |||
| c9e609fee6 | |||
| 82264a5c34 |
@@ -1,7 +1,6 @@
|
|||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import {
|
import {
|
||||||
ReportData,
|
ReportData
|
||||||
GroupKey,
|
|
||||||
} from "../../features/report";
|
} from "../../features/report";
|
||||||
|
|
||||||
export type DashboardMode = "expense" | "income";
|
export type DashboardMode = "expense" | "income";
|
||||||
@@ -12,7 +11,6 @@ export interface DashboardState {
|
|||||||
mode: DashboardMode;
|
mode: DashboardMode;
|
||||||
periodType: DashboardPeriodType;
|
periodType: DashboardPeriodType;
|
||||||
selectedPeriodId: DashboardSelectedPeriodId;
|
selectedPeriodId: DashboardSelectedPeriodId;
|
||||||
selectedGroupKey: GroupKey | null;
|
|
||||||
comparison: boolean;
|
comparison: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ export default function Dashboard(props: DashboardProps) {
|
|||||||
mode: "expense",
|
mode: "expense",
|
||||||
periodType: "rolling",
|
periodType: "rolling",
|
||||||
selectedPeriodId: null,
|
selectedPeriodId: null,
|
||||||
selectedGroupKey: null,
|
|
||||||
comparison: false,
|
comparison: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -36,10 +35,6 @@ export default function Dashboard(props: DashboardProps) {
|
|||||||
setState(prev => ({ ...prev, selectedPeriodId }));
|
setState(prev => ({ ...prev, selectedPeriodId }));
|
||||||
};
|
};
|
||||||
|
|
||||||
const setSelectedGroupKey = (groupKey: typeof state.selectedGroupKey) => {
|
|
||||||
setState(prev => ({ ...prev, selectedGroupKey: groupKey }));
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DashboardView
|
<DashboardView
|
||||||
{...props}
|
{...props}
|
||||||
@@ -49,7 +44,6 @@ export default function Dashboard(props: DashboardProps) {
|
|||||||
togglePeriodType={togglePeriodType}
|
togglePeriodType={togglePeriodType}
|
||||||
toggleComparison={toggleComparison}
|
toggleComparison={toggleComparison}
|
||||||
setSelectedPeriodId={setSelectedPeriodId}
|
setSelectedPeriodId={setSelectedPeriodId}
|
||||||
setSelectedGroupKey={setSelectedGroupKey}
|
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import {
|
|||||||
ToggleButtonGroup
|
ToggleButtonGroup
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
import { useTheme, alpha } from "@mui/material/styles";
|
import { useTheme, alpha } from "@mui/material/styles";
|
||||||
import { GroupKey } from "../../features/report";
|
|
||||||
import { DashboardProps, DashboardState } from "./Dashboard.models";
|
import { DashboardProps, DashboardState } from "./Dashboard.models";
|
||||||
|
|
||||||
interface ViewProps extends DashboardProps {
|
interface ViewProps extends DashboardProps {
|
||||||
@@ -17,7 +16,6 @@ interface ViewProps extends DashboardProps {
|
|||||||
toggleMode: () => void;
|
toggleMode: () => void;
|
||||||
togglePeriodType: () => void;
|
togglePeriodType: () => void;
|
||||||
setSelectedPeriodId: (id: string | null) => void;
|
setSelectedPeriodId: (id: string | null) => void;
|
||||||
setSelectedGroupKey: (groupKey: GroupKey | null) => void;
|
|
||||||
toggleComparison: () => void;
|
toggleComparison: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,11 +28,10 @@ export default function DashboardView({
|
|||||||
togglePeriodType,
|
togglePeriodType,
|
||||||
toggleComparison,
|
toggleComparison,
|
||||||
setSelectedPeriodId,
|
setSelectedPeriodId,
|
||||||
setSelectedGroupKey,
|
|
||||||
}: ViewProps) {
|
}: ViewProps) {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const themeMode = theme.palette.mode;
|
const themeMode = theme.palette.mode;
|
||||||
const { mode, periodType, comparison, selectedPeriodId, selectedGroupKey } = state;
|
const { mode, periodType, comparison, selectedPeriodId } = state;
|
||||||
|
|
||||||
// Resolve colors with fallbacks
|
// Resolve colors with fallbacks
|
||||||
const colors = React.useMemo(() => {
|
const colors = React.useMemo(() => {
|
||||||
@@ -123,12 +120,10 @@ export default function DashboardView({
|
|||||||
periodType={periodType}
|
periodType={periodType}
|
||||||
comparison={comparison}
|
comparison={comparison}
|
||||||
selectedPeriodId={selectedPeriodId}
|
selectedPeriodId={selectedPeriodId}
|
||||||
selectedGroupKey={selectedGroupKey}
|
|
||||||
|
|
||||||
togglePeriodType={togglePeriodType}
|
togglePeriodType={togglePeriodType}
|
||||||
toggleComparison={toggleComparison}
|
toggleComparison={toggleComparison}
|
||||||
setSelectedPeriodId={setSelectedPeriodId}
|
setSelectedPeriodId={setSelectedPeriodId}
|
||||||
setSelectedGroupKey={setSelectedGroupKey}
|
|
||||||
/>
|
/>
|
||||||
</Grid>
|
</Grid>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,75 +0,0 @@
|
|||||||
import { ReportData } from "../../features/report";
|
|
||||||
import {
|
|
||||||
mergeBucketPeriods,
|
|
||||||
getAmount,
|
|
||||||
PeriodKey,
|
|
||||||
} from "../report.helpers";
|
|
||||||
import { ChartDataPoint } from "./HistoryChart.models";
|
|
||||||
|
|
||||||
// ─── Tab → PeriodKey ─────────────────────────────────────────
|
|
||||||
|
|
||||||
const TAB_TO_KEY: Record<string, PeriodKey> = {
|
|
||||||
Weekly: "weekly",
|
|
||||||
Monthly: "monthly",
|
|
||||||
Yearly: "yearly",
|
|
||||||
"Financial Year": "fyly",
|
|
||||||
"All Time": "full",
|
|
||||||
};
|
|
||||||
|
|
||||||
export function tabToKey(tab: string): PeriodKey {
|
|
||||||
return TAB_TO_KEY[tab] ?? "full";
|
|
||||||
}
|
|
||||||
|
|
||||||
// ─── Comparison ──────────────────────────────────────────────
|
|
||||||
|
|
||||||
function attachComparison(
|
|
||||||
points: ChartDataPoint[],
|
|
||||||
key: PeriodKey
|
|
||||||
): ChartDataPoint[] {
|
|
||||||
const getCompareIndex = (i: number) => {
|
|
||||||
if (key === "weekly") return i - 4;
|
|
||||||
if (key === "monthly") return i - 12;
|
|
||||||
if (key === "yearly") return i - 1;
|
|
||||||
if (key === "fyly") return i - 1;
|
|
||||||
return -1;
|
|
||||||
};
|
|
||||||
|
|
||||||
return points.map((p, i) => {
|
|
||||||
const ci = getCompareIndex(i);
|
|
||||||
|
|
||||||
return {
|
|
||||||
...p,
|
|
||||||
compare:
|
|
||||||
ci >= 0 && points[ci]
|
|
||||||
? {
|
|
||||||
id: points[ci].id,
|
|
||||||
label: points[ci].label,
|
|
||||||
amount: points[ci].amount,
|
|
||||||
}
|
|
||||||
: undefined,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// ─── Main adapter ────────────────────────────────────────────
|
|
||||||
|
|
||||||
export function buildChartData(
|
|
||||||
reportData: ReportData,
|
|
||||||
key: PeriodKey,
|
|
||||||
mode: "expense" | "income",
|
|
||||||
comparison: boolean
|
|
||||||
): ChartDataPoint[] {
|
|
||||||
const merged = mergeBucketPeriods(reportData.buckets, key);
|
|
||||||
|
|
||||||
let points: ChartDataPoint[] = merged.map((p) => ({
|
|
||||||
id: p.id,
|
|
||||||
label: p.label,
|
|
||||||
amount: getAmount(p, mode),
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (comparison) {
|
|
||||||
points = attachComparison(points, key);
|
|
||||||
}
|
|
||||||
|
|
||||||
return points;
|
|
||||||
}
|
|
||||||
@@ -1,13 +1,133 @@
|
|||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import { HistoryChartProps } from "./HistoryChart.models";
|
import { HistoryChartProps, ChartDataPoint } from "./HistoryChart.models";
|
||||||
import HistoryChartView from "./HistoryChart.view";
|
import HistoryChartView from "./HistoryChart.view";
|
||||||
import { buildChartData, tabToKey } from "./HistoryChart.adapter";
|
import { ReportPeriod } from "../../features/report";
|
||||||
|
|
||||||
|
type DecoratedPeriod = ReportPeriod & {
|
||||||
|
id: string;
|
||||||
|
label: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const TAB_TO_KEY: Record<string, "weekly" | "monthly" | "yearly" | "fyly" | "full"> = {
|
||||||
|
Weekly: "weekly",
|
||||||
|
Monthly: "monthly",
|
||||||
|
Yearly: "yearly",
|
||||||
|
'Financial Year': "fyly",
|
||||||
|
'All Time': "full"
|
||||||
|
};
|
||||||
|
|
||||||
|
function getAmount(p: ReportPeriod, mode: "expense" | "income") {
|
||||||
|
return mode === "expense" ? p.expenses.sum : p.incomes.sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
function mergeMetric(a: any, b: any) {
|
||||||
|
const sum = a.sum + b.sum;
|
||||||
|
const count = a.count + b.count;
|
||||||
|
|
||||||
|
return {
|
||||||
|
...a,
|
||||||
|
sum,
|
||||||
|
count,
|
||||||
|
average: count > 0 ? sum / count : 0,
|
||||||
|
transactions: a.transactions || b.transactions
|
||||||
|
? [
|
||||||
|
...(a.transactions || []),
|
||||||
|
...(b.transactions || [])
|
||||||
|
]
|
||||||
|
: undefined
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function mergeBuckets(
|
||||||
|
buckets: any[],
|
||||||
|
key: "weekly" | "monthly" | "yearly" | "fyly" | "full"
|
||||||
|
): DecoratedPeriod[] {
|
||||||
|
const map = new Map<string, DecoratedPeriod>();
|
||||||
|
|
||||||
|
for (const bucket of buckets) {
|
||||||
|
const periods = (bucket.periods[key] || []) as DecoratedPeriod[];
|
||||||
|
|
||||||
|
for (const p of periods) {
|
||||||
|
const existing = map.get(p.id);
|
||||||
|
|
||||||
|
if (!existing) {
|
||||||
|
map.set(p.id, {
|
||||||
|
...p,
|
||||||
|
expenses: { ...p.expenses },
|
||||||
|
incomes: { ...p.incomes }
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
map.set(p.id, {
|
||||||
|
...existing,
|
||||||
|
expenses: mergeMetric(existing.expenses, p.expenses),
|
||||||
|
incomes: mergeMetric(existing.incomes, p.incomes)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Array.from(map.values()).sort(
|
||||||
|
(a, b) => new Date(a.start).getTime() - new Date(b.start).getTime()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function attachComparison(
|
||||||
|
points: ChartDataPoint[],
|
||||||
|
key: "weekly" | "monthly" | "yearly" | "fyly" | "full"
|
||||||
|
): ChartDataPoint[] {
|
||||||
|
const getCompareIndex = (i: number) => {
|
||||||
|
if (key === "weekly") return i - 4;
|
||||||
|
if (key === "monthly") return i - 12;
|
||||||
|
if (key === "yearly") return i - 1;
|
||||||
|
if (key === "fyly") return i - 1;
|
||||||
|
return -1;
|
||||||
|
};
|
||||||
|
|
||||||
|
return points.map((p, i) => {
|
||||||
|
const ci = getCompareIndex(i);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...p,
|
||||||
|
compare:
|
||||||
|
ci >= 0 && points[ci]
|
||||||
|
? {
|
||||||
|
id: points[ci].id,
|
||||||
|
label: points[ci].label,
|
||||||
|
amount: points[ci].amount
|
||||||
|
}
|
||||||
|
: undefined
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildChartData(
|
||||||
|
reportData: HistoryChartProps["reportData"],
|
||||||
|
key: "weekly" | "monthly" | "yearly" | "fyly" | "full",
|
||||||
|
mode: "expense" | "income",
|
||||||
|
comparison: boolean
|
||||||
|
): ChartDataPoint[] {
|
||||||
|
const merged = mergeBuckets(reportData.buckets, key);
|
||||||
|
console.log("Merged periods:", merged);
|
||||||
|
|
||||||
|
let points: ChartDataPoint[] = merged.map((p) => ({
|
||||||
|
id: p.id,
|
||||||
|
label: p.label,
|
||||||
|
amount: getAmount(p, mode)
|
||||||
|
}));
|
||||||
|
|
||||||
|
if (comparison) {
|
||||||
|
points = attachComparison(points, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return points;
|
||||||
|
}
|
||||||
|
|
||||||
export default function HistoryChart(props: HistoryChartProps) {
|
export default function HistoryChart(props: HistoryChartProps) {
|
||||||
const {
|
const {
|
||||||
tabs,
|
tabs,
|
||||||
reportData,
|
reportData,
|
||||||
mode,
|
mode,
|
||||||
|
periodType,
|
||||||
comparison,
|
comparison,
|
||||||
selectedPeriodId,
|
selectedPeriodId,
|
||||||
setSelectedPeriodId
|
setSelectedPeriodId
|
||||||
@@ -16,7 +136,7 @@ export default function HistoryChart(props: HistoryChartProps) {
|
|||||||
const [activeTab, setActiveTab] = React.useState<string>(tabs[0] || "");
|
const [activeTab, setActiveTab] = React.useState<string>(tabs[0] || "");
|
||||||
const [startIndex, setStartIndex] = React.useState(0);
|
const [startIndex, setStartIndex] = React.useState(0);
|
||||||
|
|
||||||
const activeDataKey = tabToKey(activeTab);
|
const activeDataKey = TAB_TO_KEY[activeTab];
|
||||||
|
|
||||||
const currentData = React.useMemo(() => {
|
const currentData = React.useMemo(() => {
|
||||||
return buildChartData(reportData, activeDataKey, mode, comparison);
|
return buildChartData(reportData, activeDataKey, mode, comparison);
|
||||||
@@ -64,7 +184,7 @@ export default function HistoryChart(props: HistoryChartProps) {
|
|||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
setSelectedPeriodId(null);
|
setSelectedPeriodId(null);
|
||||||
}, [activeTab]);
|
}, [activeTab, periodType]);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (
|
if (
|
||||||
|
|||||||
@@ -58,28 +58,19 @@ export default function HistoryChartView(props: ViewProps) {
|
|||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const isDark = theme.palette.mode === "dark";
|
const isDark = theme.palette.mode === "dark";
|
||||||
|
|
||||||
const total = currentData.length;
|
|
||||||
const maxStartIndex = Math.max(total - visibleCount, 0);
|
|
||||||
const clampedStartIndex = Math.min(startIndex, maxStartIndex);
|
|
||||||
|
|
||||||
const handleTabChange = (_: React.MouseEvent<HTMLElement>, newTab: string | null) => {
|
const handleTabChange = (_: React.MouseEvent<HTMLElement>, newTab: string | null) => {
|
||||||
if (newTab !== null) setActiveTab(newTab);
|
if (newTab !== null) setActiveTab(newTab);
|
||||||
};
|
};
|
||||||
|
|
||||||
const canGoLeft = clampedStartIndex > 0;
|
const canGoLeft = startIndex > 0;
|
||||||
const canGoRight = clampedStartIndex < maxStartIndex;
|
const canGoRight = startIndex + visibleCount < currentData.length;
|
||||||
|
|
||||||
const handlePrev = () => {
|
const handlePrev = () => {
|
||||||
if (!canGoLeft) return;
|
if (canGoLeft) setStartIndex((prev) => prev - visibleCount);
|
||||||
setStartIndex((prev) => Math.max(prev - visibleCount, 0));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleNext = () => {
|
const handleNext = () => {
|
||||||
if (!canGoRight) return;
|
if (canGoRight) setStartIndex((prev) => prev + visibleCount);
|
||||||
setStartIndex((prev) => {
|
|
||||||
const next = prev + visibleCount;
|
|
||||||
return Math.min(next, maxStartIndex);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,66 +0,0 @@
|
|||||||
import { ReportData, Transaction, GroupKey } from "../../features/report";
|
|
||||||
import {
|
|
||||||
mergeBucketPeriods,
|
|
||||||
periodIdToKey,
|
|
||||||
formatCurrency,
|
|
||||||
filterBuckets,
|
|
||||||
} from "../report.helpers";
|
|
||||||
import { LatestItem } from "./LatestItems.models";
|
|
||||||
|
|
||||||
// ─── Transaction extraction ─────────────────────────────────
|
|
||||||
|
|
||||||
function extractTransactions(
|
|
||||||
reportData: ReportData,
|
|
||||||
selectedPeriodId: string | null,
|
|
||||||
selectedGroupKey: GroupKey | null,
|
|
||||||
mode: "expense" | "income"
|
|
||||||
): Transaction[] {
|
|
||||||
const buckets = filterBuckets(reportData.buckets, selectedGroupKey);
|
|
||||||
if (selectedPeriodId) {
|
|
||||||
const key = periodIdToKey(selectedPeriodId);
|
|
||||||
const periods = mergeBucketPeriods(buckets, key);
|
|
||||||
const selected = periods.find((p) => p.id === selectedPeriodId);
|
|
||||||
|
|
||||||
if (!selected) return [];
|
|
||||||
|
|
||||||
return mode === "expense"
|
|
||||||
? (selected.expenses.transactions || [])
|
|
||||||
: (selected.incomes.transactions || []);
|
|
||||||
}
|
|
||||||
|
|
||||||
const periods = mergeBucketPeriods(buckets, "full");
|
|
||||||
|
|
||||||
if (!periods.length) return [];
|
|
||||||
|
|
||||||
const full = periods[0];
|
|
||||||
|
|
||||||
return mode === "expense"
|
|
||||||
? (full.expenses.transactions || [])
|
|
||||||
: (full.incomes.transactions || []);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ─── Main adapter ────────────────────────────────────────────
|
|
||||||
|
|
||||||
export function buildLatestItems(
|
|
||||||
reportData: ReportData,
|
|
||||||
selectedPeriodId: string | null,
|
|
||||||
selectedGroupKey: GroupKey | null,
|
|
||||||
mode: "expense" | "income"
|
|
||||||
): LatestItem[] {
|
|
||||||
const txns = extractTransactions(reportData, selectedPeriodId, selectedGroupKey, mode);
|
|
||||||
|
|
||||||
return txns
|
|
||||||
.filter((t) => (mode === "expense" ? t.amount < 0 : t.amount >= 0))
|
|
||||||
.sort(
|
|
||||||
(a, b) =>
|
|
||||||
new Date(b.occurred_at).getTime() -
|
|
||||||
new Date(a.occurred_at).getTime()
|
|
||||||
)
|
|
||||||
.map((t, index) => ({
|
|
||||||
id: index + 1,
|
|
||||||
title: t.payee.name,
|
|
||||||
subtitle: t.tags.map((tag) => tag.name).join(", "),
|
|
||||||
amount: formatCurrency(t.amount),
|
|
||||||
timeAgo: new Date(t.occurred_at).toLocaleDateString("en-IN"),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
@@ -1,14 +1,18 @@
|
|||||||
|
import * as React from "react";
|
||||||
|
|
||||||
export interface LatestItem {
|
export interface LatestItem {
|
||||||
id: string | number;
|
id: string | number;
|
||||||
|
icon: React.ReactNode;
|
||||||
|
iconBgColor?: string;
|
||||||
title: string;
|
title: string;
|
||||||
subtitle: string;
|
subtitle: string;
|
||||||
amount: string;
|
amount: string;
|
||||||
timeAgo: string;
|
timeAgo: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LatestItemsViewProps {
|
export interface LatestItemsListProps {
|
||||||
|
title?: string;
|
||||||
items: LatestItem[];
|
items: LatestItem[];
|
||||||
|
onViewAll?: () => void;
|
||||||
accentColor: string;
|
accentColor: string;
|
||||||
canExpand: boolean;
|
|
||||||
onExpand: () => void;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,44 +1,112 @@
|
|||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import { ReportData, GroupKey } from "../../features/report";
|
import {
|
||||||
import { buildLatestItems } from "./LatestItems.adapter";
|
List,
|
||||||
import LatestItemsView from "./LatestItems.view";
|
ListItem,
|
||||||
|
ListItemAvatar,
|
||||||
|
ListItemText,
|
||||||
|
Avatar,
|
||||||
|
Typography,
|
||||||
|
Box,
|
||||||
|
Button,
|
||||||
|
} from "@mui/material";
|
||||||
|
|
||||||
type Props = {
|
export interface LatestItem {
|
||||||
reportData: ReportData;
|
id: string | number;
|
||||||
mode: "expense" | "income";
|
icon: React.ReactNode;
|
||||||
selectedPeriodId: string | null;
|
iconBgColor?: string;
|
||||||
selectedGroupKey?: GroupKey | null;
|
title: string;
|
||||||
accentColor: string;
|
subtitle: string;
|
||||||
};
|
amount: string;
|
||||||
|
timeAgo: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LatestItemsListProps {
|
||||||
|
title?: string;
|
||||||
|
items: LatestItem[];
|
||||||
|
onViewAll?: () => void;
|
||||||
|
accentColor: any;
|
||||||
|
}
|
||||||
|
|
||||||
export default function LatestItems({
|
export default function LatestItems({
|
||||||
reportData,
|
title = "Recent Transactions",
|
||||||
mode,
|
items,
|
||||||
selectedPeriodId,
|
onViewAll,
|
||||||
selectedGroupKey = null,
|
|
||||||
accentColor,
|
accentColor,
|
||||||
}: Props) {
|
}: LatestItemsListProps) {
|
||||||
const [visibleCount, setVisibleCount] = React.useState(5);
|
|
||||||
|
|
||||||
const allItems = React.useMemo(() => {
|
|
||||||
return buildLatestItems(reportData, selectedPeriodId, selectedGroupKey, mode);
|
|
||||||
}, [reportData, selectedPeriodId, selectedGroupKey, mode]);
|
|
||||||
|
|
||||||
const hasSelection = Boolean(selectedPeriodId) || Boolean(selectedGroupKey);
|
|
||||||
|
|
||||||
const visibleItems = React.useMemo(() => {
|
|
||||||
if (!hasSelection) return allItems.slice(0, 5);
|
|
||||||
return allItems.slice(0, visibleCount);
|
|
||||||
}, [allItems, hasSelection, visibleCount]);
|
|
||||||
|
|
||||||
const canExpand = hasSelection && visibleCount < allItems.length;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<LatestItemsView
|
<Box sx={{ width: "100%", bgcolor: "background.paper", borderRadius: 4, p: 2 }}>
|
||||||
items={visibleItems}
|
{/* Header */}
|
||||||
accentColor={accentColor}
|
<Box sx={{ display: "flex", justifyContent: "space-between", alignItems: "center", mb: 2, px: 2 }}>
|
||||||
canExpand={canExpand}
|
<Typography variant="h6" fontWeight="bold">
|
||||||
onExpand={() => setVisibleCount((prev) => prev + 5)}
|
{title}
|
||||||
|
</Typography>
|
||||||
|
{onViewAll && (
|
||||||
|
<Button
|
||||||
|
variant="text"
|
||||||
|
color="inherit"
|
||||||
|
size="small"
|
||||||
|
sx={{ textTransform: "none", color: "text.secondary", fontWeight: "medium" }}
|
||||||
|
onClick={onViewAll}
|
||||||
|
>
|
||||||
|
view all
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
|
||||||
|
{/* List */}
|
||||||
|
<List disablePadding>
|
||||||
|
{items.map((item, index) => (
|
||||||
|
<ListItem
|
||||||
|
key={item.id}
|
||||||
|
sx={{
|
||||||
|
px: { xs: 1, sm: 2 },
|
||||||
|
py: 2,
|
||||||
|
mb: index !== items.length - 1 ? 1 : 0,
|
||||||
|
borderRadius: 3,
|
||||||
|
"&:hover": { bgcolor: "action.hover" },
|
||||||
|
transition: "background-color 0.2s ease",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<ListItemAvatar>
|
||||||
|
<Avatar
|
||||||
|
variant="rounded"
|
||||||
|
sx={{
|
||||||
|
bgcolor: `${accentColor}22`,
|
||||||
|
color: "inherit",
|
||||||
|
width: 48,
|
||||||
|
height: 48,
|
||||||
|
borderRadius: 3,
|
||||||
|
mr: 2,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{item.icon}
|
||||||
|
</Avatar>
|
||||||
|
</ListItemAvatar>
|
||||||
|
|
||||||
|
<ListItemText
|
||||||
|
primary={
|
||||||
|
<Typography variant="subtitle1" fontWeight={600} color="text.primary">
|
||||||
|
{item.title}
|
||||||
|
</Typography>
|
||||||
|
}
|
||||||
|
secondary={
|
||||||
|
<Typography variant="body2" color="text.secondary">
|
||||||
|
{item.subtitle}
|
||||||
|
</Typography>
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<Box sx={{ textAlign: "right" }}>
|
||||||
|
<Typography variant="subtitle1" fontWeight={700} color="text.primary">
|
||||||
|
{item.amount}
|
||||||
|
</Typography>
|
||||||
|
<Typography variant="caption" color="text.secondary" sx={{ display: 'block', mt: 0.5 }}>
|
||||||
|
{item.timeAgo}
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
</ListItem>
|
||||||
|
))}
|
||||||
|
</List>
|
||||||
|
</Box>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,88 +1,6 @@
|
|||||||
import * as React from "react";
|
import LatestItemsListView from "./LatestItems.view";
|
||||||
import {
|
import { LatestItemsListProps } from "./LatestItems.models";
|
||||||
List,
|
|
||||||
ListItem,
|
|
||||||
ListItemAvatar,
|
|
||||||
ListItemText,
|
|
||||||
Avatar,
|
|
||||||
Typography,
|
|
||||||
Box,
|
|
||||||
IconButton,
|
|
||||||
} from "@mui/material";
|
|
||||||
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
|
|
||||||
import { LatestItemsViewProps } from "./LatestItems.models";
|
|
||||||
|
|
||||||
export default function LatestItemsView({
|
export default function LatestItemsList(props: LatestItemsListProps) {
|
||||||
items,
|
return <LatestItemsListView {...props} />;
|
||||||
accentColor,
|
|
||||||
canExpand,
|
|
||||||
onExpand,
|
|
||||||
}: LatestItemsViewProps) {
|
|
||||||
return (
|
|
||||||
<Box sx={{ width: "100%", bgcolor: "background.paper", borderRadius: 4, p: 2 }}>
|
|
||||||
<Box sx={{ mb: 2, px: 2 }}>
|
|
||||||
<Typography variant="h6" fontWeight="bold">
|
|
||||||
Recent Transactions
|
|
||||||
</Typography>
|
|
||||||
</Box>
|
|
||||||
|
|
||||||
<List disablePadding>
|
|
||||||
{items.map((item, index) => (
|
|
||||||
<ListItem
|
|
||||||
key={item.id}
|
|
||||||
sx={{
|
|
||||||
px: { xs: 1, sm: 2 },
|
|
||||||
py: 2,
|
|
||||||
mb: index !== items.length - 1 ? 1 : 0,
|
|
||||||
borderRadius: 3,
|
|
||||||
"&:hover": { bgcolor: "action.hover" },
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<ListItemAvatar>
|
|
||||||
<Avatar
|
|
||||||
variant="rounded"
|
|
||||||
sx={{
|
|
||||||
bgcolor: `${accentColor}22`,
|
|
||||||
width: 48,
|
|
||||||
height: 48,
|
|
||||||
borderRadius: 3,
|
|
||||||
mr: 2,
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</ListItemAvatar>
|
|
||||||
|
|
||||||
<ListItemText
|
|
||||||
primary={
|
|
||||||
<Typography variant="subtitle1" fontWeight={600}>
|
|
||||||
{item.title}
|
|
||||||
</Typography>
|
|
||||||
}
|
|
||||||
secondary={
|
|
||||||
<Typography variant="body2" color="text.secondary">
|
|
||||||
{item.subtitle}
|
|
||||||
</Typography>
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<Box sx={{ textAlign: "right" }}>
|
|
||||||
<Typography variant="subtitle1" fontWeight={700}>
|
|
||||||
{item.amount}
|
|
||||||
</Typography>
|
|
||||||
<Typography variant="caption" color="text.secondary">
|
|
||||||
{item.timeAgo}
|
|
||||||
</Typography>
|
|
||||||
</Box>
|
|
||||||
</ListItem>
|
|
||||||
))}
|
|
||||||
|
|
||||||
{canExpand && (
|
|
||||||
<Box sx={{ display: "flex", justifyContent: "center", mt: 2 }}>
|
|
||||||
<IconButton size="small" onClick={onExpand}>
|
|
||||||
<ExpandMoreIcon />
|
|
||||||
</IconButton>
|
|
||||||
</Box>
|
|
||||||
)}
|
|
||||||
</List>
|
|
||||||
</Box>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,4 @@ export interface ProgressCardProps {
|
|||||||
totalAmount: number;
|
totalAmount: number;
|
||||||
colorTheme?: "primary" | "secondary" | "error" | "info" | "success" | "warning";
|
colorTheme?: "primary" | "secondary" | "error" | "info" | "success" | "warning";
|
||||||
compact?: boolean;
|
compact?: boolean;
|
||||||
selected?: boolean;
|
|
||||||
onClick?: () => void;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import ProgressCardView from "./ProgressCard.view";
|
import ProgressCardView from "./ProgressCard.view";
|
||||||
import { ProgressCardProps } from "./ProgressCard.models";
|
import { ProgressCardProps } from "./ProgressCard.models";
|
||||||
import { getPercentage, formatCurrency } from "../report.helpers";
|
import { getPercentage, formatCurrency } from "./ProgressCard.utils";
|
||||||
|
|
||||||
export default function ProgressCard(props: ProgressCardProps) {
|
export default function ProgressCard(props: ProgressCardProps) {
|
||||||
const { progressAmount, totalAmount, compact = false } = props;
|
const { progressAmount, totalAmount, compact = false } = props;
|
||||||
@@ -18,8 +18,6 @@ export default function ProgressCard(props: ProgressCardProps) {
|
|||||||
formattedProgress={formattedProgress}
|
formattedProgress={formattedProgress}
|
||||||
formattedTotal={formattedTotal}
|
formattedTotal={formattedTotal}
|
||||||
compact={compact}
|
compact={compact}
|
||||||
selected={props.selected}
|
|
||||||
onClick={props.onClick}
|
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
15
src/components/ProgressCard/ProgressCard.utils.ts
Normal file
15
src/components/ProgressCard/ProgressCard.utils.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
export const getPercentage = (progressAmount: number, totalAmount: number) => {
|
||||||
|
if (!totalAmount) return 0;
|
||||||
|
return Math.min(100, Math.max(0, (progressAmount / totalAmount) * 100));
|
||||||
|
};
|
||||||
|
|
||||||
|
export const formatCurrency = (val: number) => {
|
||||||
|
const absVal = Math.abs(val);
|
||||||
|
if (absVal >= 100000) {
|
||||||
|
return `₹ ${(val / 100000).toFixed(2)}L`;
|
||||||
|
}
|
||||||
|
if (absVal >= 1000) {
|
||||||
|
return `₹ ${(val / 1000).toFixed(2)}k`;
|
||||||
|
}
|
||||||
|
return `₹ ${val.toFixed(2)}`;
|
||||||
|
};
|
||||||
@@ -14,8 +14,6 @@ interface ViewProps extends ProgressCardProps {
|
|||||||
percentage: number;
|
percentage: number;
|
||||||
formattedProgress: string;
|
formattedProgress: string;
|
||||||
formattedTotal: string;
|
formattedTotal: string;
|
||||||
selected?: boolean;
|
|
||||||
onClick?: () => void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function ProgressCardView({
|
export default function ProgressCardView({
|
||||||
@@ -25,8 +23,6 @@ export default function ProgressCardView({
|
|||||||
formattedProgress,
|
formattedProgress,
|
||||||
formattedTotal,
|
formattedTotal,
|
||||||
compact = false,
|
compact = false,
|
||||||
selected,
|
|
||||||
onClick,
|
|
||||||
}: ViewProps) {
|
}: ViewProps) {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const isDark = theme.palette.mode === "dark";
|
const isDark = theme.palette.mode === "dark";
|
||||||
@@ -34,14 +30,10 @@ export default function ProgressCardView({
|
|||||||
return (
|
return (
|
||||||
<Paper
|
<Paper
|
||||||
elevation={compact ? 2 : 4}
|
elevation={compact ? 2 : 4}
|
||||||
onClick={onClick}
|
|
||||||
sx={{
|
sx={{
|
||||||
width: "100%",
|
width: "100%",
|
||||||
p: compact ? { xs: 2.5, md: 3 } : { xs: 3, md: 4 },
|
p: compact ? { xs: 2.5, md: 3 } : { xs: 3, md: 4 },
|
||||||
borderRadius: compact ? 3 : 4,
|
borderRadius: compact ? 3 : 4,
|
||||||
cursor: onClick ? "pointer" : "default",
|
|
||||||
transform: selected ? "scale(1.02)" : "scale(1)",
|
|
||||||
transition: "transform 0.2s ease, box-shadow 0.2s ease",
|
|
||||||
background: (theme) => {
|
background: (theme) => {
|
||||||
const baseColor = theme.palette[colorTheme]?.main || theme.palette.primary.main;
|
const baseColor = theme.palette[colorTheme]?.main || theme.palette.primary.main;
|
||||||
const lightColor = theme.palette[colorTheme]?.light || theme.palette.primary.light;
|
const lightColor = theme.palette[colorTheme]?.light || theme.palette.primary.light;
|
||||||
@@ -56,19 +48,13 @@ export default function ProgressCardView({
|
|||||||
justifyContent: "center",
|
justifyContent: "center",
|
||||||
position: "relative",
|
position: "relative",
|
||||||
overflow: "hidden",
|
overflow: "hidden",
|
||||||
border: selected
|
border: isDark ? "1px solid rgba(255,255,255,0.1)" : "none",
|
||||||
? `2px solid #fff`
|
boxShadow: (theme) =>
|
||||||
: isDark ? "1px solid rgba(255,255,255,0.1)" : "none",
|
`0 ${compact ? 6 : 12}px ${compact ? 12 : 24}px -10px ${
|
||||||
boxShadow: (theme) => {
|
|
||||||
const baseShadow = `0 ${compact ? 6 : 12}px ${compact ? 12 : 24}px -10px ${
|
|
||||||
isDark
|
isDark
|
||||||
? "rgba(0,0,0,0.5)"
|
? "rgba(0,0,0,0.5)"
|
||||||
: theme.palette[colorTheme]?.main || theme.palette.primary.main
|
: theme.palette[colorTheme]?.main || theme.palette.primary.main
|
||||||
}`;
|
}`,
|
||||||
return selected
|
|
||||||
? `${baseShadow}, 0 0 0 2px ${theme.palette.background.paper}, 0 0 0 4px ${theme.palette[colorTheme]?.main || theme.palette.primary.main}`
|
|
||||||
: baseShadow;
|
|
||||||
},
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Typography
|
<Typography
|
||||||
|
|||||||
@@ -1,74 +0,0 @@
|
|||||||
import { ReportData } from "../../features/report";
|
|
||||||
import {
|
|
||||||
getAmount,
|
|
||||||
DecoratedPeriod,
|
|
||||||
} from "../report.helpers";
|
|
||||||
|
|
||||||
// ─── Helpers ─────────────────────────────────────────────────
|
|
||||||
|
|
||||||
function findPeriod(
|
|
||||||
periods: DecoratedPeriod[],
|
|
||||||
selectedPeriodId?: string | null
|
|
||||||
) {
|
|
||||||
if (!periods.length) return null;
|
|
||||||
|
|
||||||
if (selectedPeriodId) {
|
|
||||||
const match = periods.find((p) => p.id === selectedPeriodId);
|
|
||||||
if (match) return match;
|
|
||||||
}
|
|
||||||
|
|
||||||
// fallback → latest
|
|
||||||
return periods.reduce((latest, p) =>
|
|
||||||
new Date(p.start).getTime() > new Date(latest.start).getTime()
|
|
||||||
? p
|
|
||||||
: latest
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ─── Main adapter ────────────────────────────────────────────
|
|
||||||
|
|
||||||
export interface TagItem {
|
|
||||||
tag: string;
|
|
||||||
amount: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function extractTopTags(
|
|
||||||
reportData: ReportData,
|
|
||||||
mode: "expense" | "income",
|
|
||||||
selectedPeriodId?: string | null
|
|
||||||
): { items: TagItem[]; total: number } {
|
|
||||||
const tagMap = new Map<string, number>();
|
|
||||||
|
|
||||||
for (const bucket of reportData.buckets) {
|
|
||||||
const tags = bucket.group_key.tags;
|
|
||||||
if (!tags || tags.length === 0) continue;
|
|
||||||
|
|
||||||
// Prefer FULL if available
|
|
||||||
const fullPeriods = (bucket.periods.full || []) as DecoratedPeriod[];
|
|
||||||
|
|
||||||
const periodsToUse = selectedPeriodId
|
|
||||||
? (Object.values(bucket.periods).flat() as DecoratedPeriod[])
|
|
||||||
: fullPeriods;
|
|
||||||
|
|
||||||
const period = findPeriod(periodsToUse, selectedPeriodId);
|
|
||||||
if (!period) continue;
|
|
||||||
|
|
||||||
const amount = getAmount(period, mode);
|
|
||||||
|
|
||||||
for (const tag of tags) {
|
|
||||||
tagMap.set(tag, (tagMap.get(tag) || 0) + amount);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const arr = Array.from(tagMap.entries()).map(([tag, amount]) => ({
|
|
||||||
tag,
|
|
||||||
amount,
|
|
||||||
}));
|
|
||||||
|
|
||||||
arr.sort((a, b) => b.amount - a.amount);
|
|
||||||
|
|
||||||
const top = arr.slice(0, 4);
|
|
||||||
const total = top.reduce((sum, t) => sum + t.amount, 0);
|
|
||||||
|
|
||||||
return { items: top, total };
|
|
||||||
}
|
|
||||||
@@ -1,61 +1,98 @@
|
|||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import { Box } from "@mui/material";
|
import { Box } from "@mui/material";
|
||||||
import { ReportData, GroupKey } from "../../features/report";
|
import { ReportData, ReportPeriod } from "../../features/report";
|
||||||
import ProgressCard from "./ProgressCard";
|
import ProgressCard from "./ProgressCard";
|
||||||
import { extractTopTags } from "./TopTags.adapter";
|
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
reportData: ReportData;
|
reportData: ReportData;
|
||||||
mode: "expense" | "income";
|
mode: "expense" | "income";
|
||||||
selectedPeriodId?: string | null;
|
selectedPeriodId?: string | null;
|
||||||
selectedGroupKey?: GroupKey | null;
|
|
||||||
setSelectedGroupKey?: (key: GroupKey | null) => void;
|
|
||||||
compact?: boolean;
|
compact?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type DecoratedPeriod = ReportPeriod & {
|
||||||
|
id: string;
|
||||||
|
label: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
function getAmount(p: ReportPeriod, mode: "expense" | "income") {
|
||||||
|
return mode === "expense" ? p.expenses.sum : p.incomes.sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
function findPeriod(
|
||||||
|
periods: DecoratedPeriod[],
|
||||||
|
selectedPeriodId?: string | null
|
||||||
|
) {
|
||||||
|
if (!periods.length) return null;
|
||||||
|
|
||||||
|
if (selectedPeriodId) {
|
||||||
|
const match = periods.find((p) => p.id === selectedPeriodId);
|
||||||
|
if (match) return match;
|
||||||
|
}
|
||||||
|
|
||||||
|
// fallback → latest
|
||||||
|
return periods.reduce((latest, p) =>
|
||||||
|
new Date(p.start).getTime() > new Date(latest.start).getTime()
|
||||||
|
? p
|
||||||
|
: latest
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export default function TopTags({
|
export default function TopTags({
|
||||||
reportData,
|
reportData,
|
||||||
mode,
|
mode,
|
||||||
selectedPeriodId,
|
selectedPeriodId,
|
||||||
selectedGroupKey,
|
compact = true
|
||||||
setSelectedGroupKey,
|
|
||||||
compact = true,
|
|
||||||
}: Props) {
|
}: Props) {
|
||||||
const { items, total } = React.useMemo(() => {
|
const { items, total } = React.useMemo(() => {
|
||||||
return extractTopTags(reportData, mode, selectedPeriodId);
|
const tagMap = new Map<string, number>();
|
||||||
|
|
||||||
|
for (const bucket of reportData.buckets) {
|
||||||
|
const tags = bucket.group_key.tags;
|
||||||
|
if (!tags || tags.length === 0) continue;
|
||||||
|
|
||||||
|
// Prefer FULL if available
|
||||||
|
const fullPeriods = (bucket.periods.full || []) as DecoratedPeriod[];
|
||||||
|
|
||||||
|
const periodsToUse =
|
||||||
|
selectedPeriodId
|
||||||
|
? Object.values(bucket.periods).flat() as DecoratedPeriod[]
|
||||||
|
: fullPeriods;
|
||||||
|
|
||||||
|
const period = findPeriod(periodsToUse, selectedPeriodId);
|
||||||
|
if (!period) continue;
|
||||||
|
|
||||||
|
const amount = getAmount(period, mode);
|
||||||
|
|
||||||
|
for (const tag of tags) {
|
||||||
|
tagMap.set(tag, (tagMap.get(tag) || 0) + amount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const arr = Array.from(tagMap.entries()).map(([tag, amount]) => ({
|
||||||
|
tag,
|
||||||
|
amount
|
||||||
|
}));
|
||||||
|
|
||||||
|
arr.sort((a, b) => b.amount - a.amount);
|
||||||
|
|
||||||
|
const top = arr.slice(0, 5);
|
||||||
|
const total = top.reduce((sum, t) => sum + t.amount, 0);
|
||||||
|
|
||||||
|
return { items: top, total };
|
||||||
}, [reportData, mode, selectedPeriodId]);
|
}, [reportData, mode, selectedPeriodId]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box sx={{ display: "grid", gap: 2 }}>
|
||||||
sx={{
|
{items.map((item) => (
|
||||||
display: "grid",
|
|
||||||
gridTemplateColumns: {
|
|
||||||
xs: "1fr",
|
|
||||||
sm: "repeat(2, 1fr)",
|
|
||||||
md: "repeat(4, 1fr)",
|
|
||||||
},
|
|
||||||
gap: 2,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{items.map((item) => {
|
|
||||||
const isSelected = selectedGroupKey?.tags?.includes(item.tag);
|
|
||||||
return (
|
|
||||||
<ProgressCard
|
<ProgressCard
|
||||||
key={item.tag}
|
key={item.tag}
|
||||||
header={item.tag}
|
header={item.tag}
|
||||||
progressAmount={item.amount}
|
progressAmount={item.amount}
|
||||||
totalAmount={total}
|
totalAmount={total}
|
||||||
compact={compact}
|
compact={compact}
|
||||||
colorTheme={mode === "expense" ? "error" : "success"}
|
|
||||||
selected={isSelected}
|
|
||||||
onClick={() => {
|
|
||||||
if (setSelectedGroupKey) {
|
|
||||||
setSelectedGroupKey(isSelected ? null : { tags: [item.tag] });
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
);
|
))}
|
||||||
})}
|
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,147 +0,0 @@
|
|||||||
import {
|
|
||||||
ReportPeriod,
|
|
||||||
ReportBucket,
|
|
||||||
GroupKey,
|
|
||||||
} from "../features/report";
|
|
||||||
|
|
||||||
// ─── Types ────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
export type PeriodKey = "weekly" | "monthly" | "yearly" | "fyly" | "full";
|
|
||||||
|
|
||||||
export type DecoratedPeriod = ReportPeriod & {
|
|
||||||
id: string;
|
|
||||||
label: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
// ─── Period helpers ───────────────────────────────────────────
|
|
||||||
|
|
||||||
const PREFIX_TO_KEY: Record<string, PeriodKey> = {
|
|
||||||
W: "weekly",
|
|
||||||
M: "monthly",
|
|
||||||
Y: "yearly",
|
|
||||||
FY: "fyly",
|
|
||||||
FULL: "full",
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Derive the period key from a decorated-period id.
|
|
||||||
* E.g. `"W:2026-04-28_2026-05-04"` → `"weekly"`
|
|
||||||
*/
|
|
||||||
export function periodIdToKey(periodId: string): PeriodKey {
|
|
||||||
const prefix = periodId.split(":")[0];
|
|
||||||
return PREFIX_TO_KEY[prefix] ?? "full";
|
|
||||||
}
|
|
||||||
|
|
||||||
// ─── Metric helpers ───────────────────────────────────────────
|
|
||||||
|
|
||||||
export function getAmount(
|
|
||||||
period: ReportPeriod,
|
|
||||||
mode: "expense" | "income"
|
|
||||||
): number {
|
|
||||||
return mode === "expense" ? period.expenses.sum : period.incomes.sum;
|
|
||||||
}
|
|
||||||
|
|
||||||
function mergeMetric(a: ReportPeriod["expenses"], b: ReportPeriod["expenses"]) {
|
|
||||||
const sum = a.sum + b.sum;
|
|
||||||
const count = a.count + b.count;
|
|
||||||
|
|
||||||
return {
|
|
||||||
...a,
|
|
||||||
sum,
|
|
||||||
count,
|
|
||||||
average: count > 0 ? sum / count : 0,
|
|
||||||
transactions:
|
|
||||||
a.transactions || b.transactions
|
|
||||||
? [...(a.transactions || []), ...(b.transactions || [])]
|
|
||||||
: undefined,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Merge periods with the same id across all buckets, summing
|
|
||||||
* their metrics and concatenating transactions.
|
|
||||||
*
|
|
||||||
* Returns sorted by start date ascending.
|
|
||||||
*/
|
|
||||||
export function mergeBucketPeriods(
|
|
||||||
buckets: ReportBucket[],
|
|
||||||
key: PeriodKey
|
|
||||||
): DecoratedPeriod[] {
|
|
||||||
const map = new Map<string, DecoratedPeriod>();
|
|
||||||
|
|
||||||
for (const bucket of buckets) {
|
|
||||||
const periods = (bucket.periods[key] || []) as DecoratedPeriod[];
|
|
||||||
|
|
||||||
for (const p of periods) {
|
|
||||||
const existing = map.get(p.id);
|
|
||||||
|
|
||||||
if (!existing) {
|
|
||||||
map.set(p.id, {
|
|
||||||
...p,
|
|
||||||
expenses: { ...p.expenses },
|
|
||||||
incomes: { ...p.incomes },
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
map.set(p.id, {
|
|
||||||
...existing,
|
|
||||||
expenses: mergeMetric(existing.expenses, p.expenses),
|
|
||||||
incomes: mergeMetric(existing.incomes, p.incomes),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Array.from(map.values()).sort(
|
|
||||||
(a, b) => new Date(a.start).getTime() - new Date(b.start).getTime()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ─── Formatting ───────────────────────────────────────────────
|
|
||||||
|
|
||||||
export const formatCurrency = (val: number) => {
|
|
||||||
const absVal = Math.abs(val);
|
|
||||||
if (absVal >= 100000) {
|
|
||||||
return `₹ ${(val / 100000).toFixed(2)}L`;
|
|
||||||
}
|
|
||||||
if (absVal >= 1000) {
|
|
||||||
return `₹ ${(val / 1000).toFixed(2)}k`;
|
|
||||||
}
|
|
||||||
return `₹ ${val.toFixed(2)}`;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getPercentage = (progressAmount: number, totalAmount: number) => {
|
|
||||||
if (!totalAmount) return 0;
|
|
||||||
return Math.min(100, Math.max(0, (progressAmount / totalAmount) * 100));
|
|
||||||
};
|
|
||||||
|
|
||||||
// ─── Group filtering ──────────────────────────────────────────
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if a bucket's group_key matches the selected GroupKey.
|
|
||||||
* Every dimension present in `selected` must exist in the bucket
|
|
||||||
* and contain all the selected values.
|
|
||||||
*/
|
|
||||||
export function matchesGroupKey(
|
|
||||||
bucket: ReportBucket,
|
|
||||||
selected: GroupKey
|
|
||||||
): boolean {
|
|
||||||
for (const [dim, values] of Object.entries(selected)) {
|
|
||||||
const bucketValues = bucket.group_key[dim as keyof GroupKey];
|
|
||||||
if (!bucketValues) return false;
|
|
||||||
if (!(values as string[]).every((v) => bucketValues.includes(v)))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return only buckets matching the selected group key,
|
|
||||||
* or all buckets if no selection.
|
|
||||||
*/
|
|
||||||
export function filterBuckets(
|
|
||||||
buckets: ReportBucket[],
|
|
||||||
selectedGroupKey: GroupKey | null
|
|
||||||
): ReportBucket[] {
|
|
||||||
if (!selectedGroupKey) return buckets;
|
|
||||||
return buckets.filter((b) => matchesGroupKey(b, selectedGroupKey));
|
|
||||||
}
|
|
||||||
@@ -19,8 +19,8 @@ export const configuration: DashboardConfig = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "top-categories",
|
id: "top-payees",
|
||||||
title: 'Top Categories',
|
title: 'Top Payees',
|
||||||
component: TopTags,
|
component: TopTags,
|
||||||
settings: {
|
settings: {
|
||||||
compact: true,
|
compact: true,
|
||||||
@@ -29,13 +29,15 @@ export const configuration: DashboardConfig = {
|
|||||||
size: 12,
|
size: 12,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
// {
|
||||||
id: "items",
|
// id: "latest",
|
||||||
component: LatestItems,
|
// title: 'Recent Transactions',
|
||||||
style: {
|
// component: LatestItems,
|
||||||
size: 12,
|
// dataKey: "latest",
|
||||||
},
|
// style: {
|
||||||
},
|
// size: 12,
|
||||||
|
// },
|
||||||
|
// },
|
||||||
],
|
],
|
||||||
style: {
|
style: {
|
||||||
palette: {
|
palette: {
|
||||||
|
|||||||
@@ -4,9 +4,7 @@ export {
|
|||||||
export type {
|
export type {
|
||||||
Transaction,
|
Transaction,
|
||||||
ReportData,
|
ReportData,
|
||||||
ReportBucket,
|
|
||||||
ReportPeriod,
|
ReportPeriod,
|
||||||
GroupKey,
|
|
||||||
} from './report.models'
|
} from './report.models'
|
||||||
export {
|
export {
|
||||||
prepareReport
|
prepareReport
|
||||||
|
|||||||
@@ -73,14 +73,20 @@ function buildLabel(
|
|||||||
end: Date
|
end: Date
|
||||||
): string {
|
): string {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "weekly": {
|
case "weekly":
|
||||||
|
if (sameMonth(start, end)) {
|
||||||
const sDay = start.getUTCDate();
|
const sDay = start.getUTCDate();
|
||||||
|
const eDay = end.getUTCDate();
|
||||||
const m = monthFmt.format(start);
|
const m = monthFmt.format(start);
|
||||||
return `${sDay} ${m}`;
|
return `${sDay} ${m} - ${eDay} ${m}`;
|
||||||
}
|
}
|
||||||
|
return `${dayFmt.format(start)} - ${dayFmt.format(end)}`;
|
||||||
|
|
||||||
case "monthly":
|
case "monthly":
|
||||||
|
if (sameMonth(start, end)) {
|
||||||
return `${monthFmt.format(start)} ${yearFmt.format(start)}`;
|
return `${monthFmt.format(start)} ${yearFmt.format(start)}`;
|
||||||
|
}
|
||||||
|
return `${monthDayFmt.format(start)} - ${monthDayFmt.format(end)}`;
|
||||||
|
|
||||||
case "yearly":
|
case "yearly":
|
||||||
return yearFmt.format(start);
|
return yearFmt.format(start);
|
||||||
@@ -104,8 +110,8 @@ function decoratePeriods(
|
|||||||
): (ReportPeriod & { id: string; label: string })[] {
|
): (ReportPeriod & { id: string; label: string })[] {
|
||||||
return periods.map((p) => ({
|
return periods.map((p) => ({
|
||||||
...p,
|
...p,
|
||||||
id: buildPeriodId(type, new Date(p.start + "Z"), new Date(p.end + "Z")),
|
id: buildPeriodId(type, new Date(p.start), new Date(p.end)),
|
||||||
label: buildLabel(type, new Date(p.start + "Z"), new Date(p.end + "Z")),
|
label: buildLabel(type, new Date(p.start), new Date(p.end)),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user