| @@ -1,8 +1,8 @@ | ||
| 1 | 1 | import apiFetch from '@wordpress/api-fetch'; |
| 2 | 2 | import { useEffect, useState } from '@wordpress/element'; |
| 3 | -import { create } from 'zustand'; | |
| 4 | -import { createJSONStorage, devtools, persist } from 'zustand/middleware'; | |
| 3 | +import create from 'zustand'; | |
| 4 | +import { devtools, persist } from 'zustand/middleware'; | |
| 5 | 5 | |
| 6 | 6 | type Settings = { |
| 7 | 7 | seenNotices: string[]; |
| 8 | 8 | hiddenThemes: string[]; |
| @@ -8,9 +8,9 @@ | ||
| 8 | 8 | hiddenThemes: string[]; |
| 9 | 9 | setSeenNotice: (notice: string) => void; |
| 10 | 10 | toggleHiddenTheme: (theme: string) => void; |
| 11 | 11 | }; |
| 12 | -const path = '/code-block-pro/v1/settings'; | |
| 12 | +const path = '/wp/v2/settings'; | |
| 13 | 13 | const getSettings = async (name: string) => { |
| 14 | 14 | const allSettings = await apiFetch({ path }); |
| 15 | 15 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 16 | 16 | // @ts-ignore-next-line |
| @@ -15,43 +15,14 @@ | ||
| 15 | 15 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 16 | 16 | // @ts-ignore-next-line |
| 17 | 17 | return allSettings?.[name]; |
| 18 | 18 | }; |
| 19 | -const defaultSettings = { | |
| 20 | - seenNotices: [], | |
| 21 | - hiddenThemes: [], | |
| 22 | -}; | |
| 23 | -const storage = { | |
| 24 | - getItem: async (name: string) => { | |
| 25 | - const settings = await getSettings(name); | |
| 26 | - return JSON.stringify({ | |
| 27 | - version: settings?.version ?? 0, | |
| 28 | - state: settings || defaultSettings, | |
| 29 | - }); | |
| 30 | - }, | |
| 31 | - setItem: async (name: string, value: string) => { | |
| 32 | - const { state, version } = JSON.parse(value); | |
| 33 | - const data = { | |
| 34 | - [name]: Object.assign( | |
| 35 | - (await getSettings(name)) || defaultSettings, | |
| 36 | - state, | |
| 37 | - { version }, | |
| 38 | - ), | |
| 39 | - }; | |
| 40 | - await apiFetch({ path, method: 'POST', data }); | |
| 41 | - }, | |
| 42 | - removeItem: async (name: string) => { | |
| 43 | - const data = { [name]: null }; | |
| 44 | - await apiFetch({ path, method: 'POST', data }); | |
| 45 | - return undefined; | |
| 46 | - }, | |
| 47 | -}; | |
| 48 | - | |
| 49 | 19 | export const useSettingsStore = create<Settings>()( |
| 50 | 20 | persist( |
| 51 | 21 | devtools( |
| 52 | 22 | (set) => ({ |
| 53 | - ...defaultSettings, | |
| 23 | + seenNotices: [], | |
| 24 | + hiddenThemes: [], | |
| 54 | 25 | setSeenNotice(notice: string) { |
| 55 | 26 | set((state) => { |
| 56 | 27 | if (state.seenNotices.includes(notice)) return state; |
| 57 | 28 | return { seenNotices: [...state.seenNotices, notice] }; |
| @@ -68,9 +39,32 @@ | ||
| 68 | 39 | { name: 'Code Block Pro Settings' }, |
| 69 | 40 | ), |
| 70 | 41 | { |
| 71 | 42 | name: 'code_block_pro_settings_2', |
| 72 | - storage: createJSONStorage(() => storage), | |
| 43 | + getStorage: () => ({ | |
| 44 | + getItem: async (name: string) => { | |
| 45 | + const settings = await getSettings(name); | |
| 46 | + return JSON.stringify({ | |
| 47 | + version: settings?.version ?? 0, | |
| 48 | + state: settings, | |
| 49 | + }); | |
| 50 | + }, | |
| 51 | + setItem: async (name: string, value: string) => { | |
| 52 | + const { state, version } = JSON.parse(value); | |
| 53 | + const data = { | |
| 54 | + [name]: Object.assign( | |
| 55 | + (await getSettings(name)) ?? {}, | |
| 56 | + state, | |
| 57 | + version, | |
| 58 | + ), | |
| 59 | + }; | |
| 60 | + await apiFetch({ path, method: 'POST', data }); | |
| 61 | + }, | |
| 62 | + removeItem: async (name: string) => { | |
| 63 | + const data = { [name]: null }; | |
| 64 | + return await apiFetch({ path, method: 'POST', data }); | |
| 65 | + }, | |
| 66 | + }), | |
| 73 | 67 | }, |
| 74 | 68 | ), |
| 75 | 69 | ); |
| 76 | 70 | |