PluginProbe
Extendify / 3.1.3
Extendify v3.1.3
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.1.3, at src/AutoLaunch/state/launch-data.js

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