From f996243675927fd39f84fa4cefab2287620e8a32 Mon Sep 17 00:00:00 2001 From: saintnoodle <14948290+saintnoodle@users.noreply.github.com> Date: Tue, 25 Jun 2024 18:59:38 +0100 Subject: [PATCH] fix: react-query, same url, different data (#1118) --- .../components/sessions/SessionOverview.tsx | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/client/src/components/sessions/SessionOverview.tsx b/client/src/components/sessions/SessionOverview.tsx index 4377c3d36..0a8690b1a 100644 --- a/client/src/components/sessions/SessionOverview.tsx +++ b/client/src/components/sessions/SessionOverview.tsx @@ -26,6 +26,8 @@ import { GPT_CLIENT_IMPLEMENTATIONS } from "lib/game-implementations"; import SessionFolderRaiseBreakdown from "./SessionFolderRaiseBreakdown"; import SessionRaiseBreakdown from "./SessionRaiseBreakdown"; +type PBsData = { pbs: Array }; + export default function SessionOverview({ sessionData, setSessionData, @@ -51,12 +53,17 @@ export default function SessionOverview({ const [importantScores, setImportantScores] = useState(null); - const { data } = useApiQuery }>>( - Object.keys(gptConfig.scoreRatingAlgs).map( - (e) => - `/users/${reqUser.id}/games/${session.game}/${session.playtype}/pbs/best?alg=${e}` - ) - ); + const scoreRatingAlgsKeys = Object.keys(gptConfig.scoreRatingAlgs); + + const query = + scoreRatingAlgsKeys.length === 1 + ? `/users/${reqUser.id}/games/${session.game}/${session.playtype}/pbs/best?alg=${scoreRatingAlgsKeys[0]}` + : scoreRatingAlgsKeys.map( + (alg) => + `/users/${reqUser.id}/games/${session.game}/${session.playtype}/pbs/best?alg=${alg}` + ); + + const { data } = useApiQuery(query); useEffect(() => { if (!data) { @@ -66,14 +73,22 @@ export default function SessionOverview({ const important: Array = []; - for (const d of data) { - for (const pb of d.pbs.slice(0, MAX_SCORES)) { + const findImportant = (data: PBsData) => { + for (const pb of data.pbs.slice(0, MAX_SCORES)) { const didFind = pb.composedFrom.find((e) => session.scoreIDs.includes(e.scoreID)); if (didFind) { important.push(didFind.scoreID); } } + }; + + if (Array.isArray(data)) { + for (const d of data) { + findImportant(d); + } + } else { + findImportant(data); } setImportantScores(scoreDataset.filter((e) => important.includes(e.scoreID)));