PagePathNav.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { type JSX, useMemo } from 'react';
  2. import { DevidedPagePath } from '@growi/core/dist/models';
  3. import { pagePathUtils } from '@growi/core/dist/utils';
  4. import LinkedPagePath from '~/models/linked-page-path';
  5. import { PagePathHierarchicalLink } from '../PagePathHierarchicalLink';
  6. import type { PagePathNavLayoutProps } from './PagePathNavLayout';
  7. import { PagePathNavLayout } from './PagePathNavLayout';
  8. import styles from './PagePathNav.module.scss';
  9. const { isTrashPage } = pagePathUtils;
  10. const Separator = ({ className }: { className?: string }): JSX.Element => {
  11. return (
  12. <span className={`separator ${className ?? ''} ${styles['grw-mx-02em']}`}>
  13. /
  14. </span>
  15. );
  16. };
  17. export const PagePathNav = (props: PagePathNavLayoutProps): JSX.Element => {
  18. const { pagePath } = props;
  19. const isInTrash = isTrashPage(pagePath);
  20. const formerLink = useMemo(() => {
  21. const dPagePath = new DevidedPagePath(pagePath, false, true);
  22. // one line
  23. if (dPagePath.isRoot || dPagePath.isFormerRoot) {
  24. return undefined;
  25. }
  26. // two line
  27. const linkedPagePathFormer = new LinkedPagePath(dPagePath.former);
  28. return (
  29. <>
  30. <PagePathHierarchicalLink
  31. linkedPagePath={linkedPagePathFormer}
  32. isInTrash={isInTrash}
  33. />
  34. <Separator />
  35. </>
  36. );
  37. }, [isInTrash, pagePath]);
  38. const latterLink = useMemo(() => {
  39. const dPagePath = new DevidedPagePath(pagePath, false, true);
  40. // one line
  41. if (dPagePath.isRoot || dPagePath.isFormerRoot) {
  42. const linkedPagePath = new LinkedPagePath(pagePath);
  43. return (
  44. <PagePathHierarchicalLink
  45. linkedPagePath={linkedPagePath}
  46. isInTrash={isInTrash}
  47. />
  48. );
  49. }
  50. // two line
  51. const linkedPagePathLatter = new LinkedPagePath(dPagePath.latter);
  52. return (
  53. <PagePathHierarchicalLink
  54. linkedPagePath={linkedPagePathLatter}
  55. basePath={dPagePath.former}
  56. isInTrash={isInTrash}
  57. />
  58. );
  59. }, [isInTrash, pagePath]);
  60. return (
  61. <PagePathNavLayout
  62. {...props}
  63. formerLink={formerLink}
  64. latterLink={latterLink}
  65. />
  66. );
  67. };