import React, { memo, useCallback } from 'react'; import Link from 'next/link'; import urljoin from 'url-join'; import LinkedPagePath from '../models/linked-page-path'; type PagePathHierarchicalLinkProps = { linkedPagePath: LinkedPagePath, linkedPagePathByHtml?: LinkedPagePath, basePath?: string, isInTrash?: boolean, // !!INTERNAL USE ONLY!! isInnerElem?: boolean, }; // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types const PagePathHierarchicalLink = memo((props: PagePathHierarchicalLinkProps): JSX.Element => { const { linkedPagePath, linkedPagePathByHtml, basePath, isInTrash, isInnerElem, } = props; // eslint-disable-next-line react/prop-types const RootElm = useCallback(({ children }) => { return isInnerElem ? <>{children} : {children}; }, [isInnerElem]); // render root element if (linkedPagePath.isRoot) { if (basePath != null) { return <>; } return isInTrash ? ( / ) : ( / ); } const isParentExists = linkedPagePath.parent != null; const isParentRoot = linkedPagePath.parent?.isRoot; const isSeparatorRequired = isParentExists && !isParentRoot; const shouldDangerouslySetInnerHTML = linkedPagePathByHtml != null; const href = encodeURI(urljoin(basePath || '/', linkedPagePath.href)); return ( { isParentExists && ( ) } { isSeparatorRequired && ( / ) } { shouldDangerouslySetInnerHTML // eslint-disable-next-line react/no-danger ? : {linkedPagePath.pathName} } ); }); PagePathHierarchicalLink.displayName = 'PagePathHierarchicalLink'; export default PagePathHierarchicalLink;