Compare commits
44 Commits
0.1.0
...
21a4a95f39
| Author | SHA1 | Date | |
|---|---|---|---|
| 21a4a95f39 | |||
| a62c10de7d | |||
| 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,4 +1,4 @@
|
|||||||
import { useQuery, useMutation, useQueryClient, keepPreviousData } from "@tanstack/react-query";
|
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||||
import { api } from "../api/client";
|
import { api } from "../api/client";
|
||||||
import { ResourceConfig } from "../types/config";
|
import { ResourceConfig } from "../types/config";
|
||||||
import { ConfigContext } from "../providers/ConfigContext";
|
import { ConfigContext } from "../providers/ConfigContext";
|
||||||
@@ -26,7 +26,6 @@ export function useResource<T = any>(config: ResourceConfig | undefined) {
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
enabled: !!endpoint,
|
enabled: !!endpoint,
|
||||||
placeholderData: keepPreviousData,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// --- READ ONE ---
|
// --- READ ONE ---
|
||||||
|
|||||||
@@ -3,15 +3,10 @@ import {
|
|||||||
Box,
|
Box,
|
||||||
Container,
|
Container,
|
||||||
CircularProgress,
|
CircularProgress,
|
||||||
Alert,
|
Alert
|
||||||
TextField,
|
|
||||||
Paper,
|
|
||||||
Autocomplete,
|
|
||||||
Button
|
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
|
|
||||||
import ConfigurableDashboard from "./components/Dashboard";
|
import ConfigurableDashboard from "./components/Dashboard";
|
||||||
import { DashboardState } from "./components/Dashboard";
|
|
||||||
import { configuration } from "./dashboard-config";
|
import { configuration } from "./dashboard-config";
|
||||||
import {
|
import {
|
||||||
useReport,
|
useReport,
|
||||||
@@ -19,65 +14,18 @@ import {
|
|||||||
} from "./features/report";
|
} from "./features/report";
|
||||||
|
|
||||||
export default function Dashboard() {
|
export default function Dashboard() {
|
||||||
const [flow, setFlow] = React.useState<"outflows" | "inflows">("outflows");
|
|
||||||
|
|
||||||
const [appliedPayees, setAppliedPayees] = React.useState<string[]>([]);
|
|
||||||
const [appliedTags, setAppliedTags] = React.useState<string[]>([]);
|
|
||||||
|
|
||||||
const [payeeInput, setPayeeInput] = React.useState<string[]>([]);
|
|
||||||
const [tagsInput, setTagsInput] = React.useState<string[]>([]);
|
|
||||||
|
|
||||||
const [loadedPayees, setLoadedPayees] = React.useState<string[]>([]);
|
|
||||||
const [loadedTags, setLoadedTags] = React.useState<string[]>([]);
|
|
||||||
|
|
||||||
const report = useReport({
|
const report = useReport({
|
||||||
periods: ["daily", "weekly", "monthly", "all"],
|
periods: ["weekly", "monthly", "full"],
|
||||||
flow: flow,
|
rolling: true,
|
||||||
payee: appliedPayees.length > 0 ? appliedPayees : undefined,
|
include_transactions: true,
|
||||||
tags: appliedTags.length > 0 ? appliedTags : undefined,
|
group_by: ["tags"],
|
||||||
});
|
})
|
||||||
|
|
||||||
React.useEffect(() => {
|
|
||||||
if (report.data?.data) {
|
|
||||||
setLoadedPayees(prev => {
|
|
||||||
const pSet = new Set<string>(prev);
|
|
||||||
report.data.data.buckets.forEach((b: any) => {
|
|
||||||
Object.values(b.periods).forEach((periodArray: any) => {
|
|
||||||
periodArray?.forEach((p: any) => {
|
|
||||||
p.metric?.transactions?.forEach((t: any) => {
|
|
||||||
if (t.payee?.name) pSet.add(t.payee.name);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
return Array.from(pSet).sort();
|
|
||||||
});
|
|
||||||
|
|
||||||
setLoadedTags(prev => {
|
|
||||||
const tSet = new Set<string>(prev);
|
|
||||||
report.data.data.buckets.forEach((b: any) => {
|
|
||||||
Object.values(b.periods).forEach((periodArray: any) => {
|
|
||||||
periodArray?.forEach((p: any) => {
|
|
||||||
p.metric?.transactions?.forEach((t: any) => {
|
|
||||||
t.tags?.forEach((tag: any) => tSet.add(tag.name || tag));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
return Array.from(tSet).sort();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, [report.data?.data]);
|
|
||||||
|
|
||||||
const isLoading = report.isLoading;
|
const isLoading = report.isLoading;
|
||||||
const error = report.error;
|
const error = report.error;
|
||||||
|
|
||||||
/** Callback for the ConfigurableDashboard's flow toggle */
|
|
||||||
const handleFlowChange = React.useCallback((newState: DashboardState) => {
|
|
||||||
setFlow(newState.flow);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
if (isLoading && !report.data) {
|
if (isLoading) {
|
||||||
return (
|
return (
|
||||||
<Box sx={{ display: "flex", justifyContent: "center", alignItems: "center", height: "60vh" }}>
|
<Box sx={{ display: "flex", justifyContent: "center", alignItems: "center", height: "60vh" }}>
|
||||||
<CircularProgress />
|
<CircularProgress />
|
||||||
@@ -93,76 +41,15 @@ export default function Dashboard() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!report.data) {
|
if (!report) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = prepareReport(report.data.data);
|
const data = prepareReport(report.data?.data);
|
||||||
return (
|
return (
|
||||||
<Box>
|
<ConfigurableDashboard
|
||||||
<Container>
|
config={configuration}
|
||||||
<Paper
|
data={data}
|
||||||
sx={{
|
/>
|
||||||
mt: 4,
|
|
||||||
p: 2,
|
|
||||||
display: "flex",
|
|
||||||
flexDirection: { xs: "column", sm: "row" },
|
|
||||||
gap: 2,
|
|
||||||
alignItems: { xs: "stretch", sm: "flex-end" },
|
|
||||||
borderRadius: 4,
|
|
||||||
mb: -2 // pull up to be closer to the dashboard container below
|
|
||||||
}}
|
|
||||||
elevation={0}
|
|
||||||
variant="outlined"
|
|
||||||
>
|
|
||||||
<Box sx={{ display: 'flex', flexDirection: 'column', flex: 1, minWidth: { sm: 250 } }}>
|
|
||||||
<Box sx={{ typography: 'caption', mb: 1, color: 'text.secondary' }}>
|
|
||||||
Filter by Payee
|
|
||||||
</Box>
|
|
||||||
<Autocomplete
|
|
||||||
multiple
|
|
||||||
freeSolo
|
|
||||||
options={loadedPayees}
|
|
||||||
value={payeeInput}
|
|
||||||
onChange={(_, val) => setPayeeInput(val as string[])}
|
|
||||||
renderInput={(params) => <TextField {...params} placeholder="Add payees..." />}
|
|
||||||
sx={{ '& .MuiOutlinedInput-root': { height: 'auto', minHeight: '2.5rem', py: 0.5 } }}
|
|
||||||
/>
|
|
||||||
</Box>
|
|
||||||
<Box sx={{ display: 'flex', flexDirection: 'column', flex: 1, minWidth: { sm: 250 } }}>
|
|
||||||
<Box sx={{ typography: 'caption', mb: 1, color: 'text.secondary' }}>
|
|
||||||
Filter by Tags
|
|
||||||
</Box>
|
|
||||||
<Autocomplete
|
|
||||||
multiple
|
|
||||||
freeSolo
|
|
||||||
options={loadedTags}
|
|
||||||
value={tagsInput}
|
|
||||||
onChange={(_, val) => setTagsInput(val as string[])}
|
|
||||||
renderInput={(params) => <TextField {...params} placeholder="Add tags..." />}
|
|
||||||
sx={{ '& .MuiOutlinedInput-root': { height: 'auto', minHeight: '2.5rem', py: 0.5 } }}
|
|
||||||
/>
|
|
||||||
</Box>
|
|
||||||
<Button
|
|
||||||
variant="contained"
|
|
||||||
size="large"
|
|
||||||
onClick={() => {
|
|
||||||
setAppliedPayees(payeeInput);
|
|
||||||
setAppliedTags(tagsInput);
|
|
||||||
}}
|
|
||||||
disabled={isLoading}
|
|
||||||
sx={{ height: 40, borderRadius: 2 }}
|
|
||||||
>
|
|
||||||
Apply
|
|
||||||
</Button>
|
|
||||||
</Paper>
|
|
||||||
</Container>
|
|
||||||
<ConfigurableDashboard
|
|
||||||
config={configuration}
|
|
||||||
data={data}
|
|
||||||
isFetching={report.isFetching}
|
|
||||||
onFlowChange={handleFlowChange}
|
|
||||||
/>
|
|
||||||
</Box>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,16 @@
|
|||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import {
|
import {
|
||||||
ReportData,
|
ReportData
|
||||||
GroupKey,
|
|
||||||
} from "../../features/report";
|
} from "../../features/report";
|
||||||
|
|
||||||
export type DashboardFlow = "outflows" | "inflows";
|
export type DashboardMode = "expense" | "income";
|
||||||
export type DashboardPeriodType = "rolling" | "calendar";
|
export type DashboardPeriodType = "rolling" | "calendar";
|
||||||
export type DashboardSelectedPeriodId = string | null;
|
export type DashboardSelectedPeriodId = string | null;
|
||||||
|
|
||||||
export interface DashboardState {
|
export interface DashboardState {
|
||||||
flow: DashboardFlow;
|
mode: DashboardMode;
|
||||||
periodType: DashboardPeriodType;
|
periodType: DashboardPeriodType;
|
||||||
selectedPeriodId: DashboardSelectedPeriodId;
|
selectedPeriodId: DashboardSelectedPeriodId;
|
||||||
selectedGroupKey: GroupKey | null;
|
|
||||||
comparison: boolean;
|
comparison: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,13 +41,11 @@ export interface ThemeAwarePalette {
|
|||||||
export interface DashboardConfig {
|
export interface DashboardConfig {
|
||||||
sections: DashboardSection[];
|
sections: DashboardSection[];
|
||||||
style?: {
|
style?: {
|
||||||
palette?: Record<DashboardFlow, ThemeAwarePalette>;
|
palette?: Record<DashboardMode, ThemeAwarePalette>;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DashboardProps {
|
export interface DashboardProps {
|
||||||
config: DashboardConfig;
|
config: DashboardConfig;
|
||||||
data: ReportData;
|
data: ReportData;
|
||||||
isFetching?: boolean;
|
|
||||||
onFlowChange?: (state: DashboardState) => void;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,27 +4,17 @@ import { DashboardProps, DashboardState } from "./Dashboard.models";
|
|||||||
|
|
||||||
export default function Dashboard(props: DashboardProps) {
|
export default function Dashboard(props: DashboardProps) {
|
||||||
const [state, setState] = React.useState<DashboardState>({
|
const [state, setState] = React.useState<DashboardState>({
|
||||||
flow: "outflows",
|
mode: "expense",
|
||||||
periodType: "rolling",
|
periodType: "rolling",
|
||||||
selectedPeriodId: null,
|
selectedPeriodId: null,
|
||||||
selectedGroupKey: null,
|
|
||||||
comparison: false,
|
comparison: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
const toggleFlow = (
|
const toggleMode = () => {
|
||||||
event: React.MouseEvent<HTMLElement>,
|
setState(prev => ({
|
||||||
newFlow: "outflows" | "inflows" | null
|
...prev,
|
||||||
) => {
|
mode: prev.mode === "expense" ? "income" : "expense",
|
||||||
if (newFlow === null) return;
|
}));
|
||||||
|
|
||||||
setState(prev => {
|
|
||||||
if (prev.flow === newFlow) return prev;
|
|
||||||
|
|
||||||
const next = { ...prev, flow: newFlow };
|
|
||||||
props.onFlowChange?.(next);
|
|
||||||
|
|
||||||
return next;
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const togglePeriodType = () => {
|
const togglePeriodType = () => {
|
||||||
@@ -45,20 +35,15 @@ 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}
|
||||||
state={state}
|
state={state}
|
||||||
setState={setState}
|
setState={setState}
|
||||||
toggleFlow={toggleFlow}
|
toggleMode={toggleMode}
|
||||||
togglePeriodType={togglePeriodType}
|
togglePeriodType={togglePeriodType}
|
||||||
toggleComparison={toggleComparison}
|
toggleComparison={toggleComparison}
|
||||||
setSelectedPeriodId={setSelectedPeriodId}
|
setSelectedPeriodId={setSelectedPeriodId}
|
||||||
setSelectedGroupKey={setSelectedGroupKey}
|
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,20 +5,17 @@ import {
|
|||||||
Grid,
|
Grid,
|
||||||
Typography,
|
Typography,
|
||||||
ToggleButton,
|
ToggleButton,
|
||||||
ToggleButtonGroup,
|
ToggleButtonGroup
|
||||||
Button
|
|
||||||
} 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 {
|
||||||
state: DashboardState;
|
state: DashboardState;
|
||||||
setState: React.Dispatch<React.SetStateAction<DashboardState>>;
|
setState: React.Dispatch<React.SetStateAction<DashboardState>>;
|
||||||
toggleFlow: (event: React.MouseEvent<HTMLElement>, newFlow: "outflows" | "inflows" | null) => 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,19 +24,18 @@ export default function DashboardView({
|
|||||||
data,
|
data,
|
||||||
state,
|
state,
|
||||||
setState,
|
setState,
|
||||||
toggleFlow,
|
toggleMode,
|
||||||
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 { flow, 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(() => {
|
||||||
const palette = config.style?.palette?.[flow];
|
const palette = config.style?.palette?.[mode];
|
||||||
const modeColors = palette ? palette[themeMode] : null;
|
const modeColors = palette ? palette[themeMode] : null;
|
||||||
|
|
||||||
if (modeColors) {
|
if (modeColors) {
|
||||||
@@ -51,13 +47,13 @@ export default function DashboardView({
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to standard theme colors
|
// Fallback to standard theme colors
|
||||||
const themeColor = flow === 'outflows' ? theme.palette.error : theme.palette.success;
|
const themeColor = mode === 'expense' ? theme.palette.error : theme.palette.success;
|
||||||
return {
|
return {
|
||||||
primary: themeColor.main,
|
primary: themeColor.main,
|
||||||
light: alpha(themeColor.main, themeMode === 'light' ? 0.08 : 0.15),
|
light: alpha(themeColor.main, themeMode === 'light' ? 0.08 : 0.15),
|
||||||
text: themeColor.main
|
text: themeColor.main
|
||||||
};
|
};
|
||||||
}, [config.style?.palette, flow, themeMode, theme.palette]);
|
}, [config.style?.palette, mode, themeMode, theme.palette]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container
|
<Container
|
||||||
@@ -70,11 +66,11 @@ export default function DashboardView({
|
|||||||
transition: 'background 0.3s ease'
|
transition: 'background 0.3s ease'
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Box sx={{ display: "flex", flexDirection: "column", alignItems: "center", mb: 3 }}>
|
<Box sx={{ display: "flex", justifyContent: "center", mb: 3 }}>
|
||||||
<ToggleButtonGroup
|
<ToggleButtonGroup
|
||||||
value={flow}
|
value={mode}
|
||||||
exclusive
|
exclusive
|
||||||
onChange={toggleFlow}
|
onChange={toggleMode}
|
||||||
sx={{
|
sx={{
|
||||||
borderRadius: 3,
|
borderRadius: 3,
|
||||||
overflow: "hidden",
|
overflow: "hidden",
|
||||||
@@ -90,19 +86,9 @@ export default function DashboardView({
|
|||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<ToggleButton value="outflows">Outflows</ToggleButton>
|
<ToggleButton value="expense">Expenses</ToggleButton>
|
||||||
<ToggleButton value="inflows">Inflows</ToggleButton>
|
<ToggleButton value="income">Income</ToggleButton>
|
||||||
</ToggleButtonGroup>
|
</ToggleButtonGroup>
|
||||||
|
|
||||||
{selectedGroupKey && Object.keys(selectedGroupKey).length > 0 && (
|
|
||||||
<Button
|
|
||||||
size="small"
|
|
||||||
sx={{ mt: 1, textTransform: "none" }}
|
|
||||||
onClick={() => setSelectedGroupKey(null)}
|
|
||||||
>
|
|
||||||
Clear Drill-down
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
<Grid container spacing={4}>
|
<Grid container spacing={4}>
|
||||||
@@ -111,6 +97,14 @@ export default function DashboardView({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Grid key={section.id} size={section.style?.size || 12 as any}>
|
<Grid key={section.id} size={section.style?.size || 12 as any}>
|
||||||
|
{section.title && !section.isList && (
|
||||||
|
<Box sx={{ mb: 2 }}>
|
||||||
|
<Typography variant="h6" fontWeight={700}>
|
||||||
|
{section.title}
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
|
||||||
<Component
|
<Component
|
||||||
{...section.settings}
|
{...section.settings}
|
||||||
header={section.title}
|
header={section.title}
|
||||||
@@ -121,18 +115,15 @@ export default function DashboardView({
|
|||||||
colorScheme={colors}
|
colorScheme={colors}
|
||||||
|
|
||||||
// State management
|
// State management
|
||||||
flow={flow}
|
mode={mode}
|
||||||
|
|
||||||
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}
|
|
||||||
isFetching={arguments[0].isFetching}
|
|
||||||
/>
|
/>
|
||||||
</Grid>
|
</Grid>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,73 +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> = {
|
|
||||||
Daily: "daily",
|
|
||||||
Weekly: "weekly",
|
|
||||||
Monthly: "monthly",
|
|
||||||
"All Time": "all",
|
|
||||||
};
|
|
||||||
|
|
||||||
export function tabToKey(tab: string): PeriodKey {
|
|
||||||
return TAB_TO_KEY[tab] ?? "all";
|
|
||||||
}
|
|
||||||
|
|
||||||
// ─── Comparison ──────────────────────────────────────────────
|
|
||||||
|
|
||||||
function attachComparison(
|
|
||||||
points: ChartDataPoint[],
|
|
||||||
key: PeriodKey
|
|
||||||
): ChartDataPoint[] {
|
|
||||||
const getCompareIndex = (i: number) => {
|
|
||||||
if (key === "daily") return i - 7;
|
|
||||||
if (key === "weekly") return i - 4;
|
|
||||||
if (key === "monthly") return i - 12;
|
|
||||||
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,
|
|
||||||
flow: "outflows" | "inflows",
|
|
||||||
comparison: boolean
|
|
||||||
): ChartDataPoint[] {
|
|
||||||
const merged = mergeBucketPeriods(reportData.buckets, key);
|
|
||||||
|
|
||||||
let points: ChartDataPoint[] = merged.map((p) => ({
|
|
||||||
id: p.id,
|
|
||||||
label: p.label,
|
|
||||||
amount: getAmount(p),
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (comparison) {
|
|
||||||
points = attachComparison(points, key);
|
|
||||||
}
|
|
||||||
|
|
||||||
return points;
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
DashboardFlow,
|
DashboardMode,
|
||||||
DashboardPeriodType,
|
DashboardPeriodType,
|
||||||
DashboardSelectedPeriodId
|
DashboardSelectedPeriodId
|
||||||
} from "../Dashboard";
|
} from "../Dashboard";
|
||||||
@@ -29,7 +29,7 @@ export interface HistoryChartProps {
|
|||||||
text: string;
|
text: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
flow: DashboardFlow;
|
mode: DashboardMode;
|
||||||
periodType: DashboardPeriodType;
|
periodType: DashboardPeriodType;
|
||||||
selectedPeriodId: DashboardSelectedPeriodId;
|
selectedPeriodId: DashboardSelectedPeriodId;
|
||||||
comparison: boolean;
|
comparison: boolean;
|
||||||
@@ -37,6 +37,4 @@ export interface HistoryChartProps {
|
|||||||
togglePeriodType: () => void;
|
togglePeriodType: () => void;
|
||||||
setSelectedPeriodId: (id: string | null) => void;
|
setSelectedPeriodId: (id: string | null) => void;
|
||||||
toggleComparison: () => void;
|
toggleComparison: () => void;
|
||||||
|
|
||||||
isFetching?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,
|
||||||
flow,
|
mode,
|
||||||
|
periodType,
|
||||||
comparison,
|
comparison,
|
||||||
selectedPeriodId,
|
selectedPeriodId,
|
||||||
setSelectedPeriodId
|
setSelectedPeriodId
|
||||||
@@ -16,11 +136,11 @@ 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, flow, comparison);
|
return buildChartData(reportData, activeDataKey, mode, comparison);
|
||||||
}, [reportData, activeDataKey, flow, comparison]);
|
}, [reportData, activeDataKey, mode, comparison]);
|
||||||
|
|
||||||
const maxAmount =
|
const maxAmount =
|
||||||
currentData.length > 0
|
currentData.length > 0
|
||||||
@@ -35,10 +155,11 @@ export default function HistoryChart(props: HistoryChartProps) {
|
|||||||
: 1;
|
: 1;
|
||||||
|
|
||||||
const visibleCountMap = {
|
const visibleCountMap = {
|
||||||
daily: 7,
|
|
||||||
weekly: 6,
|
weekly: 6,
|
||||||
monthly: 4,
|
monthly: 4,
|
||||||
all: 4,
|
yearly: 4,
|
||||||
|
fyly: 4,
|
||||||
|
full: 4,
|
||||||
};
|
};
|
||||||
|
|
||||||
const visibleCount = visibleCountMap[activeDataKey] ?? 4;
|
const visibleCount = visibleCountMap[activeDataKey] ?? 4;
|
||||||
@@ -63,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 (
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ export default function HistoryChartView(props: ViewProps) {
|
|||||||
tabs,
|
tabs,
|
||||||
colorScheme,
|
colorScheme,
|
||||||
|
|
||||||
flow,
|
mode,
|
||||||
periodType,
|
periodType,
|
||||||
selectedPeriodId,
|
selectedPeriodId,
|
||||||
comparison,
|
comparison,
|
||||||
@@ -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 (
|
||||||
@@ -92,9 +83,6 @@ export default function HistoryChartView(props: ViewProps) {
|
|||||||
border: "1px solid",
|
border: "1px solid",
|
||||||
borderColor: "divider",
|
borderColor: "divider",
|
||||||
bgcolor: isDark ? "background.paper" : colorScheme.light,
|
bgcolor: isDark ? "background.paper" : colorScheme.light,
|
||||||
opacity: props.isFetching ? 0.6 : 1,
|
|
||||||
transition: "opacity 0.3s ease",
|
|
||||||
pointerEvents: props.isFetching ? "none" : "auto",
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Typography variant="h6" fontWeight={700} gutterBottom>
|
<Typography variant="h6" fontWeight={700} gutterBottom>
|
||||||
|
|||||||
@@ -1,79 +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,
|
|
||||||
): Transaction[] {
|
|
||||||
// 1. Get raw transactions
|
|
||||||
let rawTxns: Transaction[] = [];
|
|
||||||
|
|
||||||
if (selectedPeriodId) {
|
|
||||||
const key = periodIdToKey(selectedPeriodId);
|
|
||||||
const periods = mergeBucketPeriods(reportData.buckets, key);
|
|
||||||
const selected = periods.find((p) => p.id === selectedPeriodId);
|
|
||||||
rawTxns = selected?.metric.transactions || [];
|
|
||||||
} else {
|
|
||||||
const periods = mergeBucketPeriods(reportData.buckets, "all");
|
|
||||||
if (periods.length > 0) {
|
|
||||||
rawTxns = periods[0].metric.transactions || [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Filter by group key
|
|
||||||
if (selectedGroupKey) {
|
|
||||||
rawTxns = rawTxns.filter(txn => {
|
|
||||||
let match = true;
|
|
||||||
if (selectedGroupKey.tags && selectedGroupKey.tags.length > 0) {
|
|
||||||
if (!txn.tags) match = false;
|
|
||||||
else {
|
|
||||||
const txnTags = txn.tags.map(t => typeof t === "string" ? t : t.name);
|
|
||||||
if (!selectedGroupKey.tags.every(selectedTag => txnTags.includes(selectedTag))) match = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (match && selectedGroupKey.payee && selectedGroupKey.payee.length > 0) {
|
|
||||||
if (!txn.payee || !txn.payee.name) match = false;
|
|
||||||
else {
|
|
||||||
if (!selectedGroupKey.payee.includes(txn.payee.name)) match = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return match;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return rawTxns;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ─── Main adapter ────────────────────────────────────────────
|
|
||||||
|
|
||||||
export function buildLatestItems(
|
|
||||||
reportData: ReportData,
|
|
||||||
selectedPeriodId: string | null,
|
|
||||||
selectedGroupKey: GroupKey | null,
|
|
||||||
flow: "outflows" | "inflows"
|
|
||||||
): LatestItem[] {
|
|
||||||
const txns = extractTransactions(reportData, selectedPeriodId, selectedGroupKey);
|
|
||||||
|
|
||||||
return txns
|
|
||||||
.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,16 +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[];
|
||||||
header: string;
|
onViewAll?: () => void;
|
||||||
accentColor: string;
|
accentColor: string;
|
||||||
canExpand: boolean;
|
|
||||||
onExpand: () => void;
|
|
||||||
isFetching?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,47 +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;
|
||||||
flow: "outflows" | "inflows";
|
icon: React.ReactNode;
|
||||||
header: string;
|
iconBgColor?: string;
|
||||||
selectedPeriodId: string | null;
|
title: string;
|
||||||
selectedGroupKey?: GroupKey | null;
|
subtitle: string;
|
||||||
accentColor: string;
|
amount: string;
|
||||||
isFetching?: boolean;
|
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",
|
||||||
flow,
|
items,
|
||||||
header,
|
onViewAll,
|
||||||
selectedPeriodId,
|
|
||||||
selectedGroupKey = null,
|
|
||||||
accentColor,
|
accentColor,
|
||||||
isFetching,
|
}: LatestItemsListProps) {
|
||||||
}: Props) {
|
|
||||||
const [visibleCount, setVisibleCount] = React.useState(5);
|
|
||||||
|
|
||||||
const allItems = React.useMemo(() => {
|
|
||||||
return buildLatestItems(reportData, selectedPeriodId, selectedGroupKey, flow);
|
|
||||||
}, [reportData, selectedPeriodId, selectedGroupKey, flow]);
|
|
||||||
|
|
||||||
const visibleItems = React.useMemo(() => {
|
|
||||||
return allItems.slice(0, visibleCount);
|
|
||||||
}, [allItems, visibleCount]);
|
|
||||||
|
|
||||||
const canExpand = visibleCount < allItems.length;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<LatestItemsView
|
<Box sx={{ width: "100%", bgcolor: "background.paper", borderRadius: 4, p: 2 }}>
|
||||||
items={visibleItems}
|
{/* Header */}
|
||||||
header={header}
|
<Box sx={{ display: "flex", justifyContent: "space-between", alignItems: "center", mb: 2, px: 2 }}>
|
||||||
accentColor={accentColor}
|
<Typography variant="h6" fontWeight="bold">
|
||||||
canExpand={canExpand}
|
{title}
|
||||||
isFetching={isFetching}
|
</Typography>
|
||||||
onExpand={() => setVisibleCount((prev) => prev + 5)}
|
{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,90 +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} />;
|
||||||
header,
|
|
||||||
accentColor,
|
|
||||||
canExpand,
|
|
||||||
onExpand,
|
|
||||||
isFetching,
|
|
||||||
}: LatestItemsViewProps) {
|
|
||||||
return (
|
|
||||||
<Box sx={{ width: "100%", bgcolor: "background.paper", borderRadius: 4, p: 2, opacity: isFetching ? 0.6 : 1, transition: "opacity 0.3s ease", pointerEvents: isFetching ? "none" : "auto" }}>
|
|
||||||
<Box sx={{ mb: 2, px: 2 }}>
|
|
||||||
<Typography variant="h6" fontWeight="bold">
|
|
||||||
{header}
|
|
||||||
</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,7 +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;
|
|
||||||
isFetching?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,21 +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;
|
|
||||||
},
|
|
||||||
opacity: arguments[0].isFetching ? 0.6 : 1,
|
|
||||||
pointerEvents: arguments[0].isFetching ? "none" : "auto",
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Typography
|
<Typography
|
||||||
|
|||||||
@@ -1,65 +0,0 @@
|
|||||||
import { mergeBucketPeriods, periodIdToKey } from "../report.helpers";
|
|
||||||
import { GroupKey, ReportData } from "../../features/report";
|
|
||||||
|
|
||||||
export interface PayeeItem {
|
|
||||||
name: string;
|
|
||||||
amount: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function extractTopPayees(
|
|
||||||
reportData: ReportData,
|
|
||||||
flow: "outflows" | "inflows",
|
|
||||||
selectedPeriodId?: string | null,
|
|
||||||
selectedGroupKey?: GroupKey | null
|
|
||||||
): { items: PayeeItem[]; total: number } {
|
|
||||||
const payeeMap = new Map<string, number>();
|
|
||||||
|
|
||||||
let targetPeriods = [];
|
|
||||||
|
|
||||||
if (selectedPeriodId) {
|
|
||||||
const key = periodIdToKey(selectedPeriodId);
|
|
||||||
const periods = mergeBucketPeriods(reportData.buckets, key);
|
|
||||||
const selected = periods.find((p) => p.id === selectedPeriodId);
|
|
||||||
if (selected) {
|
|
||||||
targetPeriods.push(selected);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// If no specific period is selected, aggregate over the "all" period bucket
|
|
||||||
targetPeriods = mergeBucketPeriods(reportData.buckets, "all");
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const p of targetPeriods) {
|
|
||||||
let txns = p.metric.transactions || [];
|
|
||||||
|
|
||||||
if (selectedGroupKey?.tags && selectedGroupKey.tags.length > 0) {
|
|
||||||
txns = txns.filter(txn => {
|
|
||||||
if (!txn.tags) return false;
|
|
||||||
const txnTags = txn.tags.map(t => typeof t === "string" ? t : t.name);
|
|
||||||
return selectedGroupKey.tags!.every(selectedTag => txnTags.includes(selectedTag));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const txn of txns) {
|
|
||||||
if (txn.payee && txn.payee.name) {
|
|
||||||
const current = payeeMap.get(txn.payee.name) || 0;
|
|
||||||
payeeMap.set(txn.payee.name, current + txn.amount);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let items: PayeeItem[] = [];
|
|
||||||
let total = 0;
|
|
||||||
|
|
||||||
for (const [name, amount] of payeeMap.entries()) {
|
|
||||||
items.push({ name, amount });
|
|
||||||
total += amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sort descending by amount
|
|
||||||
items.sort((a, b) => b.amount - a.amount);
|
|
||||||
|
|
||||||
return {
|
|
||||||
items: items.slice(0, 4), // Top 4
|
|
||||||
total,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
import * as React from "react";
|
|
||||||
import { Box, Paper, Typography } from "@mui/material";
|
|
||||||
import { ReportData, GroupKey } from "../../features/report";
|
|
||||||
import ProgressCard from "./ProgressCard";
|
|
||||||
import { extractTopPayees } from "./TopPayees.adapter";
|
|
||||||
|
|
||||||
type Props = {
|
|
||||||
reportData: ReportData;
|
|
||||||
flow: "outflows" | "inflows";
|
|
||||||
header: string;
|
|
||||||
selectedPeriodId?: string | null;
|
|
||||||
selectedGroupKey?: GroupKey | null;
|
|
||||||
setSelectedGroupKey?: (key: GroupKey | null) => void;
|
|
||||||
compact?: boolean;
|
|
||||||
isFetching?: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function TopPayees({
|
|
||||||
reportData,
|
|
||||||
flow,
|
|
||||||
header,
|
|
||||||
selectedPeriodId,
|
|
||||||
selectedGroupKey,
|
|
||||||
setSelectedGroupKey,
|
|
||||||
compact = true,
|
|
||||||
isFetching,
|
|
||||||
}: Props) {
|
|
||||||
const { items, total } = React.useMemo(() => {
|
|
||||||
return extractTopPayees(reportData, flow, selectedPeriodId, selectedGroupKey);
|
|
||||||
}, [reportData, flow, selectedPeriodId, selectedGroupKey]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Paper
|
|
||||||
sx={{
|
|
||||||
p: { xs: 2.5, sm: 4 },
|
|
||||||
borderRadius: 4,
|
|
||||||
width: "100%",
|
|
||||||
boxShadow: "none",
|
|
||||||
border: "1px solid",
|
|
||||||
borderColor: "divider",
|
|
||||||
bgcolor: "background.paper",
|
|
||||||
opacity: isFetching ? 0.6 : 1,
|
|
||||||
transition: "opacity 0.3s ease",
|
|
||||||
pointerEvents: isFetching ? "none" : "auto",
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Typography variant="h6" fontWeight={700} gutterBottom>
|
|
||||||
{header}
|
|
||||||
</Typography>
|
|
||||||
|
|
||||||
<Box
|
|
||||||
sx={{
|
|
||||||
display: "grid",
|
|
||||||
gridTemplateColumns: {
|
|
||||||
xs: "1fr",
|
|
||||||
sm: "repeat(2, 1fr)",
|
|
||||||
md: "repeat(4, 1fr)",
|
|
||||||
},
|
|
||||||
gap: 2,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{items.map((item) => {
|
|
||||||
const isSelected = selectedGroupKey?.payee?.includes(item.name);
|
|
||||||
return (
|
|
||||||
<ProgressCard
|
|
||||||
key={item.name}
|
|
||||||
header={item.name}
|
|
||||||
progressAmount={item.amount}
|
|
||||||
totalAmount={total}
|
|
||||||
compact={compact}
|
|
||||||
colorTheme={flow === "outflows" ? "error" : "success"}
|
|
||||||
selected={isSelected}
|
|
||||||
isFetching={isFetching}
|
|
||||||
onClick={() => {
|
|
||||||
if (setSelectedGroupKey) {
|
|
||||||
let newKey = selectedGroupKey ? { ...selectedGroupKey } : {};
|
|
||||||
|
|
||||||
if (isSelected) {
|
|
||||||
delete newKey.payee;
|
|
||||||
} else {
|
|
||||||
newKey.payee = [item.name];
|
|
||||||
}
|
|
||||||
|
|
||||||
setSelectedGroupKey(Object.keys(newKey).length ? newKey : null);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</Box>
|
|
||||||
</Paper>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,71 +0,0 @@
|
|||||||
import { ReportData } from "../../features/report";
|
|
||||||
import {
|
|
||||||
mergeBucketPeriods,
|
|
||||||
periodIdToKey,
|
|
||||||
} from "../report.helpers";
|
|
||||||
|
|
||||||
import { GroupKey } from "../../features/report";
|
|
||||||
|
|
||||||
export interface TagItem {
|
|
||||||
tag: string;
|
|
||||||
amount: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function extractTopTags(
|
|
||||||
reportData: ReportData,
|
|
||||||
flow: "outflows" | "inflows",
|
|
||||||
selectedPeriodId?: string | null,
|
|
||||||
selectedGroupKey?: GroupKey | null
|
|
||||||
): { items: TagItem[]; total: number } {
|
|
||||||
const tagMap = new Map<string, number>();
|
|
||||||
|
|
||||||
let periodKey: ReturnType<typeof periodIdToKey> = "all";
|
|
||||||
if (selectedPeriodId) {
|
|
||||||
periodKey = periodIdToKey(selectedPeriodId);
|
|
||||||
}
|
|
||||||
|
|
||||||
const periods = mergeBucketPeriods(reportData.buckets, periodKey);
|
|
||||||
|
|
||||||
let period = periods[0];
|
|
||||||
if (selectedPeriodId) {
|
|
||||||
period = periods.find(p => p.id === selectedPeriodId) || period;
|
|
||||||
} else if (periods.length > 0) {
|
|
||||||
period = periods.reduce((latest, p) =>
|
|
||||||
new Date(p.start).getTime() > new Date(latest.start).getTime()
|
|
||||||
? p
|
|
||||||
: latest
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (period && period.metric && period.metric.transactions) {
|
|
||||||
let txns = period.metric.transactions;
|
|
||||||
if (selectedGroupKey?.payee && selectedGroupKey.payee.length > 0) {
|
|
||||||
txns = txns.filter(txn =>
|
|
||||||
txn.payee?.name && selectedGroupKey.payee!.includes(txn.payee.name)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const txn of txns) {
|
|
||||||
if (txn.tags && txn.tags.length > 0) {
|
|
||||||
for (const tagObj of txn.tags) {
|
|
||||||
const tagName = typeof tagObj === "string" ? tagObj : tagObj.name;
|
|
||||||
tagMap.set(tagName, (tagMap.get(tagName) || 0) + txn.amount);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
tagMap.set("Untagged", (tagMap.get("Untagged") || 0) + txn.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,93 +1,105 @@
|
|||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import { Box, Paper, Typography } 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;
|
||||||
flow: "outflows" | "inflows";
|
mode: "expense" | "income";
|
||||||
header: string;
|
|
||||||
selectedPeriodId?: string | null;
|
selectedPeriodId?: string | null;
|
||||||
selectedGroupKey?: GroupKey | null;
|
|
||||||
setSelectedGroupKey?: (key: GroupKey | null) => void;
|
|
||||||
compact?: boolean;
|
compact?: boolean;
|
||||||
isFetching?: 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,
|
||||||
flow,
|
mode,
|
||||||
header,
|
|
||||||
selectedPeriodId,
|
selectedPeriodId,
|
||||||
selectedGroupKey,
|
compact = true
|
||||||
setSelectedGroupKey,
|
|
||||||
compact = true,
|
|
||||||
isFetching,
|
|
||||||
}: Props) {
|
}: Props) {
|
||||||
const { items, total } = React.useMemo(() => {
|
const { items, total } = React.useMemo(() => {
|
||||||
return extractTopTags(reportData, flow, selectedPeriodId, selectedGroupKey);
|
const tagMap = new Map<string, number>();
|
||||||
}, [reportData, flow, selectedPeriodId, selectedGroupKey]);
|
|
||||||
|
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 };
|
||||||
|
}, [reportData, mode, selectedPeriodId]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Paper
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
p: { xs: 2.5, sm: 4 },
|
display: "grid",
|
||||||
borderRadius: 4,
|
gridTemplateColumns: "repeat(4, 1fr)",
|
||||||
width: "100%",
|
gap: 2
|
||||||
boxShadow: "none",
|
|
||||||
border: "1px solid",
|
|
||||||
borderColor: "divider",
|
|
||||||
bgcolor: "background.paper",
|
|
||||||
opacity: isFetching ? 0.6 : 1,
|
|
||||||
transition: "opacity 0.3s ease",
|
|
||||||
pointerEvents: isFetching ? "none" : "auto",
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Typography variant="h6" fontWeight={700} gutterBottom>
|
{items.map((item) => (
|
||||||
{header}
|
<ProgressCard
|
||||||
</Typography>
|
key={item.tag}
|
||||||
|
header={item.tag}
|
||||||
<Box
|
progressAmount={item.amount}
|
||||||
sx={{
|
totalAmount={total}
|
||||||
display: "grid",
|
compact={compact}
|
||||||
gridTemplateColumns: {
|
colorTheme={mode === "expense" ? "error" : "success"}
|
||||||
xs: "1fr",
|
/>
|
||||||
sm: "repeat(2, 1fr)",
|
))}
|
||||||
md: "repeat(4, 1fr)",
|
</Box>
|
||||||
},
|
|
||||||
gap: 2,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{items.map((item) => {
|
|
||||||
const isSelected = selectedGroupKey?.tags?.includes(item.tag);
|
|
||||||
return (
|
|
||||||
<ProgressCard
|
|
||||||
key={item.tag}
|
|
||||||
header={item.tag}
|
|
||||||
progressAmount={item.amount}
|
|
||||||
totalAmount={total}
|
|
||||||
compact={compact}
|
|
||||||
colorTheme={flow === "outflows" ? "error" : "success"}
|
|
||||||
selected={isSelected}
|
|
||||||
isFetching={isFetching}
|
|
||||||
onClick={() => {
|
|
||||||
if (setSelectedGroupKey) {
|
|
||||||
let newKey = selectedGroupKey ? { ...selectedGroupKey } : {};
|
|
||||||
|
|
||||||
if (isSelected) {
|
|
||||||
delete newKey.tags;
|
|
||||||
} else {
|
|
||||||
newKey.tags = [item.tag];
|
|
||||||
}
|
|
||||||
|
|
||||||
setSelectedGroupKey(Object.keys(newKey).length ? newKey : null);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</Box>
|
|
||||||
</Paper>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,142 +0,0 @@
|
|||||||
import {
|
|
||||||
ReportPeriod,
|
|
||||||
ReportBucket,
|
|
||||||
GroupKey,
|
|
||||||
PeriodType,
|
|
||||||
} from "../features/report";
|
|
||||||
|
|
||||||
// ─── Types ────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
export type PeriodKey = PeriodType;
|
|
||||||
|
|
||||||
export type DecoratedPeriod = ReportPeriod & {
|
|
||||||
id: string;
|
|
||||||
label: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
// ─── Period helpers ───────────────────────────────────────────
|
|
||||||
|
|
||||||
const PREFIX_TO_KEY: Record<string, PeriodKey> = {
|
|
||||||
D: "daily",
|
|
||||||
W: "weekly",
|
|
||||||
M: "monthly",
|
|
||||||
ALL: "all",
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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] ?? "all";
|
|
||||||
}
|
|
||||||
|
|
||||||
// ─── Metric helpers ───────────────────────────────────────────
|
|
||||||
|
|
||||||
export function getAmount(period: ReportPeriod): number {
|
|
||||||
return period.metric.sum;
|
|
||||||
}
|
|
||||||
|
|
||||||
function mergeMetric(a: ReportPeriod["metric"], b: ReportPeriod["metric"]) {
|
|
||||||
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,
|
|
||||||
metric: { ...p.metric },
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
map.set(p.id, {
|
|
||||||
...existing,
|
|
||||||
metric: mergeMetric(existing.metric, p.metric),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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];
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
@@ -2,7 +2,6 @@ import HistoryChart from "./components/HistoryChart";
|
|||||||
import LatestItems from "./components/LatestItems";
|
import LatestItems from "./components/LatestItems";
|
||||||
import { DashboardConfig } from "./components/Dashboard";
|
import { DashboardConfig } from "./components/Dashboard";
|
||||||
import TopTags from "./components/ProgressCard/TopTags";
|
import TopTags from "./components/ProgressCard/TopTags";
|
||||||
import TopPayees from "./components/ProgressCard/TopPayees";
|
|
||||||
|
|
||||||
export const configuration: DashboardConfig = {
|
export const configuration: DashboardConfig = {
|
||||||
sections: [
|
sections: [
|
||||||
@@ -13,17 +12,7 @@ export const configuration: DashboardConfig = {
|
|||||||
component: HistoryChart,
|
component: HistoryChart,
|
||||||
settings: {
|
settings: {
|
||||||
tabs: ["Weekly", "Monthly"],
|
tabs: ["Weekly", "Monthly"],
|
||||||
},
|
// tabs: ["Weekly", "Monthly", "Yearly", "Financial Year", "All Time"],
|
||||||
style: {
|
|
||||||
size: 12,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "top-categories",
|
|
||||||
title: 'Top Categories',
|
|
||||||
component: TopTags,
|
|
||||||
settings: {
|
|
||||||
compact: true,
|
|
||||||
},
|
},
|
||||||
style: {
|
style: {
|
||||||
size: 12,
|
size: 12,
|
||||||
@@ -32,7 +21,7 @@ export const configuration: DashboardConfig = {
|
|||||||
{
|
{
|
||||||
id: "top-payees",
|
id: "top-payees",
|
||||||
title: 'Top Payees',
|
title: 'Top Payees',
|
||||||
component: TopPayees,
|
component: TopTags,
|
||||||
settings: {
|
settings: {
|
||||||
compact: true,
|
compact: true,
|
||||||
},
|
},
|
||||||
@@ -40,18 +29,19 @@ export const configuration: DashboardConfig = {
|
|||||||
size: 12,
|
size: 12,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
// {
|
||||||
id: "items",
|
// id: "latest",
|
||||||
title: 'Recent Transactions',
|
// title: 'Recent Transactions',
|
||||||
component: LatestItems,
|
// component: LatestItems,
|
||||||
style: {
|
// dataKey: "latest",
|
||||||
size: 12,
|
// style: {
|
||||||
},
|
// size: 12,
|
||||||
},
|
// },
|
||||||
|
// },
|
||||||
],
|
],
|
||||||
style: {
|
style: {
|
||||||
palette: {
|
palette: {
|
||||||
outflows: {
|
expense: {
|
||||||
light: {
|
light: {
|
||||||
primary: "#d32f2f",
|
primary: "#d32f2f",
|
||||||
background: "#fdecea",
|
background: "#fdecea",
|
||||||
@@ -63,7 +53,7 @@ export const configuration: DashboardConfig = {
|
|||||||
text: "#ffcdd2"
|
text: "#ffcdd2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
inflows: {
|
income: {
|
||||||
light: {
|
light: {
|
||||||
primary: "#2e7d32",
|
primary: "#2e7d32",
|
||||||
background: "#e8f5e9",
|
background: "#e8f5e9",
|
||||||
|
|||||||
@@ -4,11 +4,7 @@ export {
|
|||||||
export type {
|
export type {
|
||||||
Transaction,
|
Transaction,
|
||||||
ReportData,
|
ReportData,
|
||||||
ReportBucket,
|
|
||||||
ReportPeriod,
|
ReportPeriod,
|
||||||
ReportQuery,
|
|
||||||
GroupKey,
|
|
||||||
PeriodType,
|
|
||||||
} from './report.models'
|
} from './report.models'
|
||||||
export {
|
export {
|
||||||
prepareReport
|
prepareReport
|
||||||
|
|||||||
@@ -1,40 +1,29 @@
|
|||||||
export interface Payor {
|
export interface Payor {
|
||||||
id?: string;
|
|
||||||
name: string;
|
name: string;
|
||||||
username: string;
|
|
||||||
email: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Payee {
|
export interface Payee {
|
||||||
type: "merchant" | "person" | "transfer" | "other";
|
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Account {
|
export interface Account {
|
||||||
id: string;
|
|
||||||
name: string;
|
name: string;
|
||||||
number: string;
|
number: string;
|
||||||
type: "cash" | "bank" | "credit_card" | "wallet" | "other";
|
|
||||||
currency: string;
|
|
||||||
is_active?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Tag {
|
export interface Tag {
|
||||||
id: string;
|
|
||||||
name: string;
|
name: string;
|
||||||
icon: string;
|
icon: string;
|
||||||
parent_id?: string | null;
|
description: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Transaction {
|
export interface Transaction {
|
||||||
id: string;
|
|
||||||
payor: Payor;
|
payor: Payor;
|
||||||
payee: Payee;
|
payee: Payee;
|
||||||
amount: number;
|
amount: number;
|
||||||
account: Account;
|
account: Account;
|
||||||
tags: Tag[];
|
tags: Tag[];
|
||||||
occurred_at: string;
|
occurred_at: Date;
|
||||||
created_at: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------
|
// -----------------------------
|
||||||
@@ -52,12 +41,12 @@ export interface ReportMetric {
|
|||||||
// Period
|
// Period
|
||||||
// -----------------------------
|
// -----------------------------
|
||||||
|
|
||||||
export type PeriodType = "daily" | "weekly" | "monthly" | "all";
|
|
||||||
|
|
||||||
export interface ReportPeriod {
|
export interface ReportPeriod {
|
||||||
start: string;
|
start: Date;
|
||||||
end: string;
|
end: Date;
|
||||||
metric: ReportMetric;
|
|
||||||
|
expenses: ReportMetric;
|
||||||
|
incomes: ReportMetric;
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------
|
// -----------------------------
|
||||||
@@ -65,48 +54,37 @@ export interface ReportPeriod {
|
|||||||
// -----------------------------
|
// -----------------------------
|
||||||
|
|
||||||
export type GroupKey = {
|
export type GroupKey = {
|
||||||
[dimension: string]: string[];
|
payee?: string[];
|
||||||
|
tags?: string[];
|
||||||
|
flow?: string[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface ReportBucket {
|
export interface ReportBucket {
|
||||||
group_key: GroupKey;
|
group_key: GroupKey;
|
||||||
|
|
||||||
periods: {
|
periods: {
|
||||||
daily?: ReportPeriod[];
|
|
||||||
weekly?: ReportPeriod[];
|
weekly?: ReportPeriod[];
|
||||||
monthly?: ReportPeriod[];
|
monthly?: ReportPeriod[];
|
||||||
all?: ReportPeriod[];
|
yearly?: ReportPeriod[];
|
||||||
|
fyly?: ReportPeriod[];
|
||||||
|
full?: ReportPeriod[];
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------
|
|
||||||
// Report Query
|
|
||||||
// -----------------------------
|
|
||||||
|
|
||||||
export interface ReportQuery {
|
|
||||||
accounts?: string[] | null;
|
|
||||||
ignore_self?: boolean | null;
|
|
||||||
start_date?: string | null;
|
|
||||||
end_date?: string | null;
|
|
||||||
min_amount?: number | null;
|
|
||||||
max_amount?: number | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// -----------------------------
|
// -----------------------------
|
||||||
// Final Report
|
// Final Report
|
||||||
// -----------------------------
|
// -----------------------------
|
||||||
|
|
||||||
export interface ReportData {
|
export interface ReportData {
|
||||||
snapshot_id?: string | null;
|
periods: ("weekly" | "monthly" | "yearly" | "fyly" | "full")[];
|
||||||
|
|
||||||
flow?: "inflows" | "outflows" | null;
|
rolling: boolean;
|
||||||
|
report_date?: string;
|
||||||
|
|
||||||
periods: PeriodType[];
|
group_by: ("payee" | "tags")[];
|
||||||
|
|
||||||
tags?: string[] | null;
|
ignore_self: boolean;
|
||||||
payee?: string[] | null;
|
include_transactions: boolean;
|
||||||
|
|
||||||
buckets: ReportBucket[];
|
buckets: ReportBucket[];
|
||||||
|
|
||||||
query: ReportQuery;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
ReportData,
|
ReportData,
|
||||||
ReportPeriod,
|
ReportPeriod
|
||||||
PeriodType,
|
|
||||||
} from "./report.models";
|
} from "./report.models";
|
||||||
|
|
||||||
/* ---------- ID BUILDING ---------- */
|
/* ---------- ID BUILDING ---------- */
|
||||||
@@ -14,7 +13,7 @@ function formatDate(d: Date): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function buildPeriodId(
|
function buildPeriodId(
|
||||||
type: PeriodType,
|
type: "weekly" | "monthly" | "yearly" | "fyly" | "full",
|
||||||
start: Date,
|
start: Date,
|
||||||
end: Date
|
end: Date
|
||||||
): string {
|
): string {
|
||||||
@@ -22,14 +21,16 @@ function buildPeriodId(
|
|||||||
const e = formatDate(end);
|
const e = formatDate(end);
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "daily":
|
|
||||||
return `D:${s}_${e}`;
|
|
||||||
case "weekly":
|
case "weekly":
|
||||||
return `W:${s}_${e}`;
|
return `W:${s}_${e}`;
|
||||||
case "monthly":
|
case "monthly":
|
||||||
return `M:${s}_${e}`;
|
return `M:${s}_${e}`;
|
||||||
case "all":
|
case "yearly":
|
||||||
return `ALL:${s}_${e}`;
|
return `Y:${s}_${e}`;
|
||||||
|
case "fyly":
|
||||||
|
return `FY:${s}_${e}`;
|
||||||
|
case "full":
|
||||||
|
return `FULL:${s}_${e}`;
|
||||||
default:
|
default:
|
||||||
return `${s}_${e}`;
|
return `${s}_${e}`;
|
||||||
}
|
}
|
||||||
@@ -59,24 +60,40 @@ const yearFmt = new Intl.DateTimeFormat("en-GB", {
|
|||||||
timeZone: "UTC",
|
timeZone: "UTC",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function sameMonth(a: Date, b: Date) {
|
||||||
|
return (
|
||||||
|
a.getUTCFullYear() === b.getUTCFullYear() &&
|
||||||
|
a.getUTCMonth() === b.getUTCMonth()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function buildLabel(
|
function buildLabel(
|
||||||
type: PeriodType,
|
type: "weekly" | "monthly" | "yearly" | "fyly" | "full",
|
||||||
start: Date,
|
start: Date,
|
||||||
end: Date
|
end: Date
|
||||||
): string {
|
): string {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "daily":
|
case "weekly":
|
||||||
return dayFmt.format(start);
|
if (sameMonth(start, end)) {
|
||||||
|
const sDay = start.getUTCDate();
|
||||||
case "weekly": {
|
const eDay = end.getUTCDate();
|
||||||
const sDay = start.getUTCDate();
|
const m = monthFmt.format(start);
|
||||||
const m = monthFmt.format(start);
|
return `${sDay} ${m} - ${eDay} ${m}`;
|
||||||
return `${sDay} ${m}`;
|
}
|
||||||
}
|
return `${dayFmt.format(start)} - ${dayFmt.format(end)}`;
|
||||||
|
|
||||||
case "monthly":
|
case "monthly":
|
||||||
return `${monthFmt.format(start)} ${yearFmt.format(start)}`;
|
return `${monthFmt.format(start)} ${yearFmt.format(start)}`;
|
||||||
|
|
||||||
|
case "yearly":
|
||||||
|
return yearFmt.format(start);
|
||||||
|
|
||||||
|
case "fyly": {
|
||||||
|
const startY = start.getUTCFullYear();
|
||||||
|
const endY = end.getUTCFullYear();
|
||||||
|
return `FY ${startY}–${String(endY).slice(-2)}`;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return `${monthDayFmt.format(start)} - ${monthDayFmt.format(end)}`;
|
return `${monthDayFmt.format(start)} - ${monthDayFmt.format(end)}`;
|
||||||
}
|
}
|
||||||
@@ -85,7 +102,7 @@ function buildLabel(
|
|||||||
/* ---------- MAIN ---------- */
|
/* ---------- MAIN ---------- */
|
||||||
|
|
||||||
function decoratePeriods(
|
function decoratePeriods(
|
||||||
type: PeriodType,
|
type: "weekly" | "monthly" | "yearly" | "fyly" | "full",
|
||||||
periods: ReportPeriod[]
|
periods: ReportPeriod[]
|
||||||
): (ReportPeriod & { id: string; label: string })[] {
|
): (ReportPeriod & { id: string; label: string })[] {
|
||||||
return periods.map((p) => ({
|
return periods.map((p) => ({
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
import { useResourceByName } from "../../../react-openapi";
|
import { useResourceByName } from "../../../react-openapi";
|
||||||
|
|
||||||
export interface ReportParams {
|
export interface ReportParams {
|
||||||
snapshot_id?: string;
|
periods?: ("weekly" | "monthly" | "yearly" | "fyly" | "full")[];
|
||||||
periods?: ("daily" | "weekly" | "monthly" | "all")[];
|
rolling?: boolean;
|
||||||
flow?: "inflows" | "outflows";
|
report_date?: string;
|
||||||
payee?: string[];
|
group_by?: ("payee" | "tags")[];
|
||||||
tags?: string[];
|
ignore_self?: boolean;
|
||||||
|
include_transactions?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useReport(params: ReportParams) {
|
export function useReport(params: ReportParams) {
|
||||||
@@ -14,5 +15,6 @@ export function useReport(params: ReportParams) {
|
|||||||
return useList({
|
return useList({
|
||||||
...params,
|
...params,
|
||||||
periods: params.periods,
|
periods: params.periods,
|
||||||
|
group_by: params.group_by,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user