Compare commits
1 Commits
1733d3147c
...
36b2cfa4a7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36b2cfa4a7 |
@ -3,6 +3,7 @@
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Game List</title>
|
||||
</head>
|
||||
|
||||
@ -24,13 +24,7 @@ function App() {
|
||||
const [token, setToken] = useState<string>(
|
||||
localStorage.getItem("token") || ""
|
||||
);
|
||||
const [theme, _setTheme] = useState<string>(
|
||||
localStorage.getItem("theme") || "default"
|
||||
);
|
||||
const setTheme = (theme: string) => {
|
||||
_setTheme(theme);
|
||||
localStorage.setItem("theme", theme);
|
||||
};
|
||||
const [theme, setTheme] = useState<string>("default");
|
||||
const [toasts, setToasts] = useState<ToastMessage[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
|
||||
@ -1,19 +1,25 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { Person, PersonList as PersonListProto } from "../items";
|
||||
import {
|
||||
Person,
|
||||
PersonList as PersonListProto,
|
||||
Game as GameProto,
|
||||
GetGameInfoRequest,
|
||||
GameInfoResponse,
|
||||
} from "../items";
|
||||
import { apiFetch } from "./api";
|
||||
import "./GameFilter.css";
|
||||
import { useGameFilter } from "./hooks/useGameFilter";
|
||||
import { PersonSelector } from "./components/PersonSelector";
|
||||
import { FilteredGamesList } from "./components/FilteredGamesList";
|
||||
import { Link } from "react-router-dom";
|
||||
import { GameImage } from "./GameImage";
|
||||
import "./GameFilter.css"
|
||||
|
||||
export function GameFilter() {
|
||||
const [people, setPeople] = useState<Person[]>([]);
|
||||
const [selectedPeople, setSelectedPeople] = useState<Set<string>>(new Set());
|
||||
|
||||
const { filteredGames, gameToPositive } = useGameFilter(
|
||||
people,
|
||||
selectedPeople
|
||||
);
|
||||
const [filteredGames, setFilteredGames] = useState<string[]>([]);
|
||||
const [gameToPositive, setGameToPositive] = useState<
|
||||
Map<string, Set<string>>
|
||||
>(new Map());
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const [metaData, _setMetaData] = useState<{ [key: string]: GameProto }>({});
|
||||
|
||||
useEffect(() => {
|
||||
apiFetch("/api")
|
||||
@ -25,6 +31,83 @@ export function GameFilter() {
|
||||
.catch((err) => console.error("Failed to fetch people:", err));
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedPeople.size === 0) {
|
||||
// eslint-disable-next-line react-hooks/set-state-in-effect
|
||||
setFilteredGames([]);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get all games where ALL selected people have "Would Play"
|
||||
const selectedPersons = people.filter((p) => selectedPeople.has(p.name));
|
||||
|
||||
if (selectedPersons.length === 0) {
|
||||
setFilteredGames([]);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create a map of game -> set of people who would not play it
|
||||
const gameToNegative = new Map<string, Set<string>>();
|
||||
const gameToPositiveOpinion = new Map<string, Set<string>>();
|
||||
|
||||
selectedPersons.forEach((person) => {
|
||||
person.opinion.forEach((op) => {
|
||||
if (!gameToNegative.has(op.title)) {
|
||||
gameToNegative.set(op.title, new Set());
|
||||
}
|
||||
if (!gameToPositiveOpinion.has(op.title)) {
|
||||
gameToPositiveOpinion.set(op.title, new Set());
|
||||
}
|
||||
if (!op.wouldPlay) {
|
||||
gameToNegative.get(op.title)!.add(person.name);
|
||||
}
|
||||
if (op.wouldPlay) {
|
||||
gameToPositiveOpinion.get(op.title)!.add(person.name);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
setGameToPositive(gameToPositiveOpinion);
|
||||
|
||||
// Filter games where ALL selected people would play
|
||||
const game_titles = Array.from(gameToNegative.entries())
|
||||
.filter(([, players]) => players.size === 0)
|
||||
.map(([game]) => game);
|
||||
|
||||
let games = game_titles.filter((title) => metaData[title]).map((title) => metaData[title]);
|
||||
const gamesToFetch = GetGameInfoRequest.encode(
|
||||
GetGameInfoRequest.create({
|
||||
games: game_titles.filter((title) => !metaData[title]),
|
||||
})
|
||||
).finish();
|
||||
|
||||
apiFetch("/api/games/batch", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/octet-stream",
|
||||
},
|
||||
body: gamesToFetch,
|
||||
})
|
||||
.then((res) => res.arrayBuffer())
|
||||
.then((buffer) => {
|
||||
const list = GameInfoResponse.decode(new Uint8Array(buffer));
|
||||
games = games.concat(list.games);
|
||||
|
||||
games.forEach((game) => {
|
||||
metaData[game.title] = game;
|
||||
});
|
||||
|
||||
const filteredGames = games.filter((g) => {
|
||||
const game = g as GameProto;
|
||||
return (
|
||||
game.maxPlayers >= selectedPeople.size &&
|
||||
game.minPlayers <= selectedPeople.size
|
||||
);
|
||||
});
|
||||
setFilteredGames(filteredGames.map((g) => (g as GameProto).title));
|
||||
});
|
||||
}, [selectedPeople, people, metaData]);
|
||||
|
||||
const togglePerson = (name: string) => {
|
||||
const newSelected = new Set(selectedPeople);
|
||||
if (newSelected.has(name)) {
|
||||
@ -42,17 +125,118 @@ export function GameFilter() {
|
||||
Select multiple people to find games that everyone would play
|
||||
</p>
|
||||
|
||||
<PersonSelector
|
||||
people={people}
|
||||
selectedPeople={selectedPeople}
|
||||
onTogglePerson={togglePerson}
|
||||
/>
|
||||
<div style={{ marginBottom: "3rem" }}>
|
||||
<h3>Select People</h3>
|
||||
<div
|
||||
className="grid-container"
|
||||
style={{
|
||||
display: "flex",
|
||||
flexWrap: "wrap",
|
||||
gap: "1rem",
|
||||
justifyContent: "center",
|
||||
}}
|
||||
>
|
||||
{people.map((person) => (
|
||||
<div
|
||||
key={person.name}
|
||||
className="list-item gamefilter-entry"
|
||||
style={{
|
||||
borderColor: selectedPeople.has(person.name)
|
||||
? "var(--accent-color)"
|
||||
: "var(--border-color)",
|
||||
}}
|
||||
onClick={() => togglePerson(person.name)}
|
||||
>
|
||||
<div style={{ gap: "0.5rem" }}>
|
||||
<strong>{person.name}</strong>
|
||||
</div>
|
||||
<div
|
||||
style={{
|
||||
fontSize: "0.9em",
|
||||
color: "var(--text-muted)",
|
||||
marginTop: "0.5rem",
|
||||
}}
|
||||
>
|
||||
{person.opinion.length} opinion(s)
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<FilteredGamesList
|
||||
filteredGames={filteredGames}
|
||||
gameToPositive={gameToPositive}
|
||||
selectedPeopleCount={selectedPeople.size}
|
||||
/>
|
||||
{selectedPeople.size > 0 && (
|
||||
<div>
|
||||
<h3>Games Everyone Would Play ({filteredGames.length})</h3>
|
||||
{filteredGames.length > 0 ? (
|
||||
<ul className="grid-container">
|
||||
{filteredGames.map((game) => (
|
||||
<Link
|
||||
to={`/game/${encodeURIComponent(game)}`}
|
||||
key={game}
|
||||
className="list-item game-entry"
|
||||
style={{
|
||||
textDecoration: "none",
|
||||
color: "inherit",
|
||||
display: "flex",
|
||||
justifyContent: "space-between",
|
||||
alignItems: "center",
|
||||
}}
|
||||
>
|
||||
<div>
|
||||
<strong>{game}</strong>
|
||||
<div
|
||||
style={{
|
||||
fontSize: "0.9em",
|
||||
color: "#4caf50",
|
||||
marginTop: "0.5rem",
|
||||
}}
|
||||
>
|
||||
<span>✓</span> {gameToPositive.get(game)!.size} selected
|
||||
would play
|
||||
</div>
|
||||
{selectedPeople.size - gameToPositive.get(game)!.size >
|
||||
0 && (
|
||||
<div
|
||||
style={{
|
||||
fontSize: "0.9em",
|
||||
color: "#d4d400",
|
||||
marginTop: "0.3rem",
|
||||
}}
|
||||
>
|
||||
<span>?</span>{" "}
|
||||
{selectedPeople.size - gameToPositive.get(game)!.size}{" "}
|
||||
{selectedPeople.size - gameToPositive.get(game)!.size >
|
||||
1
|
||||
? "are"
|
||||
: "is"}{" "}
|
||||
neutral
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<GameImage game={game} />
|
||||
</Link>
|
||||
))}
|
||||
</ul>
|
||||
) : (
|
||||
<div
|
||||
style={{
|
||||
padding: "3rem",
|
||||
textAlign: "center",
|
||||
background: "var(--secondary-alt-bg)",
|
||||
borderRadius: "16px",
|
||||
border: "1px dashed var(--border-color)",
|
||||
color: "var(--text-muted)",
|
||||
}}
|
||||
>
|
||||
<div style={{ fontSize: "2rem", marginBottom: "1rem" }}>🔍</div>
|
||||
<p>No games found where all selected people would play.</p>
|
||||
<p style={{ fontSize: "0.9rem" }}>
|
||||
Try selecting fewer people or adding more opinions!
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@ -1,94 +0,0 @@
|
||||
import { Link } from "react-router-dom";
|
||||
import { GameImage } from "../GameImage";
|
||||
|
||||
interface FilteredGamesListProps {
|
||||
filteredGames: string[];
|
||||
gameToPositive: Map<string, Set<string>>;
|
||||
selectedPeopleCount: number;
|
||||
}
|
||||
|
||||
export function FilteredGamesList({
|
||||
filteredGames,
|
||||
gameToPositive,
|
||||
selectedPeopleCount,
|
||||
}: FilteredGamesListProps) {
|
||||
if (selectedPeopleCount === 0) return null;
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h3>Games Everyone Would Play ({filteredGames.length})</h3>
|
||||
{filteredGames.length > 0 ? (
|
||||
<ul className="grid-container">
|
||||
{filteredGames.map((game) => {
|
||||
const positiveCount = gameToPositive.get(game)?.size || 0;
|
||||
const neutralCount = selectedPeopleCount - positiveCount;
|
||||
|
||||
return (
|
||||
<Link
|
||||
to={`/game/${encodeURIComponent(game)}`}
|
||||
key={game}
|
||||
className="list-item game-entry"
|
||||
style={{
|
||||
textDecoration: "none",
|
||||
color: "inherit",
|
||||
display: "flex",
|
||||
justifyContent: "space-between",
|
||||
alignItems: "center",
|
||||
}}
|
||||
>
|
||||
<div>
|
||||
<strong>{game}</strong>
|
||||
<div
|
||||
style={{
|
||||
fontSize: "0.9em",
|
||||
color: "#4caf50",
|
||||
marginTop: "0.5rem",
|
||||
}}
|
||||
>
|
||||
<span>✓</span> {positiveCount} selected would play
|
||||
</div>
|
||||
{neutralCount > 0 && (
|
||||
<div
|
||||
style={{
|
||||
fontSize: "0.9em",
|
||||
color: "#d4d400",
|
||||
marginTop: "0.3rem",
|
||||
}}
|
||||
>
|
||||
<span>?</span> {neutralCount}{" "}
|
||||
{neutralCount > 1 ? "are" : "is"} neutral
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<GameImage game={game} />
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
) : (
|
||||
<EmptyState />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function EmptyState() {
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
padding: "3rem",
|
||||
textAlign: "center",
|
||||
background: "var(--secondary-alt-bg)",
|
||||
borderRadius: "16px",
|
||||
border: "1px dashed var(--border-color)",
|
||||
color: "var(--text-muted)",
|
||||
}}
|
||||
>
|
||||
<div style={{ fontSize: "2rem", marginBottom: "1rem" }}>🔍</div>
|
||||
<p>No games found where all selected people would play.</p>
|
||||
<p style={{ fontSize: "0.9rem" }}>
|
||||
Try selecting fewer people or adding more opinions!
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -1,55 +0,0 @@
|
||||
import { Person } from "../../items";
|
||||
|
||||
interface PersonSelectorProps {
|
||||
people: Person[];
|
||||
selectedPeople: Set<string>;
|
||||
onTogglePerson: (name: string) => void;
|
||||
}
|
||||
|
||||
export function PersonSelector({
|
||||
people,
|
||||
selectedPeople,
|
||||
onTogglePerson,
|
||||
}: PersonSelectorProps) {
|
||||
return (
|
||||
<div style={{ marginBottom: "3rem" }}>
|
||||
<h3>Select People</h3>
|
||||
<div
|
||||
className="grid-container"
|
||||
style={{
|
||||
display: "flex",
|
||||
flexWrap: "wrap",
|
||||
gap: "1rem",
|
||||
justifyContent: "center",
|
||||
}}
|
||||
>
|
||||
{people.map((person) => (
|
||||
<div
|
||||
key={person.name}
|
||||
className="list-item gamefilter-entry"
|
||||
style={{
|
||||
borderColor: selectedPeople.has(person.name)
|
||||
? "var(--accent-color)"
|
||||
: "var(--border-color)",
|
||||
cursor: "pointer",
|
||||
}}
|
||||
onClick={() => onTogglePerson(person.name)}
|
||||
>
|
||||
<div style={{ gap: "0.5rem" }}>
|
||||
<strong>{person.name}</strong>
|
||||
</div>
|
||||
<div
|
||||
style={{
|
||||
fontSize: "0.9em",
|
||||
color: "var(--text-muted)",
|
||||
marginTop: "0.5rem",
|
||||
}}
|
||||
>
|
||||
{person.opinion.length} opinion(s)
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -1,98 +0,0 @@
|
||||
import { useState, useEffect, useRef, useMemo } from "react";
|
||||
import {
|
||||
Person,
|
||||
Game as GameProto,
|
||||
GetGameInfoRequest,
|
||||
GameInfoResponse,
|
||||
} from "../../items";
|
||||
import { apiFetch } from "../api";
|
||||
|
||||
export function useGameFilter(people: Person[], selectedPeople: Set<string>) {
|
||||
const [fetchedTitles, setFetchedTitles] = useState<string[]>([]);
|
||||
const metaDataRef = useRef<{ [key: string]: GameProto }>({});
|
||||
|
||||
const { gameToNegative, gameToPositiveOpinion } = useMemo(() => {
|
||||
const gameToNegative = new Map<string, Set<string>>();
|
||||
const gameToPositiveOpinion = new Map<string, Set<string>>();
|
||||
|
||||
if (selectedPeople.size === 0)
|
||||
return { gameToNegative, gameToPositiveOpinion };
|
||||
|
||||
const selectedPersons = people.filter((p) => selectedPeople.has(p.name));
|
||||
selectedPersons.forEach((person) => {
|
||||
person.opinion.forEach((op) => {
|
||||
if (!gameToNegative.has(op.title))
|
||||
gameToNegative.set(op.title, new Set());
|
||||
if (!gameToPositiveOpinion.has(op.title))
|
||||
gameToPositiveOpinion.set(op.title, new Set());
|
||||
|
||||
if (!op.wouldPlay) {
|
||||
gameToNegative.get(op.title)!.add(person.name);
|
||||
} else {
|
||||
gameToPositiveOpinion.get(op.title)!.add(person.name);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return { gameToNegative, gameToPositiveOpinion };
|
||||
}, [people, selectedPeople]);
|
||||
|
||||
const titlesEveryoneWouldPlay = useMemo(() => {
|
||||
return Array.from(gameToNegative.entries())
|
||||
.filter(([, players]) => players.size === 0)
|
||||
.map(([game]) => game);
|
||||
}, [gameToNegative]);
|
||||
|
||||
useEffect(() => {
|
||||
const titlesToFetch = titlesEveryoneWouldPlay.filter(
|
||||
(title) => !metaDataRef.current[title]
|
||||
);
|
||||
if (titlesToFetch.length === 0) return;
|
||||
|
||||
const gamesToFetch = GetGameInfoRequest.encode(
|
||||
GetGameInfoRequest.create({
|
||||
games: titlesToFetch,
|
||||
})
|
||||
).finish();
|
||||
|
||||
apiFetch("/api/games/batch", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/octet-stream" },
|
||||
body: gamesToFetch,
|
||||
})
|
||||
.then((res) => res.arrayBuffer())
|
||||
.then((buffer) => {
|
||||
const list = GameInfoResponse.decode(new Uint8Array(buffer));
|
||||
list.games.forEach((game) => {
|
||||
metaDataRef.current[game.title] = game;
|
||||
});
|
||||
// Trigger a re-render to update filteredGames
|
||||
setFetchedTitles([...titlesToFetch]);
|
||||
})
|
||||
.catch((err) => console.error("Failed to fetch game metadata:", err));
|
||||
}, [titlesEveryoneWouldPlay]);
|
||||
|
||||
const filteredGames = useMemo(() => {
|
||||
if (selectedPeople.size === 0) return [];
|
||||
|
||||
const games = titlesEveryoneWouldPlay
|
||||
.filter((title) => metaDataRef.current[title])
|
||||
.map((title) => metaDataRef.current[title]);
|
||||
|
||||
return filterByPlayerCount(games, selectedPeople.size);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [titlesEveryoneWouldPlay, selectedPeople.size, fetchedTitles]);
|
||||
|
||||
return { filteredGames, gameToPositive: gameToPositiveOpinion };
|
||||
}
|
||||
|
||||
function filterByPlayerCount(
|
||||
games: GameProto[],
|
||||
playerCount: number
|
||||
): string[] {
|
||||
return games
|
||||
.filter(
|
||||
(game) => game.maxPlayers >= playerCount && game.minPlayers <= playerCount
|
||||
)
|
||||
.map((game) => game.title);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user