@slashid/docusaurus-theme-slashid
Version:
SlashID theme for Docusaurus.
32 lines (30 loc) • 1.23 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 from "react";
import { Redirect } from "@docusaurus/router";
import { useSlashID } from "@slashid/react";
import DocItem from "@theme-init/DocItem";
import { shouldItemRender, shouldPathRender } from "../../domain";
import { useSlashIDConfig } from "../hooks/useSlashIDConfig";
function getSlashIDProps(docItemProps) {
const slashIDProps =
docItemProps?.content?.frontMatter?.sidebar_custom_props?.slashid;
if (!slashIDProps) return undefined;
return slashIDProps;
}
export default function DocItemWrapper(props) {
const { user } = useSlashID();
const config = useSlashIDConfig();
const redirectTo = config.privateRedirectPath;
if (!shouldPathRender(props.route.path, config.privatePaths, user)) {
return <Redirect to={redirectTo} />;
}
if (!shouldItemRender(getSlashIDProps(props), user)) {
return <Redirect to={redirectTo} />;
}
return <DocItem {...props} />;
}