fix scores flashing no scores found
All checks were successful
Deploy SSR / deploy (push) Successful in 1m11s

This commit is contained in:
Lee 2024-09-12 12:56:22 +01:00
parent aba0d4ba57
commit 70c27c87ad
3 changed files with 18 additions and 15 deletions

@ -52,7 +52,7 @@ export default async function Search({ params: { slug } }: Props) {
return ( return (
<div className="flex flex-col h-full w-full"> <div className="flex flex-col h-full w-full">
<PlayerData initalPlayerData={player} initialScoreData={scores} sort={sort} page={page} /> <PlayerData initialPlayerData={player} initialScoreData={scores} sort={sort} page={page} />
</div> </div>
); );
} }

@ -12,13 +12,13 @@ import PlayerScores from "./player-scores";
const REFRESH_INTERVAL = 5 * 60 * 1000; // 5 minutes const REFRESH_INTERVAL = 5 * 60 * 1000; // 5 minutes
type Props = { type Props = {
initalPlayerData: ScoreSaberPlayer; initialPlayerData: ScoreSaberPlayer;
initialScoreData?: ScoreSaberPlayerScoresPage; initialScoreData?: ScoreSaberPlayerScoresPage;
sort: ScoreSort; sort: ScoreSort;
page: number; page: number;
}; };
export default function PlayerData({ initalPlayerData, initialScoreData, sort, page }: Props) { export default function PlayerData({ initialPlayerData: initalPlayerData, initialScoreData, sort, page }: Props) {
let player = initalPlayerData; let player = initalPlayerData;
const { data, isLoading, isError } = useQuery({ const { data, isLoading, isError } = useQuery({
queryKey: ["player", player.id], queryKey: ["player", player.id],

@ -27,6 +27,7 @@ export default function PlayerScores({ initialScoreData, player, sort, page }: P
const [currentSort, setCurrentSort] = useState(sort); const [currentSort, setCurrentSort] = useState(sort);
const [currentPage, setCurrentPage] = useState(page); const [currentPage, setCurrentPage] = useState(page);
const [currentScores, setCurrentScores] = useState<ScoreSaberPlayerScoresPage | undefined>(initialScoreData);
const { const {
data: scores, data: scores,
@ -37,7 +38,6 @@ export default function PlayerScores({ initialScoreData, player, sort, page }: P
queryKey: ["playerScores", player.id, currentSort, currentPage], queryKey: ["playerScores", player.id, currentSort, currentPage],
queryFn: () => scoresaberFetcher.lookupPlayerScores(player.id, currentSort, currentPage), queryFn: () => scoresaberFetcher.lookupPlayerScores(player.id, currentSort, currentPage),
staleTime: 30 * 1000, // Cache data for 30 seconds staleTime: 30 * 1000, // Cache data for 30 seconds
initialData: initialScoreData,
}); });
const handleAnimation = useCallback(() => { const handleAnimation = useCallback(() => {
@ -45,6 +45,12 @@ export default function PlayerScores({ initialScoreData, player, sort, page }: P
controls.start({ x: 0, opacity: 1, transition: { duration: 0.25 } }); controls.start({ x: 0, opacity: 1, transition: { duration: 0.25 } });
}, [controls]); }, [controls]);
useEffect(() => {
if (scores) {
setCurrentScores(scores);
}
}, [scores]);
useEffect(() => { useEffect(() => {
if (scores) { if (scores) {
handleAnimation(); handleAnimation();
@ -64,18 +70,10 @@ export default function PlayerScores({ initialScoreData, player, sort, page }: P
} }
}; };
if (scores === undefined) { if (currentScores === undefined) {
return undefined; return undefined;
} }
if (isError) {
return (
<Card className="gap-2">
<p>Oopsies! Something went wrong.</p>
</Card>
);
}
return ( return (
<Card className="flex gap-4"> <Card className="flex gap-4">
<div className="flex items-center flex-row w-full gap-2 justify-center"> <div className="flex items-center flex-row w-full gap-2 justify-center">
@ -90,9 +88,14 @@ export default function PlayerScores({ initialScoreData, player, sort, page }: P
))} ))}
</div> </div>
<div className="text-center">
{isError && <p>Oopsies! Something went wrong.</p>}
{currentScores.playerScores.length === 0 && <p>No scores found. Invalid Page?</p>}
</div>
<motion.div animate={controls}> <motion.div animate={controls}>
<div className="grid min-w-full grid-cols-1 divide-y divide-border"> <div className="grid min-w-full grid-cols-1 divide-y divide-border">
{scores.playerScores.map((playerScore, index) => ( {currentScores.playerScores.map((playerScore, index) => (
<Score key={index} playerScore={playerScore} /> <Score key={index} playerScore={playerScore} />
))} ))}
</div> </div>
@ -101,7 +104,7 @@ export default function PlayerScores({ initialScoreData, player, sort, page }: P
<Pagination <Pagination
mobilePagination={width < 768} mobilePagination={width < 768}
page={currentPage} page={currentPage}
totalPages={Math.ceil(scores.metadata.total / scores.metadata.itemsPerPage)} totalPages={Math.ceil(currentScores.metadata.total / currentScores.metadata.itemsPerPage)}
loadingPage={isLoading ? currentPage : undefined} loadingPage={isLoading ? currentPage : undefined}
onPageChange={setCurrentPage} onPageChange={setCurrentPage}
/> />