@slashid/docusaurus-theme-slashid
Version:
SlashID theme for Docusaurus.
61 lines (59 loc) • 1.83 kB
JavaScript
/* ============================================================================
* 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.
* ========================================================================== */
import React, { useMemo } from "react";
import { useSlashID, Groups } from "@slashid/react";
import DocSidebarItem from "@theme-init/DocSidebarItem";
import {
isCategory,
isHtmlSidebarItem,
shouldNoItemsRender,
shouldPathRender,
} from "../../domain";
import { useSlashIDConfig } from "../hooks/useSlashIDConfig";
function getSlashIDProps(item) {
const props = item?.customProps?.slashid;
if (!props) return undefined;
return props;
}
export default function DocSidebarItemWrapper(props) {
const { user } = useSlashID();
const slashIDProps = getSlashIDProps(props.item);
const config = useSlashIDConfig();
const Component = useMemo(() => {
// path takes precedence
if (
!isHtmlSidebarItem(props.item) &&
!shouldPathRender(props.item.href, config.privatePaths, user)
) {
return null;
}
if (
isCategory(props.item) &&
shouldNoItemsRender(props.item.items, user, getSlashIDProps)
) {
return null;
}
if (!slashIDProps || !slashIDProps.auth) {
return <DocSidebarItem {...props} />;
}
const { auth, groups } = slashIDProps;
if (auth && !user) {
return null;
}
if (groups) {
const belongsTo = (userGroups) =>
groups.every((group) => userGroups.includes(group));
return (
<Groups belongsTo={belongsTo}>
<DocSidebarItem {...props} />
</Groups>
);
}
return null;
}, [config.privatePaths, props, slashIDProps, user]);
return Component;
}