Files
blitzkit/packages/website-ue/src/pages/[...locale]/tanks/_index.tsx
T

70 lines
2.0 KiB
TypeScript
Raw Normal View History

2026-04-27 19:57:44 -05:00
import fuzzysort from "fuzzysort";
2026-04-27 19:34:06 -05:00
import { useMemo } from "react";
2026-05-15 18:43:08 -05:00
import { api } from "../../../api/dynamic";
2026-04-28 15:31:21 -05:00
import { IncrementalLoader } from "../../../components/IncrementalLoader";
2026-04-26 18:19:09 -05:00
import { TankCard } from "../../../components/TankCard";
import { TankCards } from "../../../components/TankCards";
2026-04-27 19:57:44 -05:00
import { TankSearch } from "../../../components/TankSearch";
2026-05-20 12:17:09 -05:00
import { withLocale } from "../../../hocs/withLocale";
2026-04-26 18:19:09 -05:00
import { useAwait } from "../../../hooks/useAwait";
2026-04-27 19:57:44 -05:00
import { useSearchableTanks } from "../../../hooks/useSearchableTanks";
2026-04-27 19:34:06 -05:00
import { useTierCompare } from "../../../hooks/useTierCompare";
2026-04-27 19:57:44 -05:00
import { TankFilters } from "../../../stores/tankFilters";
const { go } = fuzzysort;
2026-04-26 18:19:09 -05:00
2026-05-20 12:17:09 -05:00
export const Page = withLocale(() => {
2026-04-27 19:34:06 -05:00
const tanks = useAwait(() => api.tanks(), "tanks");
2026-04-27 19:57:44 -05:00
2026-04-27 19:34:06 -05:00
const compareTiers = useTierCompare();
2026-04-27 19:57:44 -05:00
const searchableTanks = useSearchableTanks();
const search = TankFilters.use((state) => state.search);
2026-04-27 19:34:06 -05:00
const tanksOrdered = useMemo(() => {
let list = Object.values(tanks.tanks);
2026-04-28 15:11:20 -05:00
list = list.sort((a, b) => a.tank!.tank_type - b.tank!.tank_type);
list = list.sort((a, b) => a.tank!.tank_class - b.tank!.tank_class);
list = list.sort((a, b) => a.tank!.nation - b.tank!.nation);
2026-04-27 19:34:06 -05:00
list = list.sort((a, b) =>
compareTiers(a.tank!.tier_catalog_id, b.tank!.tier_catalog_id),
);
2026-04-28 15:31:21 -05:00
return list.map(({ id }) => id);
2026-04-27 19:34:06 -05:00
}, []);
2026-04-27 19:57:44 -05:00
const tanksFiltered = useMemo(() => {
if (!search) return tanksOrdered;
const results = go(search, searchableTanks, {
keys: ["id", "name", "deburred"],
});
2026-04-28 15:31:21 -05:00
return results.map(({ obj }) => obj.id);
2026-04-27 19:57:44 -05:00
}, [search]);
2026-04-26 18:19:09 -05:00
return (
2026-04-27 19:57:44 -05:00
<>
<TankSearch />
2026-04-28 15:31:21 -05:00
<List tanks={tanksFiltered} />
2026-04-27 19:57:44 -05:00
</>
2026-04-26 18:19:09 -05:00
);
2026-05-20 12:17:09 -05:00
});
2026-04-28 15:31:21 -05:00
interface ListProps {
tanks: string[];
}
function List({ tanks }: ListProps) {
const data = useMemo(() => tanks.map((id) => ({ key: id, id })), [tanks]);
return (
<TankCards>
<IncrementalLoader
initial={7 * 5}
intermediate={7 * 3}
data={data}
Component={TankCard}
/>
</TankCards>
);
}