2025-12-27 10:57:56 +01:00

99 lines
2.1 KiB
TypeScript

/**
* Map transaction state to color
*/
export const getStateColor = (state: string): string => {
const normalizedState = state.toLowerCase();
switch (normalizedState) {
case "success":
case "completed":
case "successful":
return "#4caf50"; // green
case "pending":
case "waiting":
return "#ff9800"; // orange
case "failed":
case "error":
return "#f44336"; // red
case "cancelled":
case "canceled":
return "#9e9e9e"; // gray
default:
return "#9e9e9e"; // gray
}
};
/**
* Transform flat API overview response to array format
*/
export const transformOverviewResponse = (data: {
cancelled?: number;
failed?: number;
successful?: number;
waiting?: number;
}): Array<{
state: string;
count: number;
}> => {
const states = [
{ key: "successful", label: "Successful" },
{ key: "waiting", label: "Waiting" },
{ key: "failed", label: "Failed" },
{ key: "cancelled", label: "Cancelled" },
];
return states
.map(({ key, label }) => ({
state: label,
count: data[key as keyof typeof data] || 0,
}))
.filter(item => item.count > 0); // Only include states with counts > 0
};
/**
* Calculate percentage for each state
*/
export const calculatePercentages = (
items: Array<{ state: string; count: number }>
): Array<{
state: string;
count: number;
percentage: string;
}> => {
const total = items.reduce((sum, item) => sum + item.count, 0);
if (total === 0) {
return items.map(item => ({
...item,
percentage: "0%",
}));
}
return items.map(item => ({
...item,
percentage: `${Math.round((item.count / total) * 100)}%`,
}));
};
/**
* Transform API overview data to include colors if missing
*/
export const enrichOverviewData = (
data: Array<{
state: string;
count: number;
percentage: string;
color?: string;
}>
): Array<{
state: string;
count: number;
percentage: string;
color: string;
}> => {
return data.map(item => ({
...item,
color: item.color || getStateColor(item.state),
}));
};