|
1 | | -import { Tag, useRemoteConfigStore } from 'src/features/remoteConfig' |
2 | | -import { enhanceTags } from 'src/utils/DataEnhancement' |
| 1 | +import { Tag } from 'src/features/remoteConfig' |
| 2 | +import localStateStore from 'src/utils/LocalStateStorage' |
| 3 | +import { capitalize } from 'src/utils/String' |
3 | 4 | import { create } from 'zustand' |
4 | | -import { StateStorage, createJSONStorage, persist } from 'zustand/middleware' |
| 5 | +import { createJSONStorage, persist } from 'zustand/middleware' |
5 | 6 | import { |
6 | 7 | CardSettingsType, |
7 | 8 | DNDDuration, |
@@ -61,62 +62,6 @@ type UserPreferencesStoreActions = { |
61 | 62 | setAdvStatus: (status: boolean) => void |
62 | 63 | } |
63 | 64 |
|
64 | | -const defaultStorage: StateStorage = { |
65 | | - getItem: (name: string) => { |
66 | | - const item = window.localStorage.getItem(name) |
67 | | - if (!item) { |
68 | | - return null |
69 | | - } |
70 | | - |
71 | | - try { |
72 | | - let { |
73 | | - version, |
74 | | - state, |
75 | | - }: { |
76 | | - version: number |
77 | | - state: UserPreferencesState |
78 | | - } = JSON.parse(item) |
79 | | - |
80 | | - const newState = { |
81 | | - ...state, |
82 | | - } |
83 | | - if (version == 0) { |
84 | | - const MAP_OLD_TAGS: Record<string, string> = { |
85 | | - 'artificial-intelligence': 'artificial intelligence', |
86 | | - 'machine-learning': 'machine learning', |
87 | | - cpp: 'c++', |
88 | | - csharp: 'c#', |
89 | | - 'data-science': 'data science', |
90 | | - 'objective-c': 'objectivec', |
91 | | - } |
92 | | - |
93 | | - const stateTags = state.userSelectedTags as unknown as string[] |
94 | | - const newTags = stateTags.map((tag) => { |
95 | | - if (MAP_OLD_TAGS[tag]) { |
96 | | - return MAP_OLD_TAGS[tag] |
97 | | - } |
98 | | - return tag |
99 | | - }) |
100 | | - newState.userSelectedTags = enhanceTags(useRemoteConfigStore.getState(), newTags) |
101 | | - } |
102 | | - |
103 | | - return JSON.stringify({ state: newState, version }) |
104 | | - } catch (e) { |
105 | | - return null |
106 | | - } |
107 | | - }, |
108 | | - setItem: (name: string, value: string) => { |
109 | | - try { |
110 | | - window.localStorage.setItem(name, value) |
111 | | - } catch (e) { |
112 | | - window.localStorage.setItem(name, '') |
113 | | - } |
114 | | - }, |
115 | | - removeItem: (name: string) => { |
116 | | - window.localStorage.removeItem(name) |
117 | | - }, |
118 | | -} |
119 | | - |
120 | 65 | export const useUserPreferences = create( |
121 | 66 | persist<UserPreferencesState & UserPreferencesStoreActions>( |
122 | 67 | (set, get) => ({ |
@@ -237,16 +182,41 @@ export const useUserPreferences = create( |
237 | 182 | { |
238 | 183 | name: 'preferences_storage', |
239 | 184 | version: 1, |
240 | | - storage: createJSONStorage(() => defaultStorage), |
| 185 | + storage: createJSONStorage(() => localStateStore), |
241 | 186 | migrate: (persistedState, version) => { |
242 | 187 | const state = persistedState as unknown as UserPreferencesState & |
243 | 188 | UserPreferencesStoreActions |
| 189 | + |
244 | 190 | if (version === 0) { |
245 | | - console.log('Migrating preferences_storage to version 1', state) |
| 191 | + const previousTags = state.userSelectedTags as unknown as string[] |
| 192 | + |
| 193 | + const MAP_OLD_TAGS: Record<string, string> = { |
| 194 | + 'artificial-intelligence': 'artificial intelligence', |
| 195 | + 'machine-learning': 'machine learning', |
| 196 | + cpp: 'c++', |
| 197 | + csharp: 'c#', |
| 198 | + 'data-science': 'data science', |
| 199 | + 'objective-c': 'objectivec', |
| 200 | + } |
| 201 | + |
| 202 | + const newTags = previousTags |
| 203 | + .map((tag) => { |
| 204 | + if (MAP_OLD_TAGS[tag]) { |
| 205 | + return MAP_OLD_TAGS[tag] |
| 206 | + } |
| 207 | + return tag |
| 208 | + }) |
| 209 | + .map((tag) => { |
| 210 | + return { |
| 211 | + label: capitalize(tag), |
| 212 | + value: tag, |
| 213 | + } |
| 214 | + }) |
246 | 215 |
|
247 | 216 | return { |
248 | 217 | ...state, |
249 | 218 | onboardingCompleted: true, |
| 219 | + userSelectedTags: newTags, |
250 | 220 | occupation: state.onboardingResult?.title || '', |
251 | 221 | } |
252 | 222 | } |
|
0 commit comments