@@ -12,6 +12,7 @@ import { Tournament } from 'src/components'
1212import { AnalysisListContext } from 'src/contexts'
1313import { getAnalysisGameList } from 'src/api'
1414import { getCustomAnalysesAsWebGames } from 'src/lib/customAnalysis'
15+ import { AnalysisWebGame } from 'src/types'
1516import { useRouter } from 'next/router'
1617
1718interface GameData {
@@ -66,9 +67,15 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
6667 const [ currentPage , setCurrentPage ] = useState ( 1 )
6768 const [ totalPages , setTotalPages ] = useState ( 1 )
6869 const [ loading , setLoading ] = useState ( false )
69- const [ localPlayGames , setLocalPlayGames ] = useState ( analysisPlayList )
70- const [ localHandGames , setLocalHandGames ] = useState ( analysisHandList )
71- const [ localBrainGames , setLocalBrainGames ] = useState ( analysisBrainList )
70+
71+ const [ gamesByPage , setGamesByPage ] = useState < {
72+ [ gameType : string ] : { [ page : number ] : AnalysisWebGame [ ] }
73+ } > ( {
74+ play : { } ,
75+ hand : { } ,
76+ brain : { } ,
77+ } )
78+
7279 const [ customAnalyses , setCustomAnalyses ] = useState ( ( ) => {
7380 if ( typeof window !== 'undefined' ) {
7481 return getCustomAnalysesAsWebGames ( )
@@ -208,9 +215,14 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
208215 [ selected ] : calculatedTotalPages ,
209216 } ) )
210217
211- if ( selected === 'play' ) {
212- setLocalPlayGames ( parsedGames )
213- }
218+ setGamesByPage ( ( prev ) => ( {
219+ ...prev ,
220+ [ selected ] : {
221+ ...prev [ selected ] ,
222+ [ currentPage ] : parsedGames ,
223+ } ,
224+ } ) )
225+
214226 setLoading ( false )
215227 } )
216228 . catch ( ( ) => {
@@ -275,11 +287,14 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
275287 [ gameType ] : calculatedTotalPages ,
276288 } ) )
277289
278- if ( gameType === 'hand' ) {
279- setLocalHandGames ( parsedGames )
280- } else if ( gameType === 'brain' ) {
281- setLocalBrainGames ( parsedGames )
282- }
290+ setGamesByPage ( ( prev ) => ( {
291+ ...prev ,
292+ [ gameType ] : {
293+ ...prev [ gameType ] ,
294+ [ currentPage ] : parsedGames ,
295+ } ,
296+ } ) )
297+
283298 setLoading ( false )
284299 } )
285300 . catch ( ( ) => {
@@ -299,20 +314,23 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
299314 const gameType = hbSubsection === 'hand' ? 'hand' : 'brain'
300315 if ( totalPagesCache [ gameType ] ) {
301316 setTotalPages ( totalPagesCache [ gameType ] )
317+ } else {
318+ setTotalPages ( 1 )
302319 }
303- setCurrentPage ( currentPagePerTab [ gameType ] )
320+ setCurrentPage ( currentPagePerTab [ gameType ] || 1 )
304321 } else if ( totalPagesCache [ selected ] ) {
305322 setTotalPages ( totalPagesCache [ selected ] )
323+ setCurrentPage ( currentPagePerTab [ selected ] || 1 )
306324 } else if (
307325 selected === 'lichess' ||
308326 selected === 'tournament' ||
309327 selected === 'custom'
310328 ) {
311329 setTotalPages ( 1 )
312- }
313-
314- if ( selected !== 'hb' ) {
315- setCurrentPage ( currentPagePerTab [ selected ] )
330+ setCurrentPage ( 1 )
331+ } else {
332+ setTotalPages ( 1 )
333+ setCurrentPage ( currentPagePerTab [ selected ] || 1 )
316334 }
317335 } , [ selected , hbSubsection , totalPagesCache , currentPagePerTab ] )
318336
@@ -342,9 +360,10 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
342360
343361 const getCurrentGames = ( ) => {
344362 if ( selected === 'play' ) {
345- return localPlayGames
363+ return gamesByPage . play [ currentPage ] || [ ]
346364 } else if ( selected === 'hb' ) {
347- return hbSubsection === 'hand' ? localHandGames : localBrainGames
365+ const gameType = hbSubsection === 'hand' ? 'hand' : 'brain'
366+ return gamesByPage [ gameType ] ?. [ currentPage ] || [ ]
348367 } else if ( selected === 'custom' ) {
349368 return customAnalyses
350369 } else if ( selected === 'lichess' ) {
@@ -397,34 +416,32 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
397416 < div className = "flex border-b border-white border-opacity-10" >
398417 < button
399418 onClick = { ( ) => setHbSubsection ( 'hand' ) }
400- className = { `flex-1 px-3 py-2 text-sm ${
419+ className = { `flex-1 px-3 text-sm ${
401420 hbSubsection === 'hand'
402421 ? 'bg-background-2 text-primary'
403422 : 'bg-background-1/50 text-secondary hover:bg-background-2'
404423 } `}
405424 >
406- < div className = "flex items-center justify-center gap-2 " >
407- < span className = "material-symbols-outlined text-xs " >
425+ < div className = "flex items-center justify-center gap-1 " >
426+ < span className = "material-symbols-outlined ! text-lg " >
408427 hand_gesture
409428 </ span >
410- < span className = "text-xs" > Hand ( { localHandGames . length } ) </ span >
429+ < span className = "text-xs" > Hand</ span >
411430 </ div >
412431 </ button >
413432 < button
414433 onClick = { ( ) => setHbSubsection ( 'brain' ) }
415- className = { `flex-1 px-3 py-2 text-sm ${
434+ className = { `flex-1 px-3 text-sm ${
416435 hbSubsection === 'brain'
417436 ? 'bg-background-2 text-primary'
418437 : 'bg-background-1/50 text-secondary hover:bg-background-2'
419438 } `}
420439 >
421- < div className = "flex items-center justify-center gap-2" >
422- < span className = "material-symbols-outlined text-xs" >
423- psychology
424- </ span >
425- < span className = "text-xs" >
426- Brain ({ localBrainGames . length } )
440+ < div className = "flex items-center justify-center gap-1" >
441+ < span className = "material-symbols-outlined !text-lg" >
442+ neurology
427443 </ span >
444+ < span className = "text-xs" > Brain</ span >
428445 </ div >
429446 </ button >
430447 </ div >
0 commit comments