Skip to content

Commit 701fbcd

Browse files
committed
Smarter dropdown search
1 parent 45f64ee commit 701fbcd

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

app/routes/compare-players.$player1Id.$player2Id.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,20 @@ export const loader = async ({ params }: LoaderFunctionArgs) => {
2727
const player1 = players.find((player) => player.id === player1Id);
2828
const player2 = players.find((player) => player.id === player2Id);
2929

30-
const playerOptions = players.map((player) => ({
31-
value: player.id,
32-
label: player.name,
33-
}));
30+
const playerOptions = players
31+
.sort((a, b) => {
32+
// Sort inactive players to the bottom
33+
if (a.inactive && !b.inactive) return 1;
34+
if (!a.inactive && b.inactive) return -1;
35+
// Then sort by number of games
36+
const aGames = a.matchesAsWinner.length + a.matchesAsLoser.length;
37+
const bGames = b.matchesAsWinner.length + b.matchesAsLoser.length;
38+
return bGames - aGames;
39+
})
40+
.map((player) => ({
41+
value: player.id,
42+
label: `${player.name} (${player.matchesAsWinner.length + player.matchesAsLoser.length} kamper)${player.inactive ? ' ❌' : ''}`,
43+
}));
3444

3545
const player1WinStats =
3646
player1 && player2 ? findPlayerWinStats(player1, player2) : undefined;

app/routes/profile.$profileId.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,31 @@ export default function Index() {
5555
.filter((player) => !player.inactive)
5656
.sort((p1, p2) => p2.currentTeamELO - p1.currentTeamELO);
5757

58-
const playerOptions = players.map((player) => ({
59-
value: player.id,
60-
label: player.name,
61-
}));
58+
const playerOptions = players
59+
.sort((a, b) => {
60+
// Sort inactive players to the bottom
61+
if (a.inactive && !b.inactive) return 1;
62+
if (!a.inactive && b.inactive) return -1;
63+
// Then sort by number of games
64+
const aGames = a.matchesAsWinner.length + a.matchesAsLoser.length;
65+
const bGames = b.matchesAsWinner.length + b.matchesAsLoser.length;
66+
return bGames - aGames;
67+
})
68+
.map((player) => ({
69+
value: player.id,
70+
label: `${player.name} (${player.matchesAsWinner.length + player.matchesAsLoser.length} kamper)${player.inactive ? ' ❌' : ''}`,
71+
}));
6272

6373
const numberOfWins = player ? player.matchesAsWinner.length : 0;
6474
const numberOfLosses = player ? player.matchesAsLoser.length : 0;
6575
const numberOfMatches = numberOfWins + numberOfLosses;
6676
const winPercentage = (numberOfWins / numberOfMatches) * 100;
6777

68-
const findLongestWinStreak = (eloLogs) => {
78+
interface EloLog {
79+
elo: number;
80+
}
81+
82+
const findLongestWinStreak = (eloLogs: EloLog[]): number => {
6983
if (eloLogs.length === 0) return 0;
7084

7185
const logs = [...eloLogs].reverse();

app/routes/team-profile.$teamId.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,24 @@ export default function Index() {
3535
);
3636
const topFiveTeamIds = teamsSortedOnELODesc.slice(0, 5).map((t) => t.id);
3737

38-
const teamOptions = teams.map((team) => ({
39-
value: team.id,
40-
label: team.name,
41-
}));
38+
const teamOptions = teams
39+
.sort((a, b) => {
40+
// Sort teams with inactive players to the bottom
41+
const aHasInactive = a.players.some((player) => player.inactive);
42+
const bHasInactive = b.players.some((player) => player.inactive);
43+
if (aHasInactive && !bHasInactive) return 1;
44+
if (!aHasInactive && bHasInactive) return -1;
45+
// Then sort by number of games
46+
const aMatches =
47+
a.teamMatchesAsWinner.length + a.teamMatchesAsLoser.length;
48+
const bMatches =
49+
b.teamMatchesAsWinner.length + b.teamMatchesAsLoser.length;
50+
return bMatches - aMatches;
51+
})
52+
.map((team) => ({
53+
value: team.id,
54+
label: `${team.players.map((p) => p.name).join(' & ')} (${team.teamMatchesAsWinner.length + team.teamMatchesAsLoser.length} kamper)${team.players.some((p) => p.inactive) ? ' ❌' : ''}`,
55+
}));
4256

4357
const numberOfWins = team ? team.teamMatchesAsWinner.length : 0;
4458
const numberOfLosses = team ? team.teamMatchesAsLoser.length : 0;

0 commit comments

Comments
 (0)