Add sorting by all fields in totals

This commit is contained in:
Calli
2024-07-01 16:03:29 +03:00
parent e73c49f0a2
commit 76e56a0fa4

View File

@@ -41,9 +41,13 @@ const displayValue = (valueInMillions: number): string =>
? `${(valueInMillions / 1000).toFixed(2)} B` ? `${(valueInMillions / 1000).toFixed(2)} B`
: `${valueInMillions.toFixed(2)} M`; : `${valueInMillions.toFixed(2)} M`;
type SortBy = "name" | "perHour" | "price";
type SortDirection = "asc" | "desc";
export const Summary = ({ characters }: { characters: AccessToken[] }) => { export const Summary = ({ characters }: { characters: AccessToken[] }) => {
const { piPrices } = useContext(SessionContext); const { piPrices } = useContext(SessionContext);
const [sort, setSort] = useState(true); const [sortDirection, setSortDirection] = useState<SortDirection>("asc");
const [sortBy, setSortBy] = useState<SortBy>("name");
const exports = characters.flatMap((char) => { const exports = characters.flatMap((char) => {
return char.planets return char.planets
.filter( .filter(
@@ -102,8 +106,13 @@ export const Summary = ({ characters }: { characters: AccessToken[] }) => {
<Tooltip title="What exports factories are producing"> <Tooltip title="What exports factories are producing">
<TableSortLabel <TableSortLabel
active={true} active={true}
direction={sort ? "asc" : "desc"} direction={sortDirection}
onClick={() => setSort(!sort)} onClick={() => {
setSortDirection(
sortDirection === "asc" ? "desc" : "asc",
);
setSortBy("name");
}}
> >
Exports Exports
</TableSortLabel> </TableSortLabel>
@@ -111,16 +120,34 @@ export const Summary = ({ characters }: { characters: AccessToken[] }) => {
</TableCell> </TableCell>
<TableCell width="10%"> <TableCell width="10%">
<Tooltip title="How many units per hour factories are producing"> <Tooltip title="How many units per hour factories are producing">
<Typography fontSize={theme.custom.smallText}> <TableSortLabel
active={true}
direction={sortDirection}
onClick={() => {
setSortDirection(
sortDirection === "asc" ? "desc" : "asc",
);
setSortBy("perHour");
}}
>
u/h u/h
</Typography> </TableSortLabel>
</Tooltip> </Tooltip>
</TableCell> </TableCell>
<TableCell width="10%" align="right"> <TableCell width="10%" align="right">
<Tooltip title="How many million ISK per month this planet is exporting (Jita sell min)"> <Tooltip title="How many million ISK per month this planet is exporting (Jita sell min)">
<Typography fontSize={theme.custom.smallText}> <TableSortLabel
active={true}
direction={sortDirection}
onClick={() => {
setSortDirection(
sortDirection === "asc" ? "desc" : "asc",
);
setSortBy("price");
}}
>
ISK/M ISK/M
</Typography> </TableSortLabel>
</Tooltip> </Tooltip>
</TableCell> </TableCell>
</TableRow> </TableRow>
@@ -128,9 +155,24 @@ export const Summary = ({ characters }: { characters: AccessToken[] }) => {
<TableBody> <TableBody>
{withProductNameAndPrice {withProductNameAndPrice
.sort((a, b) => { .sort((a, b) => {
if (a.materialName === b.materialName) return 0; if (sortBy === "name") {
if (sort) return a.materialName > b.materialName ? 1 : -1; if (sortDirection === "asc")
if (!sort) return a.materialName > b.materialName ? -1 : 1; return a.materialName > b.materialName ? 1 : -1;
if (sortDirection === "desc")
return a.materialName > b.materialName ? -1 : 1;
}
if (sortBy === "perHour") {
if (sortDirection === "asc")
return a.amount > b.amount ? 1 : -1;
if (sortDirection === "desc")
return a.amount > b.amount ? -1 : 1;
}
if (sortBy === "price") {
if (sortDirection === "asc")
return a.price > b.price ? 1 : -1;
if (sortDirection === "desc")
return a.price > b.price ? -1 : 1;
}
return 0; return 0;
}) })
.map((product) => ( .map((product) => (