PluginProbe
Extendify / 3.0.6
Extendify v3.0.6
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 / AutoLaunch / state / launch-data.js

launch-data.js in Extendify 3.0.6, at src/AutoLaunch/state/launch-data.js

179 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {
2 getDesignBuildShape,
3 getHomeShape,
4 getImagesShape,
5 getLogoShape,
6 getPagesShape,
7 getPluginsShape,
8 getProfileShape,
9 getStringsShape,
10 getStyleShape,
11 } from '@auto-launch/fetchers/shape';
12 import { clearSiteImages } from '@auto-launch/functions/wp';
13 import { __ } from '@wordpress/i18n';
14 import { create } from 'zustand';
15 import { devtools, persist } from 'zustand/middleware';
16 import { overrideWithUrlParams, urlParams, urlParamsShape } from './url-params';
17
18 const shapeToKeyValue = (shape) => {
19 return Object.fromEntries(
20 Object.keys(shape.shape).map((key) => [key, undefined]),
21 );
22 };
23
24 const initialState = {
25 go: false,
26 // translators: this is for a action log UI. Keep it short
27 statusMessages: [__('Booting things up', 'extendify-local')],
28 errorMessage: null,
29 errorCount: 0,
30 description: null,
31 descriptionBackup: undefined,
32 descriptionRaw: null,
33 urlParams: {},
34 siteProfile: {
35 ...shapeToKeyValue(getProfileShape),
36 },
37 ...shapeToKeyValue(getLogoShape),
38 ...shapeToKeyValue(getPluginsShape),
39 ...shapeToKeyValue(getStyleShape),
40 ...shapeToKeyValue(getStringsShape),
41 ...shapeToKeyValue(getImagesShape),
42 ...shapeToKeyValue(getHomeShape),
43 ...shapeToKeyValue(getPagesShape),
44 ...shapeToKeyValue(getDesignBuildShape),
45 designBuild: undefined,
46 attempt: 1,
47 };
48
49 const state = (set, get) => ({
50 ...initialState,
51 urlParams: {
52 ...initialState.urlParams,
53 ...urlParams,
54 },
55 title: undefined,
56 description: undefined,
57 descriptionBackup: undefined,
58 descriptionRaw: undefined,
59 pulse: false,
60 setPulse: (value) => set({ pulse: value }),
61 setData: (key, value) => {
62 if (!isValidKey(key)) return;
63 if (get()[key] === value) return; // avoid unnecessary updates
64 set({ [key]: value });
65 },
66 addStatusMessage: (message) => {
67 const currentMessages = get().statusMessages;
68 // remove any previous duplicates
69 const prev = currentMessages.filter((msg) => msg !== message);
70 set({ statusMessages: [...prev, message] });
71 },
72 setErrorMessage: (message) => {
73 set((state) => ({
74 errorMessage: message,
75 errorCount: state.errorCount + 1,
76 }));
77 },
78 needToStall: () => get().errorCount > 6,
79 resetErrorCount: () => {
80 set({ errorCount: 0 });
81 },
82 reset: ({ exclude }) => {
83 const newState = { ...initialState, attempt: get().attempt + 1 };
84 if (exclude && Array.isArray(exclude)) {
85 exclude.forEach((key) => {
86 if (!isValidKey(key)) return;
87 newState[key] = get()[key];
88 });
89 }
90 clearSiteImages().catch(() => null);
91 set(newState);
92 },
93 });
94
95 // Checks that a key being set is actually something we expect
96 const isValidKey = (key) => Object.keys(initialState).includes(key);
97
98 const keySchemas = {
99 urlParams: urlParamsShape,
100 siteProfile: getProfileShape,
101 ...Object.fromEntries(
102 [
103 getLogoShape,
104 getPluginsShape,
105 getStyleShape,
106 getStringsShape,
107 getImagesShape,
108 getHomeShape,
109 getPagesShape,
110 getDesignBuildShape,
111 ].flatMap((s) => Object.entries(s.shape)),
112 ),
113 };
114
115 export const useLaunchDataStore = create(
116 persist(devtools(state, { name: 'Extendify Launch Data' }), {
117 name: `extendify-launch-data-${window.extSharedData.siteId}`,
118 merge: (p, current) => {
119 // Make sure the persisted state is valid and not corrupted.
120 const persisted = p && typeof p === 'object' ? p : {};
121
122 // This gives us some recovery on page reload
123 const validated = Object.fromEntries(
124 Object.entries(persisted)
125 .filter(([key]) => key in keySchemas)
126 .map(([key, value]) => {
127 const result = keySchemas[key].safeParse(value);
128 return [key, result.success ? result.data : undefined];
129 }),
130 );
131 // Merge in the url params
132 const { title, description, go, ...urlParamsMapped } =
133 overrideWithUrlParams(urlParams);
134
135 return {
136 ...current,
137 ...persisted,
138 ...validated,
139 // If there's a URL param here it should override these values
140 title: title || persisted.title,
141 description: description || persisted.description,
142 descriptionRaw: description || persisted.descriptionRaw,
143 descriptionBackup: persisted.descriptionBackup || description || title,
144 go: go || persisted.go,
145 urlParams: {
146 title,
147 description,
148 go,
149 ...urlParamsMapped,
150 },
151 };
152 },
153 onRehydrateStorage: () => (state) => {
154 if (!state) return;
155 queueMicrotask(() => {
156 useLaunchDataStore.setState(state);
157 });
158 },
159 partialize: (state) => {
160 const {
161 statusMessages,
162 errorMessage,
163 errorCount,
164 pulse,
165 description,
166 descriptionRaw,
167 title,
168 ...rest
169 } = state;
170 return Object.fromEntries(
171 Object.entries(rest).filter(([, v]) =>
172 Array.isArray(v) ? v.length > 0 : Boolean(v),
173 ),
174 );
175 },
176 }),
177 state,
178 );
179