@@ -515,8 +515,25 @@ export default function AnalyzePage() {
515515 setVideoInfo ( null ) ;
516516 }
517517
518- setTopics ( hydratedTopics ) ;
519- setBaseTopics ( hydratedTopics ) ;
518+ // Separate base topics and theme topics
519+ const baseTopicsFromCache = hydratedTopics . filter ( topic => ! topic . theme ) ;
520+ const themeTopicsFromCache = hydratedTopics . filter ( topic => topic . theme ) ;
521+
522+ // Reconstruct themeTopicsMap
523+ const reconstructedThemeMap : Record < string , Topic [ ] > = { } ;
524+ themeTopicsFromCache . forEach ( topic => {
525+ if ( topic . theme ) {
526+ if ( ! reconstructedThemeMap [ topic . theme ] ) {
527+ reconstructedThemeMap [ topic . theme ] = [ ] ;
528+ }
529+ reconstructedThemeMap [ topic . theme ] . push ( topic ) ;
530+ }
531+ } ) ;
532+
533+ setTopics ( baseTopicsFromCache ) ;
534+ setBaseTopics ( baseTopicsFromCache ) ;
535+ setThemeTopicsMap ( reconstructedThemeMap ) ;
536+
520537 const initialKeys = new Set < string > ( ) ;
521538 hydratedTopics . forEach ( topic => {
522539 if ( topic . quote ?. timestamp && topic . quote . text ) {
@@ -525,7 +542,7 @@ export default function AnalyzePage() {
525542 }
526543 } ) ;
527544 setUsedTopicKeys ( initialKeys ) ;
528- setSelectedTopic ( hydratedTopics . length > 0 ? hydratedTopics [ 0 ] : null ) ;
545+ setSelectedTopic ( baseTopicsFromCache . length > 0 ? baseTopicsFromCache [ 0 ] : null ) ;
529546
530547 // Set cached takeaways and questions
531548 if ( cacheData . summary ) {
@@ -1293,11 +1310,41 @@ export default function AnalyzePage() {
12931310 }
12941311 } ) ;
12951312 setUsedTopicKeys ( nextUsedKeys ) ;
1296- themedTopics = hydratedThemeTopics ;
1313+
1314+ // Tag theme topics with theme name
1315+ const themedTopicsWithTheme = hydratedThemeTopics . map ( topic => ( {
1316+ ...topic ,
1317+ theme : normalizedTheme
1318+ } ) ) ;
1319+
1320+ themedTopics = themedTopicsWithTheme ;
12971321 setThemeTopicsMap ( prev => ( {
12981322 ...prev ,
12991323 [ normalizedTheme ] : themedTopics || [ ]
13001324 } ) ) ;
1325+
1326+ // Save theme topics to database (background operation)
1327+ backgroundOperation (
1328+ 'save-theme-topics' ,
1329+ async ( ) => {
1330+ const allTopics = [
1331+ ...baseTopics . map ( t => ( { ...t , theme : null } ) ) ,
1332+ ...Object . entries ( themeTopicsMap ) . flatMap ( ( [ theme , topics ] ) =>
1333+ topics . map ( t => ( { ...t , theme } ) )
1334+ ) ,
1335+ ...themedTopicsWithTheme
1336+ ] ;
1337+
1338+ await fetch ( "/api/update-video-analysis" , {
1339+ method : "POST" ,
1340+ headers : { "Content-Type" : "application/json" } ,
1341+ body : JSON . stringify ( {
1342+ videoId,
1343+ topics : allTopics
1344+ } )
1345+ } ) ;
1346+ }
1347+ ) ;
13011348 } catch ( error ) {
13021349 const isAbortError =
13031350 typeof error === "object" &&
0 commit comments