1
- import { useMemo } from 'react' ;
1
+ import { useMemo , useEffect } from 'react' ;
2
2
import useBaseUrl from '@docusaurus/useBaseUrl' ;
3
3
import useIsBrowser from '@docusaurus/useIsBrowser' ;
4
4
import { usePluginData } from '@docusaurus/useGlobalData' ;
@@ -9,6 +9,9 @@ import { AppStore, RedocRawOptions } from 'redoc';
9
9
import { SpecProps } from '../types/common' ;
10
10
import { GlobalData } from '../types/options' ;
11
11
12
+ // the current store singleton in the app's instance
13
+ let currentStore : AppStore | null = null ;
14
+
12
15
/**
13
16
* Redocusaurus
14
17
* https://rohit-gohri.github.io/redocusaurus/
@@ -27,7 +30,7 @@ export function useSpec(
27
30
themeId ,
28
31
) as GlobalData ;
29
32
30
- const stores = useMemo ( ( ) => {
33
+ const result = useMemo ( ( ) => {
31
34
const { lightTheme, darkTheme, options : redocOptions } = themeOptions ;
32
35
33
36
const commonOptions : Partial < RedocRawOptions > = {
@@ -38,48 +41,48 @@ export function useSpec(
38
41
: redocOptions . scrollYOffset ,
39
42
} ;
40
43
41
- const lightStore = new AppStore (
42
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
- spec as any ,
44
- fullUrl ,
45
- merge (
46
- {
47
- ...redocOptions ,
48
- ...commonOptions ,
49
- theme : lightTheme ,
50
- } ,
51
- optionsOverrides ,
52
- ) ,
44
+ const lightThemeOptions : RedocRawOptions = merge (
45
+ {
46
+ ...redocOptions ,
47
+ ...commonOptions ,
48
+ theme : lightTheme ,
49
+ } ,
50
+ optionsOverrides ,
51
+ ) ;
52
+
53
+ const darkThemeOptions : RedocRawOptions = merge (
54
+ {
55
+ ...redocOptions ,
56
+ ...commonOptions ,
57
+ theme : darkTheme ,
58
+ } ,
59
+ optionsOverrides ,
53
60
) ;
54
61
55
- const darkStore = new AppStore (
62
+ if ( currentStore !== null ) {
63
+ currentStore . dispose ( ) ;
64
+ }
65
+ currentStore = new AppStore (
56
66
// eslint-disable-next-line @typescript-eslint/no-explicit-any
57
67
spec as any ,
58
68
fullUrl ,
59
- merge (
60
- {
61
- ...redocOptions ,
62
- ...commonOptions ,
63
- theme : darkTheme ,
64
- } ,
65
- optionsOverrides ,
66
- ) ,
69
+ isBrowser && isDarkTheme ? darkThemeOptions : lightThemeOptions ,
67
70
) ;
68
71
69
72
return {
70
- lightStore,
71
- darkStore,
72
- } ;
73
- } , [ isBrowser , spec , fullUrl , themeOptions , optionsOverrides ] ) ;
74
-
75
- const result = useMemo ( ( ) => {
76
- return {
77
- ...stores ,
73
+ darkThemeOptions,
74
+ lightThemeOptions,
78
75
// @ts -expect-error extra prop
79
76
hasLogo : ! ! spec . info ?. [ 'x-logo' ] ,
80
- store : isBrowser && isDarkTheme ? stores . darkStore : stores . lightStore ,
77
+ store : currentStore ,
81
78
} ;
82
- } , [ isBrowser , isDarkTheme , spec , stores ] ) ;
79
+ } , [ isBrowser , spec , fullUrl , isDarkTheme , themeOptions , optionsOverrides ] ) ;
80
+
81
+ useEffect ( ( ) => {
82
+ // to ensure that menu is properly loaded when theme gets changed
83
+ // or when first load
84
+ result . store . onDidMount ( ) ;
85
+ } , [ result , isBrowser , isDarkTheme ] ) ;
83
86
84
87
return result ;
85
88
}
0 commit comments