@@ -12,7 +12,7 @@ import { Tournament } from 'src/components'
1212import { FavoriteModal } from 'src/components/Common/FavoriteModal'
1313import { AnalysisListContext } from 'src/contexts'
1414import { getAnalysisGameList } from 'src/api'
15- import { getCustomAnalysesAsWebGames } from 'src/lib/customAnalysis'
15+ import { ensureMigration } from 'src/lib/customAnalysis'
1616import {
1717 getFavoritesAsWebGames ,
1818 addFavoriteGame ,
@@ -87,14 +87,9 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
8787 hand : { } ,
8888 brain : { } ,
8989 favorites : { } ,
90+ custom : { } ,
9091 } )
9192
92- const [ customAnalyses , setCustomAnalyses ] = useState ( ( ) => {
93- if ( typeof window !== 'undefined' ) {
94- return getCustomAnalysesAsWebGames ( )
95- }
96- return [ ]
97- } )
9893 const [ favoriteGames , setFavoriteGames ] = useState < AnalysisWebGame [ ] > ( [ ] )
9994 const [ favoritedGameIds , setFavoritedGameIds ] = useState < Set < string > > ( new Set ( ) )
10095 const [ hbSubsection , setHbSubsection ] = useState < 'hand' | 'brain' > ( 'hand' )
@@ -106,7 +101,6 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
106101 } > ( { isOpen : false , game : null } )
107102
108103 useEffect ( ( ) => {
109- setCustomAnalyses ( getCustomAnalysesAsWebGames ( ) )
110104 // Load favorites asynchronously
111105 getFavoritesAsWebGames ( ) . then ( ( favorites ) => {
112106 setFavoriteGames ( favorites )
@@ -117,6 +111,12 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
117111 } )
118112 } , [ refreshTrigger ] )
119113
114+ useEffect ( ( ) => {
115+ ensureMigration ( ) . catch ( ( error ) => {
116+ console . warn ( 'Failed to migrate custom analyses:' , error )
117+ } )
118+ } , [ ] )
119+
120120 useEffect ( ( ) => {
121121 if ( currentId ?. [ 1 ] === 'custom' ) {
122122 setSelected ( 'custom' )
@@ -129,6 +129,7 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
129129 play : { } ,
130130 hand : { } ,
131131 brain : { } ,
132+ custom : { } ,
132133 lichess : { } ,
133134 tournament : { } ,
134135 favorites : { } ,
@@ -144,6 +145,7 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
144145 play : 1 ,
145146 hand : 1 ,
146147 brain : 1 ,
148+ custom : 1 ,
147149 lichess : 1 ,
148150 tournament : 1 ,
149151 favorites : 1 ,
@@ -193,11 +195,19 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
193195 setLoadingIndex ( null )
194196 } , [ selected ] )
195197
198+ useEffect ( ( ) => {
199+ if ( selected === 'custom' ) {
200+ setFetchedCache ( ( prev ) => ( {
201+ ...prev ,
202+ custom : { } ,
203+ } ) )
204+ }
205+ } , [ refreshTrigger , selected ] )
206+
196207 useEffect ( ( ) => {
197208 if (
198209 selected !== 'tournament' &&
199210 selected !== 'lichess' &&
200- selected !== 'custom' &&
201211 selected !== 'hb'
202212 ) {
203213 const isAlreadyFetched = fetchedCache [ selected ] ?. [ currentPage ]
@@ -227,19 +237,30 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
227237 } ) )
228238 } else {
229239 // Handle regular games response format
240+ let parsedGames
241+
242+ if ( selected === 'custom' ) {
243+ parsedGames = data . games . map ( ( game : any ) => ( {
244+ id : game . id ,
245+ label : game . name || 'Custom Game' ,
246+ result : '*' ,
247+ type : game . pgn ? 'custom-pgn' : 'custom-fen' ,
248+ pgn : game . pgn ,
249+ } ) )
250+ } else {
230251 const parse = (
231- game : {
232- game_id : string
233- maia_name : string
234- result : string
235- player_color : 'white' | 'black'
252+ game : {
253+ game_id : string
254+ maia_name : string
255+ result : string
256+ player_color : 'white' | 'black'
236257 is_favorited ?: boolean
237258 custom_name ?: string
238- } ,
239- type : string ,
240- ) => {
241- const raw = game . maia_name . replace ( '_kdd_' , ' ' )
242- const maia = raw . charAt ( 0 ) . toUpperCase ( ) + raw . slice ( 1 )
259+ } ,
260+ type : string ,
261+ ) => {
262+ const raw = game . maia_name . replace ( '_kdd_' , ' ' )
263+ const maia = raw . charAt ( 0 ) . toUpperCase ( ) + raw . slice ( 1 )
243264
244265 // Use custom name if available, otherwise generate default label
245266 const defaultLabel = game . player_color === 'white'
@@ -396,13 +417,12 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
396417 } else if ( totalPagesCache [ selected ] ) {
397418 setTotalPages ( totalPagesCache [ selected ] )
398419 setCurrentPage ( currentPagePerTab [ selected ] || 1 )
399- } else if (
400- selected === 'lichess' ||
401- selected === 'tournament' ||
402- selected === 'custom'
403- ) {
420+ } else if ( selected === 'lichess' || selected === 'tournament' ) {
404421 setTotalPages ( 1 )
405422 setCurrentPage ( 1 )
423+ } else if ( selected === 'custom' ) {
424+ setTotalPages ( totalPagesCache [ 'custom' ] || 1 )
425+ setCurrentPage ( currentPagePerTab [ 'custom' ] || 1 )
406426 } else {
407427 setTotalPages ( 1 )
408428 setCurrentPage ( currentPagePerTab [ selected ] || 1 )
@@ -538,7 +558,7 @@ export const AnalysisGameList: React.FC<AnalysisGameListProps> = ({
538558 const gameType = hbSubsection === 'hand' ? 'hand' : 'brain'
539559 return gamesByPage [ gameType ] ?. [ currentPage ] || [ ]
540560 } else if ( selected === 'custom' ) {
541- return customAnalyses
561+ return gamesByPage [ 'custom' ] ?. [ currentPage ] || [ ]
542562 } else if ( selected === 'lichess' ) {
543563 return analysisLichessList
544564 } else if ( selected === 'favorites' ) {
0 commit comments