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