@legalplace/wizardx-core 4.42.10-nightly.20251126153634 → 4.42.10-nightly.20251126154644

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,22 @@
1
1
  import React from "react";
2
- import type { Reducer } from "redux";
2
+ import type { LoadableComponent } from "@loadable/component";
3
+ import type { Action, Reducer } from "redux";
3
4
  import type { Saga } from "redux-saga";
4
- import type { IPlugin, IPluginConfig } from "./types/PluginConfig.type";
5
+ declare let pluginsStoreReducers: Record<string, Reducer<Record<string, any>, any>>;
6
+ declare const pluginsStoreSagas: Record<string, Saga[]>;
7
+ export type PluginAppStatesRegisterFunction = (currentAppStates: string[]) => string[];
8
+ export type PluginsPageAppStatesType = Record<string, string>;
9
+ export type actionOverrideFunction = (action: Action) => Action;
10
+ export interface IPluginsAppStatesComponents {
11
+ [key: string]: LoadableComponent<any>;
12
+ }
13
+ export interface PluginsAppStatesDeclarations {
14
+ register: PluginAppStatesRegisterFunction;
15
+ pageAppState: PluginsPageAppStatesType;
16
+ components: IPluginsAppStatesComponents;
17
+ additionalRoutes?: Record<string, ReactComponent<any>>[];
18
+ }
19
+ declare let PluginsPageAppStates: PluginsPageAppStatesType;
5
20
  type TPluginLoader = React.FC<PluginLoaderProps>;
6
21
  export interface PluginLoaderProps {
7
22
  anchor: string[];
@@ -16,16 +31,20 @@ export type ReplaceFragmentPropsType = {
16
31
  fragmentName?: string;
17
32
  children?: React.ReactNode;
18
33
  };
19
- export declare const ReplaceFragment: ({ replacerName, fragmentName, children, }: ReplaceFragmentPropsType) => import("react/jsx-runtime").JSX.Element;
34
+ export declare const ReplaceFragment: React.FC<ReplaceFragmentPropsType>;
20
35
  export declare function registerReplacer(replacerName: string, Replacer: ReactComponent<any>): void;
21
- export declare function loadPlugins(plugins?: Record<string, string | (() => Promise<IPlugin>) | Promise<IPlugin> | IPlugin>): Promise<any>;
36
+ export declare const registerPluginsAppStates: PluginAppStatesRegisterFunction;
37
+ export declare const loadPluginComponent: (name: string) => LoadableComponent<any> | undefined;
38
+ export declare const pluginPageAppState: (page: string) => string | false;
39
+ export declare const getPluginsPageAppStates: () => Readonly<typeof PluginsPageAppStates>;
40
+ export declare const getPluginsStoreReducers: () => Readonly<typeof pluginsStoreReducers>;
41
+ export declare const getPluginsStoreSagas: () => Readonly<typeof pluginsStoreSagas>;
42
+ export declare function RunActionAnchor(anchor: string, ...args: any[]): void;
43
+ export declare function RunOverrideActionAnchor(action: Action): Action<any>;
44
+ export declare const loadPlugins: (plugins?: Record<string, string>) => Promise<void>;
45
+ export declare const pluginsLoaded: () => boolean;
46
+ export declare const getPluginsAdditionalRoutes: () => Record<string, ReactComponent<any>>;
22
47
  export declare function getPluginsReducers(): Record<string, Reducer<Record<string, any>, any>>;
23
- export declare function getPluginsAdditionalRoutes(): IPluginConfig[];
24
- export declare function getPluginsSagas(): Saga[];
25
- export declare function clearPlugins(): void;
26
- export declare const RunActionAnchor: () => void;
27
- export declare const RunOverrideActionAnchor: () => void;
28
- export declare const registerPluginsAppStates: (appStates: any) => any;
29
- export declare const getPluginsStoreReducers: typeof getPluginsReducers;
30
- export declare const getPluginsStoreSagas: typeof getPluginsSagas;
48
+ export declare function getPluginsSagas(): Record<string, Saga[]>;
49
+ export declare const clearPlugins: () => void;
31
50
  export {};
@@ -1,209 +1,256 @@
1
- import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
2
11
  import React from "react";
3
12
  import { getStore } from "./redux/store";
13
+ import { getHistory } from "./redux/routerHistory";
14
+ import { getConfig } from "./config";
4
15
  import Globals from "./Globals";
16
+ import { INIT_PLUGINS } from "./redux/constants/app";
17
+ let globalAnchors = {};
18
+ let reduxAnchors = {};
19
+ let actionOverrideAnchor = {};
20
+ let replacers = {};
5
21
  let pluginsStoreReducers = {};
6
- let pluginsSagas = [];
7
- const replacers = {};
22
+ const pluginsStoreSagas = {};
23
+ let cache = {};
24
+ let pluginsLoadingDone = false;
25
+ let PluginsAppStatesRegistrers = [];
26
+ let PluginsPageAppStates = {};
27
+ let PluginsAppStatesComponents = {};
28
+ let PluginAdditionRoutes = {};
8
29
  export const PluginLoader = (props) => {
9
30
  const { anchor } = props;
10
- const plugins = getStore().getState().pluginsStore;
31
+ const anchorsList = !Array.isArray(anchor) ? [anchor] : anchor;
11
32
  let Plugins = [];
12
- if (!plugins || !plugins.config || !Array.isArray(plugins.config)) {
13
- return _jsx(_Fragment, {});
14
- }
15
- const matchedPlugins = plugins.config.filter((p) => {
16
- if (p.anchor.length !== anchor.length)
17
- return false;
18
- for (let i = 0; i < anchor.length; i += 1) {
19
- if (p.anchor[i] !== anchor[i] && p.anchor[i] !== "*")
20
- return false;
33
+ for (let i = 0; i < anchorsList.length; i += 1) {
34
+ const currentAnchor = anchorsList[i];
35
+ if (Object.prototype.hasOwnProperty.call(globalAnchors, currentAnchor) &&
36
+ globalAnchors[currentAnchor].length > 0) {
37
+ const currentPlugins = globalAnchors[currentAnchor];
38
+ Plugins = [
39
+ ...Plugins,
40
+ ...currentPlugins.map((Plugin) => (_jsx(React.Suspense, { fallback: null, children: _jsx(Plugin, Object.assign({}, props.props)) }, `lpwp-${currentAnchor}-${Math.random()}`))),
41
+ ];
21
42
  }
22
- return true;
23
- });
24
- if (matchedPlugins.length > 0) {
25
- Plugins = matchedPlugins.map((p, i) => (_jsx(p.component, Object.assign({}, (props.props || {})), i)));
26
43
  }
27
- return _jsx(_Fragment, { children: Plugins });
44
+ if (Plugins.length > 0) {
45
+ return _jsx(_Fragment, { children: Plugins });
46
+ }
47
+ return null;
28
48
  };
29
49
  export function replaceComponent(replacerName, OriginalComponent, componentName) {
50
+ const displayName = componentName ||
51
+ OriginalComponent.displayName ||
52
+ OriginalComponent.name ||
53
+ "Component";
54
+ const anchorsList = !Array.isArray(replacerName)
55
+ ? [replacerName]
56
+ : replacerName;
30
57
  let Replacer = null;
31
- const name = componentName || OriginalComponent.displayName;
32
- if (Array.isArray(replacerName)) {
33
- replacerName.forEach((r) => {
34
- if (replacers[r])
35
- Replacer = r;
36
- });
58
+ const cacheKey = anchorsList.reduce((a, b) => a + b) + OriginalComponent.toString();
59
+ if (Object.prototype.hasOwnProperty.call(cache, cacheKey)) {
60
+ return cache[cacheKey];
37
61
  }
38
- else if (replacers[replacerName]) {
39
- Replacer = replacerName;
62
+ for (let i = 0; i < anchorsList.length; i += 1) {
63
+ const currentAnchor = anchorsList[i];
64
+ if (Object.prototype.hasOwnProperty.call(replacers, currentAnchor)) {
65
+ Replacer = currentAnchor;
66
+ break;
67
+ }
40
68
  }
69
+ let FinalComponent;
41
70
  if (Replacer !== null) {
42
- const BaseComponent = (props) => (_jsx(OriginalComponent, Object.assign({}, props, { children: props.children })));
71
+ const BaseComponent = (props) => React.createElement(OriginalComponent, props, props.children);
43
72
  const FinalReplacedComponent = replacers[Replacer].reduce((PreviousReplacer, CurrentReplacer) => {
44
73
  const ReplacedComponent = (props) => (_jsx(React.Suspense, { fallback: null, children: _jsx(CurrentReplacer, Object.assign({}, props, { OriginalComponent: PreviousReplacer, children: props.children })) }));
45
74
  return ReplacedComponent;
46
75
  }, BaseComponent);
47
- if (name) {
48
- FinalReplacedComponent.displayName = `Replaced(${name})`;
49
- }
50
- return FinalReplacedComponent;
76
+ FinalComponent = FinalReplacedComponent;
77
+ FinalComponent.displayName = `replacedComponent(${displayName})`;
78
+ }
79
+ else {
80
+ FinalComponent = OriginalComponent;
81
+ FinalComponent.displayName = `originalComponent(${displayName})`;
51
82
  }
52
- return OriginalComponent;
83
+ cache[cacheKey] = React.memo(FinalComponent);
84
+ return cache[cacheKey];
53
85
  }
54
86
  export const ReplaceFragment = ({ replacerName, fragmentName, children, }) => {
55
- let Replacer = null;
56
- if (Array.isArray(replacerName)) {
57
- replacerName.forEach((r) => {
58
- if (replacers[r])
59
- Replacer = r;
60
- });
61
- }
62
- else if (replacers[replacerName]) {
63
- Replacer = replacerName;
64
- }
65
- if (Replacer !== null) {
66
- const Fragment = (props) => _jsx(_Fragment, { children: props.children });
67
- const FinalReplacedComponent = replacers[Replacer].reduce((PreviousReplacer, CurrentReplacer) => {
68
- const ReplacedComponent = (props) => (_jsx(React.Suspense, { fallback: null, children: _jsx(CurrentReplacer, Object.assign({}, props, { OriginalComponent: PreviousReplacer, children: props.children })) }));
69
- return ReplacedComponent;
70
- }, Fragment);
71
- if (fragmentName) {
72
- FinalReplacedComponent.displayName = `Replaced(${fragmentName})`;
73
- }
74
- return _jsx(FinalReplacedComponent, { children: children });
75
- }
76
- return _jsx(_Fragment, { children: children });
87
+ const Fragment = () => _jsx(_Fragment, { children: children });
88
+ const FinalFragment = replaceComponent(replacerName, Fragment, `Fragment::${fragmentName}`);
89
+ return _jsx(FinalFragment, {});
77
90
  };
78
91
  export function registerReplacer(replacerName, Replacer) {
79
92
  if (!replacers[replacerName])
80
93
  replacers[replacerName] = [];
81
94
  replacers[replacerName].push(Replacer);
82
95
  }
83
- export function loadPlugins(plugins) {
84
- const promises = [];
85
- if (!plugins || typeof plugins !== "object") {
86
- return Promise.resolve([]);
87
- }
88
- const store = getStore();
89
- Object.keys(plugins).forEach((key) => {
90
- const pluginValue = plugins[key];
91
- let pluginPromise;
92
- if (typeof pluginValue === "string") {
93
- try {
94
- pluginPromise = Promise.resolve(Globals.loadPlugin(key));
96
+ const pluginAppStates = (declaration) => {
97
+ PluginsAppStatesRegistrers.push(declaration.register);
98
+ if (typeof declaration.pageAppState === "object")
99
+ Object.keys(declaration.pageAppState).forEach((appState) => {
100
+ if (typeof declaration.pageAppState[appState] === "string") {
101
+ const page = declaration.pageAppState[appState];
102
+ if (Object.prototype.hasOwnProperty.call(PluginsPageAppStates, appState)) {
103
+ throw new Error(`Plugins AppState "${appState}" already declared.`);
104
+ }
105
+ const samePageAppStates = Object.keys(PluginsPageAppStates).filter((currentAppState) => PluginsPageAppStates[currentAppState] === page);
106
+ if (samePageAppStates.length > 0)
107
+ throw new Error(`Plugins AppState page "${page}" already declared.`);
108
+ PluginsPageAppStates[appState] = page;
95
109
  }
96
- catch (error) {
97
- console.error(`Failed to load plugin "${key}" using Globals.loadPlugin:`, error);
110
+ });
111
+ Object.keys(declaration.components).forEach((appState) => {
112
+ if (Object.prototype.hasOwnProperty.call(declaration, appState) === true)
113
+ throw new Error(`Plugin's appState ${appState} already declared previously`);
114
+ PluginsAppStatesComponents[appState] = declaration.components[appState];
115
+ });
116
+ };
117
+ export const registerPluginsAppStates = (currentAppStates) => {
118
+ let appStates = [...currentAppStates];
119
+ PluginsAppStatesRegistrers.forEach((register) => {
120
+ appStates = register(appStates);
121
+ });
122
+ return appStates;
123
+ };
124
+ export const loadPluginComponent = (name) => {
125
+ if (Object.prototype.hasOwnProperty.call(PluginsAppStatesComponents, name) ===
126
+ false) {
127
+ return undefined;
128
+ }
129
+ return PluginsAppStatesComponents[name];
130
+ };
131
+ export const pluginPageAppState = (page) => {
132
+ const appStates = Object.keys(PluginsPageAppStates).filter((appState) => PluginsPageAppStates[appState] === page);
133
+ if (appStates.length === 1)
134
+ return appStates[0];
135
+ if (appStates.length > 1)
136
+ throw new Error("Found multipled appStates when checking for plugins page name");
137
+ return false;
138
+ };
139
+ export const getPluginsPageAppStates = () => PluginsPageAppStates;
140
+ export const getPluginsStoreReducers = () => pluginsStoreReducers;
141
+ export const getPluginsStoreSagas = () => pluginsStoreSagas;
142
+ export function RunActionAnchor(anchor, ...args) {
143
+ if (Object.prototype.hasOwnProperty.call(reduxAnchors, anchor) &&
144
+ reduxAnchors[anchor].length > 0) {
145
+ reduxAnchors[anchor].forEach((currentFn) => {
146
+ currentFn.call(null, ...args);
147
+ });
148
+ }
149
+ }
150
+ export function RunOverrideActionAnchor(action) {
151
+ let overridedAction = action;
152
+ if (Object.prototype.hasOwnProperty.call(actionOverrideAnchor, action.type) &&
153
+ actionOverrideAnchor[action.type].length > 0) {
154
+ actionOverrideAnchor[action.type].forEach((currentFn) => {
155
+ overridedAction = currentFn.call(null, overridedAction);
156
+ });
157
+ }
158
+ return overridedAction;
159
+ }
160
+ const loadPluginFiles = (pluginsList) => __awaiter(void 0, void 0, void 0, function* () {
161
+ const plugins = Object.keys(pluginsList);
162
+ const pluginModules = yield Promise.all(plugins.map((plugin) => Globals.loadPlugin(plugin)));
163
+ plugins.forEach((plugin, index) => {
164
+ const pluginModule = pluginModules[index];
165
+ const anchors = pluginModule.default;
166
+ const { loader } = pluginModule;
167
+ if (loader !== undefined) {
168
+ loader(getStore().dispatch, getHistory(), getConfig());
169
+ }
170
+ Object.keys(anchors).forEach((anchor) => {
171
+ if (anchor === "replace") {
172
+ Object.keys(anchors[anchor]).forEach((replacer) => {
173
+ if (!Object.prototype.hasOwnProperty.call(replacers, replacer)) {
174
+ replacers[replacer] = [];
175
+ }
176
+ replacers[replacer].push(anchors[anchor][replacer]);
177
+ });
98
178
  return;
99
179
  }
100
- }
101
- else if (typeof pluginValue === "function") {
102
- try {
103
- pluginPromise = Promise.resolve(pluginValue());
180
+ if (anchor === "appstates") {
181
+ pluginAppStates(anchors.appstates);
104
182
  }
105
- catch (error) {
106
- console.error(`Failed to execute plugin "${key}" loader:`, error);
183
+ if (anchor === "redux") {
184
+ Object.keys(anchors[anchor]).forEach((action) => {
185
+ if (reduxAnchors[action] === undefined)
186
+ reduxAnchors[action] = [];
187
+ reduxAnchors[action].push(anchors[anchor][action]);
188
+ });
107
189
  return;
108
190
  }
109
- }
110
- else if (pluginValue && typeof pluginValue === "object" && "then" in pluginValue) {
111
- pluginPromise = pluginValue;
112
- }
113
- else if (pluginValue && typeof pluginValue === "object") {
114
- pluginPromise = Promise.resolve(pluginValue);
115
- }
116
- else {
117
- console.warn(`Plugin "${key}" has invalid format, skipping...`);
118
- return;
119
- }
120
- promises.push(pluginPromise.then((plugin) => {
121
- if (!plugin)
191
+ if (anchor === "actionOverride") {
192
+ Object.keys(anchors[anchor]).forEach((action) => {
193
+ if (actionOverrideAnchor[action] === undefined)
194
+ actionOverrideAnchor[action] = [];
195
+ actionOverrideAnchor[action].push(anchors[anchor][action]);
196
+ });
122
197
  return;
123
- if (plugin.redux) {
124
- pluginsStoreReducers = Object.assign(Object.assign({}, pluginsStoreReducers), { [key]: plugin.redux.reducer });
125
- pluginsSagas = [...pluginsSagas, ...plugin.redux.sagas];
126
- }
127
- if (plugin.reduxStore) {
128
- pluginsStoreReducers = Object.assign(Object.assign({}, pluginsStoreReducers), { [key]: plugin.reduxStore.reducer });
129
- if (plugin.reduxStore.sagas) {
130
- pluginsSagas = [...pluginsSagas, ...plugin.reduxStore.sagas];
131
- }
132
198
  }
133
- if (plugin.replace) {
134
- Object.keys(plugin.replace).forEach((componentName) => {
135
- if (plugin.replace) {
136
- registerReplacer(componentName, plugin.replace[componentName]);
137
- }
199
+ if (anchor === "additionalRoutes") {
200
+ Object.keys(anchors[anchor]).forEach((route) => {
201
+ PluginAdditionRoutes[anchors[anchor][route].path] =
202
+ anchors[anchor][route].component;
138
203
  });
204
+ return;
139
205
  }
140
- const configs = [];
141
- if (plugin.config && Array.isArray(plugin.config)) {
142
- configs.push(...plugin.config);
206
+ if (anchor === "reduxStore" && anchors[anchor].reducer) {
207
+ pluginsStoreReducers[plugin] = anchors[anchor].reducer;
143
208
  }
144
- else {
145
- Object.keys(plugin).forEach((prop) => {
146
- if (["redux", "reduxStore", "replace", "config", "appstates", "actionOverride", "additionalRoutes", "paginationAfterNext"].includes(prop)) {
147
- return;
148
- }
149
- const value = plugin[prop];
150
- if (value && (typeof value === "function" || (typeof value === "object" && (value.$$typeof || value._payload)))) {
151
- configs.push({
152
- anchor: [prop],
153
- component: value,
154
- });
155
- }
156
- });
157
- }
158
- if (configs.length > 0) {
159
- const currentState = store.getState().pluginsStore;
160
- const existingConfig = currentState.config || [];
161
- store.dispatch({
162
- type: "@@PLUGIN/ADD_CONFIG",
163
- payload: {
164
- config: [...existingConfig, ...configs],
165
- },
166
- });
209
+ if (anchor === "reduxStore" && anchors[anchor].sagas) {
210
+ pluginsStoreSagas[plugin] = anchors[anchor].sagas;
167
211
  }
168
- }).catch((error) => {
169
- console.error(`Failed to load plugin "${key}":`, error);
170
- }));
212
+ if (!Object.prototype.hasOwnProperty.call(globalAnchors, anchor))
213
+ globalAnchors[anchor] = [];
214
+ globalAnchors[anchor].push(anchors[anchor]);
215
+ });
216
+ if (typeof anchors.onLoad === "function")
217
+ anchors.onLoad();
171
218
  });
172
- return Promise.all(promises);
173
- }
219
+ if (Object.keys(pluginsStoreReducers).length > 0 ||
220
+ Object.keys(pluginsStoreSagas).length > 0) {
221
+ getStore().dispatch({ type: INIT_PLUGINS });
222
+ }
223
+ });
224
+ export const loadPlugins = (plugins) => __awaiter(void 0, void 0, void 0, function* () {
225
+ let pluginsList;
226
+ if (!plugins)
227
+ pluginsList = getConfig().plugins;
228
+ else
229
+ pluginsList = plugins;
230
+ if (pluginsList === undefined) {
231
+ pluginsList = getConfig().plugins;
232
+ }
233
+ yield loadPluginFiles(pluginsList);
234
+ pluginsLoadingDone = true;
235
+ });
236
+ export const pluginsLoaded = () => pluginsLoadingDone;
237
+ export const getPluginsAdditionalRoutes = () => PluginAdditionRoutes;
174
238
  export function getPluginsReducers() {
175
239
  return pluginsStoreReducers;
176
240
  }
177
- export function getPluginsAdditionalRoutes() {
178
- const routes = [];
179
- const plugins = getStore().getState().pluginsStore;
180
- if ((plugins === null || plugins === void 0 ? void 0 : plugins.config) && Array.isArray(plugins.config)) {
181
- plugins.config.forEach((p) => {
182
- if (p.anchor[0] === "routes") {
183
- routes.push(p);
184
- }
185
- });
186
- }
187
- return routes;
188
- }
189
241
  export function getPluginsSagas() {
190
- return pluginsSagas;
242
+ return pluginsStoreSagas;
191
243
  }
192
- export function clearPlugins() {
244
+ export const clearPlugins = () => {
245
+ globalAnchors = {};
246
+ reduxAnchors = {};
247
+ actionOverrideAnchor = {};
248
+ replacers = {};
249
+ PluginsAppStatesRegistrers = [];
250
+ PluginsPageAppStates = {};
251
+ PluginsAppStatesComponents = {};
252
+ PluginAdditionRoutes = {};
253
+ cache = {};
254
+ pluginsLoadingDone = false;
193
255
  pluginsStoreReducers = {};
194
- pluginsSagas = [];
195
- const store = getStore(false);
196
- if (store) {
197
- store.dispatch({
198
- type: "@@PLUGIN/ADD_CONFIG",
199
- payload: {
200
- config: [],
201
- },
202
- });
203
- }
204
- }
205
- export const RunActionAnchor = () => { };
206
- export const RunOverrideActionAnchor = () => { };
207
- export const registerPluginsAppStates = (appStates) => appStates || [];
208
- export const getPluginsStoreReducers = getPluginsReducers;
209
- export const getPluginsStoreSagas = getPluginsSagas;
256
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legalplace/wizardx-core",
3
- "version": "4.42.10-nightly.20251126153634",
3
+ "version": "4.42.10-nightly.20251126154644",
4
4
  "author": "Moncef Hammou (moncef@legalplace.fr)",
5
5
  "license": "MIT",
6
6
  "files": [