InAppNotification.tsx 2.4 KB

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