add extra filters

This commit is contained in:
2026-01-12 08:57:42 +01:00
parent 500be84f39
commit d560b6db6f
4 changed files with 213 additions and 8 deletions
+55 -6
View File
@@ -7,7 +7,13 @@ import {
} from "../../items";
import { apiFetch } from "../api";
export function useGameFilter(people: Person[], selectedPeople: Set<string>) {
export function useGameFilter(
people: Person[],
selectedPeople: Set<string>,
freeGamesOnly: boolean,
maxPrice: number | null,
ownershipMode: boolean
) {
const [fetchedTitles, setFetchedTitles] = useState<string[]>([]);
const metaDataRef = useRef<{ [key: string]: GameProto }>({});
@@ -79,20 +85,63 @@ export function useGameFilter(people: Person[], selectedPeople: Set<string>) {
.filter((title) => metaDataRef.current[title])
.map((title) => metaDataRef.current[title]);
return filterByPlayerCount(games, selectedPeople.size);
return filterGames(
games,
selectedPeople.size,
freeGamesOnly,
maxPrice,
ownershipMode,
selectedPeople,
people
);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [titlesEveryoneWouldPlay, selectedPeople.size, fetchedTitles]);
}, [
titlesEveryoneWouldPlay,
selectedPeople.size,
fetchedTitles,
freeGamesOnly,
maxPrice,
ownershipMode,
people,
selectedPeople,
]);
return { filteredGames, gameToPositive: gameToPositiveOpinion };
const gamesMap = useMemo(() => {
return new Map(Object.entries(metaDataRef.current));
}, [fetchedTitles]);
return { filteredGames, gameToPositive: gameToPositiveOpinion, games: gamesMap };
}
function filterByPlayerCount(
function filterGames(
games: GameProto[],
playerCount: number
playerCount: number,
freeGamesOnly: boolean,
maxPrice: number | null,
ownershipMode: boolean,
selectedPeople: Set<string>,
people: Person[]
): string[] {
const selectedPersons = people.filter((p) => selectedPeople.has(p.name));
return games
.filter(
(game) => game.maxPlayers >= playerCount && game.minPlayers <= playerCount
)
.filter((game) => {
if (freeGamesOnly) return game.price === 0;
if (maxPrice !== null) return game.price <= maxPrice;
return true;
})
.filter((game) => {
if (!ownershipMode) return true;
if (game.price === 0) return true;
return selectedPersons.every((person) =>
person.opinion.some(
(op) => op.title === game.title && op.wouldPlay
)
);
})
.map((game) => game.title);
}