PageExplorerItem.tsx 2.6 KB

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