1
- import { createContext , useContext } from 'react' ;
1
+ import React , { createContext , useCallback , useContext , useEffect , useState } from 'react' ;
2
2
import { AppContext , appErrorContext } from '../../common/appContext' ;
3
3
import { CmsContent } from '../../common/cms-documents/content' ;
4
4
import { CmsCategoryListItem } from '../../common/cms-documents/category' ;
@@ -13,7 +13,6 @@ type AppState = {
13
13
setContentSelectorOpen : ( isOpen : boolean ) => void ;
14
14
} ;
15
15
16
- // eslint-disable-next-line react-refresh/only-export-components
17
16
const AppStateContext = createContext < AppState > ( {
18
17
appContext : appErrorContext ,
19
18
selectedContent : null ,
@@ -25,24 +24,55 @@ const AppStateContext = createContext<AppState>({
25
24
} ) ;
26
25
27
26
export const useAppState = ( ) => {
28
- const {
29
- appContext,
30
- selectedContent,
31
- setSelectedContent,
32
- selectedCategory,
33
- setSelectedCategory,
34
- contentSelectorOpen,
35
- setContentSelectorOpen,
36
- } = useContext ( AppStateContext ) ;
37
-
38
- return {
39
- AppStateProvider : AppStateContext . Provider ,
40
- appContext,
41
- selectedContent,
42
- setSelectedContent,
43
- selectedCategory,
44
- setSelectedCategory,
45
- contentSelectorOpen,
46
- setContentSelectorOpen,
47
- } ;
27
+ return useContext ( AppStateContext ) ;
28
+ } ;
29
+
30
+ type ProviderProps = {
31
+ appContext : AppContext ;
32
+ children : React . ReactNode ;
33
+ } ;
34
+
35
+ export const AppStateProvider = ( { appContext, children } : ProviderProps ) => {
36
+ const [ selectedContent , setSelectedContent ] = useState < CmsContent | null > ( null ) ;
37
+ const [ selectedCategory , setSelectedCategory ] = useState < CmsCategoryListItem | null > ( null ) ;
38
+ const [ contentSelectorOpen , setContentSelectorOpen ] = useState < boolean > ( false ) ;
39
+
40
+ const setSelectedContentWithHistory = useCallback (
41
+ ( content : CmsContent | null , toHistory : boolean = true ) => {
42
+ setSelectedContent ( content ) ;
43
+ if ( toHistory ) {
44
+ window . history . pushState (
45
+ { ...window . history . state , content } ,
46
+ '' ,
47
+ `${ appContext . basePath } /${ content ? content . versionKey : '' } `
48
+ ) ;
49
+ }
50
+ } ,
51
+ [ appContext ]
52
+ ) ;
53
+
54
+ useEffect ( ( ) => {
55
+ const onPopState = ( e : PopStateEvent ) => {
56
+ setSelectedContentWithHistory ( e . state ?. content || null , false ) ;
57
+ } ;
58
+
59
+ window . addEventListener ( 'popstate' , onPopState ) ;
60
+ return ( ) => window . removeEventListener ( 'popstate' , onPopState ) ;
61
+ } , [ setSelectedContentWithHistory ] ) ;
62
+
63
+ return (
64
+ < AppStateContext . Provider
65
+ value = { {
66
+ appContext,
67
+ selectedContent,
68
+ setSelectedContent : setSelectedContentWithHistory ,
69
+ selectedCategory,
70
+ setSelectedCategory,
71
+ contentSelectorOpen,
72
+ setContentSelectorOpen,
73
+ } }
74
+ >
75
+ { children }
76
+ </ AppStateContext . Provider >
77
+ ) ;
48
78
} ;
0 commit comments