PluginProbe
Extendify / 0.11.1
Extendify v0.11.1
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / Assist / state / Global.js

Global.js in Extendify 0.11.1, at src/Assist/state/Global.js

47 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useEffect, useState } from '@wordpress/element'
2 import create from 'zustand'
3 import { devtools, persist } from 'zustand/middleware'
4 import { getGlobalData, saveGlobalData } from '../api/Data'
5
6 const state = (set, get) => ({
7 dismissedNotices: [],
8 isDismissed(id) {
9 return get().dismissedNotices.some((notice) => notice.id === id)
10 },
11 dismissNotice(id) {
12 if (get().isDismissed(id)) return
13 const notice = { id, dismissedAt: new Date().toISOString() }
14 set((state) => ({
15 dismissedNotices: [...state.dismissedNotices, notice],
16 }))
17 },
18 })
19
20 const storage = {
21 getItem: async () => JSON.stringify(await getGlobalData()),
22 setItem: async (_, value) => await saveGlobalData(value),
23 removeItem: () => undefined,
24 }
25
26 export const useGlobalStore = create(
27 persist(devtools(state, { name: 'Extendify Assist Globals' }), {
28 name: 'extendify-assist-globals',
29 getStorage: () => storage,
30 }),
31 state,
32 )
33
34 /* Hook useful for when you need to wait on the async state to hydrate */
35 export const useGlobalStoreReady = () => {
36 const [hydrated, setHydrated] = useState(useGlobalStore.persist.hasHydrated)
37 useEffect(() => {
38 const unsubFinishHydration = useGlobalStore.persist.onFinishHydration(
39 () => setHydrated(true),
40 )
41 return () => {
42 unsubFinishHydration()
43 }
44 }, [])
45 return hydrated
46 }
47