InAppNotification.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import React from 'react';
  2. import { UserPicture } from '@growi/ui';
  3. import { PageCommentInAppNotification } from './PageCommentInAppNotification';
  4. import { PageUpdateInAppNotification } from './PageUpdateInAppNotification';
  5. import { InAppNotification as IInAppNotification } from '../../interfaces/in-app-notification';
  6. import FormattedDistanceDate from '../FormattedDistanceDate';
  7. interface Props {
  8. notification: IInAppNotification
  9. onClick: any
  10. }
  11. export const InAppNotification = (props: Props): JSX.Element => {
  12. const { notification } = props;
  13. // TODO get actionUsers with mongoose virtual method by #79077
  14. const getActionUsers = () => {
  15. const latestActionUsers = notification.actionUsers.slice(0, 3);
  16. const latestUsers = latestActionUsers.map((user) => {
  17. return `@${user.name}`;
  18. });
  19. let actionedUsers = '';
  20. const latestUsersCount = latestUsers.length;
  21. if (latestUsersCount === 1) {
  22. actionedUsers = latestUsers[0];
  23. }
  24. else if (notification.actionUsers.length >= 4) {
  25. actionedUsers = `${latestUsers.slice(0, 2).join(', ')} and ${notification.actionUsers.length - 2} others`;
  26. }
  27. else {
  28. actionedUsers = latestUsers.join(', ');
  29. }
  30. return actionedUsers;
  31. };
  32. const renderUserImage = (): JSX.Element => {
  33. const actionUsers = notification.actionUsers;
  34. if (actionUsers.length < 1) {
  35. return <></>;
  36. }
  37. return <UserPicture user={actionUsers[0]} size="md" noTooltip />;
  38. };
  39. const componentName = `${notification.targetModel}:${notification.action}`;
  40. const propsNew = {
  41. actionUsers: getActionUsers(),
  42. ...props,
  43. };
  44. const renderInAppNotificationContent = (): JSX.Element => {
  45. switch (componentName) {
  46. case 'Page:COMMENT':
  47. return <PageCommentInAppNotification {...propsNew} onClick={props.onClick(props.notification)} />;
  48. case 'Page:UPDATE':
  49. return <PageUpdateInAppNotification {...propsNew} onClick={props.onClick(props.notification)} />;
  50. default:
  51. return <></>;
  52. }
  53. };
  54. return (
  55. <>
  56. <div className="dropdown-item d-flex flex-row mb-3">
  57. <div className="p-2 d-flex align-items-center">
  58. {renderUserImage()}
  59. </div>
  60. <div className="p-2">
  61. {renderInAppNotificationContent()}
  62. <div>
  63. <FormattedDistanceDate id={props.notification._id} date={props.notification.createdAt} isShowTooltip={false} />
  64. </div>
  65. </div>
  66. </div>
  67. </>
  68. );
  69. };