PluginProbe
Extendify / 3.2.1
Extendify v3.2.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 / globals.js

globals.js in Extendify 3.2.1, at src/Assist/state/globals.js

65 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { safeParseJson } from '@shared/lib/parsing';
2 import apiFetch from '@wordpress/api-fetch';
3 import { create } from 'zustand';
4 import { createJSONStorage, devtools, persist } from 'zustand/middleware';
5
6 const key = 'extendify-assist-globals';
7 const startingState = {
8 dismissedNotices: [],
9 dismissedBanners: [],
10 modals: [],
11 showConfetti: true,
12 // domains suggestion key
13 domainsCacheKey: 'first-run',
14 // initialize the state with default values
15 ...(safeParseJson(window.extAssistData.userData.globalData)?.state ?? {}),
16 };
17
18 const state = (set, get) => ({
19 ...startingState,
20 isDismissedBanner(id) {
21 return get().dismissedBanners.some((banner) => banner.id === id);
22 },
23 dismissBanner(id) {
24 if (get().isDismissedBanner(id)) return;
25 const banner = { id, dismissedAt: new Date().toISOString() };
26 set((state) => ({
27 dismissedBanners: [...state.dismissedBanners, banner],
28 }));
29 },
30 dismissConfetti() {
31 set({ showConfetti: false });
32 },
33 pushModal(modal) {
34 set((state) => ({ modals: [modal, ...state.modals] }));
35 },
36 popModal() {
37 set((state) => ({ modals: state.modals.slice(1) }));
38 },
39 clearModals() {
40 set({ modals: [] });
41 },
42 updateDomainsCacheKey() {
43 set(() => ({ domainsCacheKey: Date.now() }));
44 },
45 });
46
47 const path = '/extendify/v1/assist/global-data';
48 const storage = {
49 getItem: async () => await apiFetch({ path }),
50 setItem: async (_name, state) =>
51 await apiFetch({ path, method: 'POST', data: { state } }),
52 };
53
54 export const useGlobalStore = create(
55 persist(devtools(state, { name: 'Extendify Assist Globals' }), {
56 name: key,
57 storage: createJSONStorage(() => storage),
58 skipHydration: true,
59 partialize: (state) => {
60 delete state.modals;
61 return state;
62 },
63 }),
64 );
65