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

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