@zextras/carbonio-shell-ui
Version:
The Zextras Carbonio web client
65 lines • 2.4 kB
JavaScript
;
/*
* SPDX-FileCopyrightText: 2021 Zextras <https://www.zextras.com>
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useIntegrationsStore = void 0;
const immer_1 = require("immer");
const lodash_1 = require("lodash");
const zustand_1 = require("zustand");
const composer_1 = __importDefault(require("./composer"));
const constants_1 = require("../../constants");
const initialState = {
actions: {},
components: {
composer: {
Item: composer_1.default,
app: constants_1.SHELL_APP_ID
}
},
functions: {}
};
// extra currying as suggested in https://github.com/pmndrs/zustand/blob/main/docs/guides/typescript.md#basic-usage
exports.useIntegrationsStore = (0, zustand_1.create)()((set) => ({
...initialState,
registerActions: (...items) => set((0, immer_1.produce)((state) => {
(0, lodash_1.forEach)(items, ({ id, action, type }) => {
if (!state.actions[type])
state.actions[type] = {};
state.actions[type][id] = action;
});
})),
registerComponents: (app) => (...items) => set((0, immer_1.produce)((state) => {
(0, lodash_1.forEach)(items, ({ id, component }) => {
state.components[id] = { app, Item: component };
});
})),
registerFunctions: (...items) => set((0, immer_1.produce)((state) => {
(0, lodash_1.forEach)(items, ({ id, fn }) => {
state.functions[id] = fn;
});
})),
removeActions: (...ids) => set((0, immer_1.produce)((state) => {
(0, lodash_1.forEach)(state.actions, (actionTypeMap, type) => {
(0, lodash_1.forEach)(actionTypeMap, (actionFactory, actionFactoryId) => {
if ((0, lodash_1.includes)(ids, actionFactoryId)) {
delete state.actions[type][actionFactoryId];
}
});
});
})),
removeComponents: (...ids) => set((s) => ({
...s,
components: (0, lodash_1.omit)(s.components, ids)
})),
removeFunctions: (...ids) => set((s) => ({
...s,
functions: (0, lodash_1.omit)(s.functions, ids)
}))
}));
//# sourceMappingURL=store.js.map