@zextras/carbonio-shell-ui
Version:
The Zextras Carbonio web client
97 lines • 4.43 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.useBoard = exports.useBoardHooks = exports.BoardProvider = exports.boardHooksContext = exports.boardContext = exports.getBoardContextById = exports.useBoardContextById = exports.getBoardById = exports.useBoardById = void 0;
/*
* SPDX-FileCopyrightText: 2022 Zextras <https://www.zextras.com>
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
const react_1 = __importStar(require("react"));
const store_1 = require("./store");
const useBoardById = (id) => (0, store_1.useBoardStore)((s) => s.boards[id]);
exports.useBoardById = useBoardById;
const getBoardById = (id) => store_1.useBoardStore.getState().boards[id];
exports.getBoardById = getBoardById;
const useBoardContextById = (id) => (0, store_1.useBoardStore)((s) => s.boards[id]?.context);
exports.useBoardContextById = useBoardContextById;
const getBoardContextById = (id) => store_1.useBoardStore.getState().boards[id]?.context;
exports.getBoardContextById = getBoardContextById;
// Must be empty if no board is available
exports.boardContext = (0, react_1.createContext)(undefined);
// Must be empty if no board is available
exports.boardHooksContext = (0, react_1.createContext)(undefined);
const BoardProvider = ({ children, id }) => {
const board = (0, exports.useBoardById)(id);
const callbacks = (0, react_1.useMemo)(() => ({
// at this point the id should be only one of the available boards,
// that's way the cast without further checks
getBoardContext: () => (0, exports.getBoardContextById)(id),
getBoard: () => (0, exports.getBoardById)(id),
closeBoard: () => (0, store_1.closeBoard)(id),
updateBoard: (b) => (0, store_1.updateBoard)(id, b),
setCurrentBoard: () => (0, store_1.setCurrentBoard)(id)
}), [id]);
return (react_1.default.createElement(exports.boardHooksContext.Provider, { value: callbacks },
react_1.default.createElement(exports.boardContext.Provider, { value: board }, children)));
};
exports.BoardProvider = BoardProvider;
/**
* Allow quick access to all current board hooks.
* This hook must be used from inside a board view.
* Outside a board, it will throw an error.
*/
const useBoardHooks = () => {
const hooks = (0, react_1.useContext)(exports.boardHooksContext);
if (hooks === undefined) {
throw new Error('board hooks is undefined. This hook is meant to be used only from inside boards. Check that the hook is invoked from a board');
}
return hooks;
};
exports.useBoardHooks = useBoardHooks;
/**
* Retrieve the board object.
* This hook must be used from inside a board view.
* Outside a board, it will throw an error.
*/
const useBoard = () => {
const board = (0, react_1.useContext)(exports.boardContext);
if (board === undefined) {
throw new Error('board is undefined. This hook is meant to be used only from inside boards. Check that the hook is invoked from a board');
}
return board;
};
exports.useBoard = useBoard;
//# sourceMappingURL=hooks.js.map