AuthorInfo.jsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { format } from 'date-fns';
  4. import { UserPicture } from '@growi/ui';
  5. import { pagePathUtils } from '@growi/core';
  6. const { userPageRoot } = pagePathUtils;
  7. const formatType = 'yyyy/MM/dd HH:mm';
  8. const AuthorInfo = (props) => {
  9. const {
  10. mode, user, date, locate,
  11. } = props;
  12. const infoLabelForSubNav = mode === 'create'
  13. ? 'Created by'
  14. : 'Updated by';
  15. const infoLabelForFooter = mode === 'create'
  16. ? 'Created at'
  17. : 'Last revision posted at';
  18. const userLabel = user != null
  19. ? <a href={userPageRoot(user)}>{user.name}</a>
  20. : <i>Unknown</i>;
  21. if (locate === 'footer') {
  22. return <p>{infoLabelForFooter} {format(new Date(date), formatType)} by <UserPicture user={user} size="sm" /> {userLabel}</p>;
  23. }
  24. return (
  25. <div className="d-flex align-items-center">
  26. <div className="mr-2">
  27. <UserPicture user={user} size="sm" />
  28. </div>
  29. <div>
  30. <div>{infoLabelForSubNav} {userLabel}</div>
  31. <div className="text-muted text-date">{format(new Date(date), formatType)}</div>
  32. </div>
  33. </div>
  34. );
  35. };
  36. AuthorInfo.propTypes = {
  37. date: PropTypes.string.isRequired,
  38. user: PropTypes.object,
  39. mode: PropTypes.oneOf(['create', 'update']),
  40. locate: PropTypes.oneOf(['subnav', 'footer']),
  41. };
  42. AuthorInfo.defaultProps = {
  43. mode: 'create',
  44. locate: 'subnav',
  45. };
  46. export default AuthorInfo;