@@ -15,6 +15,7 @@ import {
1515 defaultPathIndex ,
1616 defaultViewModes ,
1717} from "../lib/const" ;
18+
1819export type SourceType = Exclude < Options [ "sourceType" ] , undefined > ;
1920export type Version = Exclude < Options [ "ecmaVersion" ] , undefined > ;
2021export type Language = "javascript" | "json" | "markdown" | "css" | "html" ;
@@ -102,20 +103,23 @@ type ExplorerState = {
102103 setEsquerySelector : ( esquerySelector : EsquerySelector ) => void ;
103104} ;
104105
106+ const getHashParams = ( ) : URLSearchParams => {
107+ return new URLSearchParams ( location . hash . slice ( 1 ) ) ;
108+ } ;
109+
105110const hashStorage : StateStorage = {
106111 getItem : ( key ) : string => {
107- const searchParams = new URLSearchParams ( location . hash . slice ( 1 ) ) ;
108- const storedValue = searchParams . get ( key ) ?? "" ;
112+ const storedValue = getHashParams ( ) . get ( key ) ?? "" ;
109113 return storedValue ? JSON . parse ( atob ( storedValue ) ) : "" ;
110114 } ,
111115 setItem : ( key , newValue ) : void => {
112- const searchParams = new URLSearchParams ( location . hash . slice ( 1 ) ) ;
116+ const searchParams = getHashParams ( ) ;
113117 const encodedValue = btoa ( JSON . stringify ( newValue ) ) ;
114118 searchParams . set ( key , encodedValue ) ;
115119 location . hash = searchParams . toString ( ) ;
116120 } ,
117121 removeItem : ( key ) : void => {
118- const searchParams = new URLSearchParams ( location . hash . slice ( 1 ) ) ;
122+ const searchParams = getHashParams ( ) ;
119123 searchParams . delete ( key ) ;
120124 location . hash = searchParams . toString ( ) ;
121125 } ,
@@ -170,6 +174,26 @@ export const useExplorer = create<ExplorerState>()(
170174 ) ,
171175 {
172176 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+ } ,
173197 } ,
174198 ) ,
175199 ) ,
0 commit comments