|
1 | 1 | import { create } from "zustand"; |
2 | | -import { devtools, persist } from "zustand/middleware"; |
| 2 | +import { |
| 3 | + devtools, |
| 4 | + persist, |
| 5 | + StateStorage, |
| 6 | + createJSONStorage, |
| 7 | +} from "zustand/middleware"; |
3 | 8 | import type { Options } from "espree"; |
4 | 9 | import { |
5 | 10 | defaultCode, |
@@ -71,37 +76,63 @@ type ExplorerState = { |
71 | 76 | setPathIndex: (pathIndex: PathIndex) => void; |
72 | 77 | }; |
73 | 78 |
|
| 79 | +const hashStorage: StateStorage = { |
| 80 | + getItem: (key): string => { |
| 81 | + const searchParams = new URLSearchParams(location.hash.slice(1)); |
| 82 | + const storedValue = searchParams.get(key) ?? ""; |
| 83 | + return storedValue ? JSON.parse(atob(storedValue)) : ""; |
| 84 | + }, |
| 85 | + setItem: (key, newValue): void => { |
| 86 | + const searchParams = new URLSearchParams(location.hash.slice(1)); |
| 87 | + const encodedValue = btoa(JSON.stringify(newValue)); |
| 88 | + searchParams.set(key, encodedValue); |
| 89 | + location.hash = searchParams.toString(); |
| 90 | + }, |
| 91 | + removeItem: (key): void => { |
| 92 | + const searchParams = new URLSearchParams(location.hash.slice(1)); |
| 93 | + searchParams.delete(key); |
| 94 | + location.hash = searchParams.toString(); |
| 95 | + }, |
| 96 | +}; |
| 97 | + |
74 | 98 | export const useExplorer = create<ExplorerState>()( |
75 | 99 | devtools( |
76 | 100 | persist( |
77 | | - set => ({ |
78 | | - tool: "ast", |
79 | | - setTool: tool => set({ tool }), |
| 101 | + persist( |
| 102 | + set => ({ |
| 103 | + tool: "ast", |
| 104 | + setTool: tool => set({ tool }), |
80 | 105 |
|
81 | | - code: defaultCode, |
82 | | - setCode: code => set({ code }), |
| 106 | + code: defaultCode, |
| 107 | + setCode: code => set({ code }), |
83 | 108 |
|
84 | | - language: "javascript", |
85 | | - setLanguage: language => set({ language }), |
| 109 | + language: "javascript", |
| 110 | + setLanguage: language => set({ language }), |
86 | 111 |
|
87 | | - jsOptions: defaultJsOptions, |
88 | | - setJsOptions: jsOptions => set({ jsOptions }), |
| 112 | + jsOptions: defaultJsOptions, |
| 113 | + setJsOptions: jsOptions => set({ jsOptions }), |
89 | 114 |
|
90 | | - jsonOptions: defaultJsonOptions, |
91 | | - setJsonOptions: jsonOptions => set({ jsonOptions }), |
| 115 | + jsonOptions: defaultJsonOptions, |
| 116 | + setJsonOptions: jsonOptions => set({ jsonOptions }), |
92 | 117 |
|
93 | | - markdownOptions: defaultMarkdownOptions, |
94 | | - setMarkdownOptions: markdownOptions => set({ markdownOptions }), |
| 118 | + markdownOptions: defaultMarkdownOptions, |
| 119 | + setMarkdownOptions: markdownOptions => |
| 120 | + set({ markdownOptions }), |
95 | 121 |
|
96 | | - wrap: true, |
97 | | - setWrap: wrap => set({ wrap }), |
| 122 | + wrap: true, |
| 123 | + setWrap: wrap => set({ wrap }), |
98 | 124 |
|
99 | | - viewModes: defaultViewModes, |
100 | | - setViewModes: viewModes => set({ viewModes }), |
| 125 | + viewModes: defaultViewModes, |
| 126 | + setViewModes: viewModes => set({ viewModes }), |
101 | 127 |
|
102 | | - pathIndex: defaultPathIndex, |
103 | | - setPathIndex: pathIndex => set({ pathIndex }), |
104 | | - }), |
| 128 | + pathIndex: defaultPathIndex, |
| 129 | + setPathIndex: pathIndex => set({ pathIndex }), |
| 130 | + }), |
| 131 | + { |
| 132 | + name: "eslint-explorer", |
| 133 | + storage: createJSONStorage(() => hashStorage), |
| 134 | + }, |
| 135 | + ), |
105 | 136 | { |
106 | 137 | name: "eslint-explorer", |
107 | 138 | }, |
|
0 commit comments