PageExplorerItem.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import React from 'react';
  2. import { gettext } from '../../utils/gettext';
  3. import { ADMIN_URLS, LOCALE_NAMES } from '../../config/wagtailConfig';
  4. import Icon from '../Icon/Icon';
  5. import Link from '../Link/Link';
  6. import PublicationStatus from '../PublicationStatus/PublicationStatus';
  7. import { PageState } from './reducers/nodes';
  8. // Hoist icons in the explorer item, as it is re-rendered many times.
  9. const childrenIcon = <Icon name="folder-inverse" className="icon--menuitem" />;
  10. interface PageExplorerItemProps {
  11. item: PageState;
  12. onClick(): void;
  13. navigate(url: string): Promise<void>;
  14. }
  15. /**
  16. * One menu item in the page explorer, with different available actions
  17. * and information depending on the metadata of the page.
  18. */
  19. const PageExplorerItem: React.FunctionComponent<PageExplorerItemProps> = ({
  20. item,
  21. onClick,
  22. navigate,
  23. }) => {
  24. const { id, admin_display_title: title, meta } = item;
  25. const hasChildren = meta.children.count > 0;
  26. const isPublished = meta.status.live && !meta.status.has_unpublished_changes;
  27. const localeName =
  28. meta.parent?.id === 1 &&
  29. meta.locale &&
  30. (LOCALE_NAMES.get(meta.locale) || meta.locale);
  31. return (
  32. <div className="c-page-explorer__item">
  33. <Link
  34. href={`${ADMIN_URLS.PAGES}${id}/`}
  35. navigate={navigate}
  36. className="c-page-explorer__item__link"
  37. >
  38. {hasChildren ? childrenIcon : null}
  39. <h3 className="c-page-explorer__item__title">{title}</h3>
  40. {(!isPublished || localeName) && (
  41. <span className="c-page-explorer__meta">
  42. {localeName && <span className="c-status">{localeName}</span>}
  43. {!isPublished && <PublicationStatus status={meta.status} />}
  44. </span>
  45. )}
  46. </Link>
  47. <Link
  48. href={`${ADMIN_URLS.PAGES}${id}/edit/`}
  49. className="c-page-explorer__item__action c-page-explorer__item__action--small"
  50. navigate={navigate}
  51. >
  52. <Icon
  53. name="edit"
  54. title={gettext("Edit '%(title)s'").replace('%(title)s', title || '')}
  55. className="icon--item-action"
  56. />
  57. </Link>
  58. {hasChildren ? (
  59. <Link
  60. className="c-page-explorer__item__action"
  61. onClick={onClick}
  62. href={`${ADMIN_URLS.PAGES}${id}/`}
  63. navigate={navigate}
  64. >
  65. <Icon
  66. name="arrow-right"
  67. title={gettext("View child pages of '%(title)s'").replace(
  68. '%(title)s',
  69. title || '',
  70. )}
  71. className="icon--item-action"
  72. />
  73. </Link>
  74. ) : null}
  75. </div>
  76. );
  77. };
  78. export default PageExplorerItem;