72 lines
1.4 KiB
TypeScript
72 lines
1.4 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
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 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),
|
|
}));
|
|
};
|