PagePathHierarchicalLink.jsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import urljoin from 'url-join';
  4. import LinkedPagePath from '../models/linked-page-path';
  5. const PagePathHierarchicalLink = (props) => {
  6. const { linkedPagePath, basePath } = props;
  7. // render root element
  8. if (linkedPagePath.isRoot) {
  9. if (basePath != null) {
  10. return null;
  11. }
  12. return props.isPageInTrash
  13. ? (
  14. <>
  15. <span className="path-segment">
  16. <a href="/trash"><i className="icon-trash"></i></a>
  17. </span>
  18. <span className="separator"><a href="/">/</a></span>
  19. </>
  20. )
  21. : (
  22. <>
  23. <span className="path-segment">
  24. <a href="/">
  25. <i className="icon-home"></i>
  26. <span className="separator">/</span>
  27. </a>
  28. </span>
  29. </>
  30. );
  31. }
  32. const isParentExists = linkedPagePath.parent != null;
  33. const isParentRoot = isParentExists && linkedPagePath.parent.isRoot;
  34. const isSeparatorRequired = isParentExists && !isParentRoot;
  35. const href = encodeURI(urljoin(basePath || '/', linkedPagePath.href));
  36. return (
  37. <>
  38. { isParentExists && (
  39. <PagePathHierarchicalLink linkedPagePath={linkedPagePath.parent} basePath={basePath} />
  40. ) }
  41. { isSeparatorRequired && (
  42. <span className="separator">/</span>
  43. ) }
  44. <a className="page-segment" href={href}>{linkedPagePath.pathName}</a>
  45. </>
  46. );
  47. };
  48. PagePathHierarchicalLink.propTypes = {
  49. linkedPagePath: PropTypes.instanceOf(LinkedPagePath).isRequired,
  50. basePath: PropTypes.string,
  51. isPageInTrash: PropTypes.bool, // TODO: omit
  52. };
  53. export default PagePathHierarchicalLink;