ExplorerItem.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* eslint-disable react/prop-types */
  2. import React from 'react';
  3. import { ADMIN_URLS, STRINGS, LOCALE_NAMES } from '../../config/wagtailConfig';
  4. import Icon from '../../components/Icon/Icon';
  5. import Button from '../../components/Button/Button';
  6. import PublicationStatus from '../../components/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 = (
  10. <Icon name="folder-inverse" className="icon--menuitem" />
  11. );
  12. interface ExplorerItemProps {
  13. item: PageState;
  14. onClick(): void;
  15. }
  16. /**
  17. * One menu item in the page explorer, with different available actions
  18. * and information depending on the metadata of the page.
  19. */
  20. const ExplorerItem: React.FunctionComponent<ExplorerItemProps> = ({ item, onClick }) => {
  21. const { id, admin_display_title: title, meta } = item;
  22. const hasChildren = meta.children.count > 0;
  23. const isPublished = meta.status.live && !meta.status.has_unpublished_changes;
  24. const localeName = meta.parent?.id === 1 && meta.locale && (LOCALE_NAMES.get(meta.locale) || meta.locale);
  25. return (
  26. <div className="c-explorer__item">
  27. <Button href={`${ADMIN_URLS.PAGES}${id}/`} className="c-explorer__item__link">
  28. {hasChildren ? childrenIcon : null}
  29. <h3 className="c-explorer__item__title">
  30. {title}
  31. </h3>
  32. {(!isPublished || localeName) &&
  33. <span className="c-explorer__meta">
  34. {localeName && <span className="o-pill c-status">{localeName}</span>}
  35. {!isPublished && <PublicationStatus status={meta.status} />}
  36. </span>
  37. }
  38. </Button>
  39. <Button
  40. href={`${ADMIN_URLS.PAGES}${id}/edit/`}
  41. className="c-explorer__item__action c-explorer__item__action--small"
  42. >
  43. <Icon name="edit" title={STRINGS.EDIT_PAGE.replace('{title}', title)} className="icon--item-action" />
  44. </Button>
  45. {hasChildren ? (
  46. <Button
  47. className="c-explorer__item__action"
  48. onClick={onClick}
  49. href={`${ADMIN_URLS.PAGES}${id}/`}
  50. >
  51. <Icon
  52. name="arrow-right"
  53. title={STRINGS.VIEW_CHILD_PAGES_OF_PAGE.replace('{title}', title)}
  54. className="icon--item-action"
  55. />
  56. </Button>
  57. ) : null}
  58. </div>
  59. );
  60. };
  61. export default ExplorerItem;