Files
prowler/ui/components/graphs/hooks/useSortableData.ts
2025-10-13 13:53:28 +02:00

34 lines
776 B
TypeScript

import { useState } from "react";
import { DEFAULT_SORT_OPTION, SORT_OPTIONS } from "../shared/constants";
type SortOption = (typeof SORT_OPTIONS)[keyof typeof SORT_OPTIONS];
interface SortableItem {
name: string;
value: number;
}
export function useSortableData<T extends SortableItem>(data: T[]) {
const [sortBy, setSortBy] = useState<SortOption>(DEFAULT_SORT_OPTION);
const sortedData = [...data].sort((a, b) => {
switch (sortBy) {
case SORT_OPTIONS.highLow:
return b.value - a.value;
case SORT_OPTIONS.lowHigh:
return a.value - b.value;
case SORT_OPTIONS.alphabetical:
return a.name.localeCompare(b.name);
default:
return 0;
}
});
return {
sortBy,
setSortBy,
sortedData,
};
}