@@ -15,6 +15,7 @@ import {
15
15
defaultPathIndex ,
16
16
defaultViewModes ,
17
17
} from "../lib/const" ;
18
+
18
19
export type SourceType = Exclude < Options [ "sourceType" ] , undefined > ;
19
20
export type Version = Exclude < Options [ "ecmaVersion" ] , undefined > ;
20
21
export type Language = "javascript" | "json" | "markdown" | "css" | "html" ;
@@ -102,20 +103,23 @@ type ExplorerState = {
102
103
setEsquerySelector : ( esquerySelector : EsquerySelector ) => void ;
103
104
} ;
104
105
106
+ const getHashParams = ( ) : URLSearchParams => {
107
+ return new URLSearchParams ( location . hash . slice ( 1 ) ) ;
108
+ } ;
109
+
105
110
const hashStorage : StateStorage = {
106
111
getItem : ( key ) : string => {
107
- const searchParams = new URLSearchParams ( location . hash . slice ( 1 ) ) ;
108
- const storedValue = searchParams . get ( key ) ?? "" ;
112
+ const storedValue = getHashParams ( ) . get ( key ) ?? "" ;
109
113
return storedValue ? JSON . parse ( atob ( storedValue ) ) : "" ;
110
114
} ,
111
115
setItem : ( key , newValue ) : void => {
112
- const searchParams = new URLSearchParams ( location . hash . slice ( 1 ) ) ;
116
+ const searchParams = getHashParams ( ) ;
113
117
const encodedValue = btoa ( JSON . stringify ( newValue ) ) ;
114
118
searchParams . set ( key , encodedValue ) ;
115
119
location . hash = searchParams . toString ( ) ;
116
120
} ,
117
121
removeItem : ( key ) : void => {
118
- const searchParams = new URLSearchParams ( location . hash . slice ( 1 ) ) ;
122
+ const searchParams = getHashParams ( ) ;
119
123
searchParams . delete ( key ) ;
120
124
location . hash = searchParams . toString ( ) ;
121
125
} ,
@@ -170,6 +174,26 @@ export const useExplorer = create<ExplorerState>()(
170
174
) ,
171
175
{
172
176
name : "eslint-explorer" ,
177
+ onRehydrateStorage : ( ) => state => {
178
+ if ( ! state ) return ;
179
+
180
+ let needsPatching = false ;
181
+ const patchedCode = { ...defaultCode } ;
182
+
183
+ ( Object . keys ( defaultCode ) as ( keyof Code ) [ ] ) . forEach (
184
+ key => {
185
+ if ( state . code && key in state . code ) {
186
+ patchedCode [ key ] = state . code [ key ] ;
187
+ } else {
188
+ needsPatching = true ;
189
+ }
190
+ } ,
191
+ ) ;
192
+
193
+ if ( needsPatching ) {
194
+ state . setCode ( patchedCode ) ;
195
+ }
196
+ } ,
173
197
} ,
174
198
) ,
175
199
) ,
0 commit comments