UNPKG

@slashid/docusaurus-theme-slashid

Version:
61 lines (59 loc) 3.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = DocSidebarItemWrapper; var _react = _interopRequireWildcard(require("react")); var _react2 = require("@slashid/react"); var _DocSidebarItem = _interopRequireDefault(require("@theme-init/DocSidebarItem")); var _domain = require("../../domain"); var _useSlashIDConfig = require("../hooks/useSlashIDConfig"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /* ============================================================================ * Copyright (c) SlashID * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * ========================================================================== */ function getSlashIDProps(item) { var _item$customProps; const props = item === null || item === void 0 ? void 0 : (_item$customProps = item.customProps) === null || _item$customProps === void 0 ? void 0 : _item$customProps.slashid; if (!props) return undefined; return props; } function DocSidebarItemWrapper(props) { const { user } = (0, _react2.useSlashID)(); const slashIDProps = getSlashIDProps(props.item); const config = (0, _useSlashIDConfig.useSlashIDConfig)(); const Component = (0, _react.useMemo)(() => { // path takes precedence if (!(0, _domain.isHtmlSidebarItem)(props.item) && !(0, _domain.shouldPathRender)(props.item.href, config.privatePaths, user)) { return null; } if ((0, _domain.isCategory)(props.item) && (0, _domain.shouldNoItemsRender)(props.item.items, user, getSlashIDProps)) { return null; } if (!slashIDProps || !slashIDProps.auth) { return <_DocSidebarItem.default {...props} />; } const { auth, groups } = slashIDProps; if (auth && !user) { return null; } if (groups) { const belongsTo = userGroups => groups.every(group => userGroups.includes(group)); return <_react2.Groups belongsTo={belongsTo}> <_DocSidebarItem.default {...props} /> </_react2.Groups>; } return null; }, [config.privatePaths, props, slashIDProps, user]); return Component; }