Skip to content

Commit

Permalink
improve loading state of players
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Feb 4, 2024
1 parent 54e8079 commit bb6960c
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/components/databases/PlayerCard.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import { commands } from "@/bindings";
import { Player, PlayerGameInfo } from "@/utils/db";
import { unwrap } from "@/utils/invoke";
import { Loader } from "@mantine/core";
import { Center, Group, Loader, Paper, Stack, Text } from "@mantine/core";
import { useEffect, useState } from "react";
import PersonalPlayerCard from "../home/PersonalCard";
import useSWRImmutable from "swr/immutable";

function PlayerCard({ player, file }: { player: Player; file: string }) {
const [info, setInfo] = useState<PlayerGameInfo | null>(null);

useEffect(() => {
async function fetchGames() {
const games = await commands.getPlayersGameInfo(file, player.id);
setInfo(unwrap(games));
}
fetchGames();
}, [file, player]);
const { data: info, isLoading } = useSWRImmutable(
["player-game-info", file, player.id],
async ([key, file, id]) => {
const games = await commands.getPlayersGameInfo(file, id);
return unwrap(games);
},
);

return (
<>
{info ? (
<PersonalPlayerCard name={player.name} info={info} />
) : (
<Loader />
{isLoading && (
<Paper withBorder h="100%">
<Center h="100%">
<Stack align="center">
<Text fw="bold">Processing player data...</Text>
<Loader />
</Stack>
</Center>
</Paper>
)}
{info && <PersonalPlayerCard name={player.name} info={info} />}
</>
);
}
Expand Down

0 comments on commit bb6960c

Please sign in to comment.