InAppNotificationElm.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import React, { useCallback } from 'react';
  2. import { UserPicture, PagePathLabel } from '@growi/ui';
  3. import { IInAppNotification } from '~/interfaces/in-app-notification';
  4. import { HasObjectId } from '~/interfaces/has-object-id';
  5. import { apiv3Post } from '~/client/util/apiv3-client';
  6. import FormattedDistanceDate from '../FormattedDistanceDate';
  7. interface Props {
  8. notification: IInAppNotification & HasObjectId
  9. }
  10. const InAppNotificationElm = (props: Props): JSX.Element => {
  11. const { notification } = props;
  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 renderActionUserPictures = (): 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 notificationClickHandler = useCallback(() => {
  48. // set notification status "OPEND"
  49. apiv3Post('/in-app-notification/open', { id: notification._id });
  50. // jump to target page
  51. window.location.href = notification.target.path;
  52. }, []);
  53. const actionUsers = getActionUsers();
  54. const pagePath = { path: props.notification.target.path };
  55. const actionType: string = notification.action;
  56. let actionMsg: string;
  57. let actionIcon: string;
  58. switch (actionType) {
  59. case 'PAGE_UPDATE':
  60. actionMsg = 'updated on';
  61. actionIcon = 'ti-agenda';
  62. break;
  63. case 'COMMENT_CREATE':
  64. actionMsg = 'commented on';
  65. actionIcon = 'icon-bubble';
  66. break;
  67. default:
  68. actionMsg = '';
  69. actionIcon = '';
  70. }
  71. return (
  72. <div className="dropdown-item d-flex flex-row mb-3">
  73. <div className="p-2 mr-2 d-flex align-items-center">
  74. {renderActionUserPictures()}
  75. </div>
  76. <div className="p-2">
  77. <div onClick={notificationClickHandler}>
  78. <div>
  79. <b>{actionUsers}</b> {actionMsg} <PagePathLabel page={pagePath} />
  80. </div>
  81. <i className={`${actionIcon} mr-2`} />
  82. <FormattedDistanceDate
  83. id={notification._id}
  84. date={notification.createdAt}
  85. isShowTooltip={false}
  86. differenceForAvoidingFormat={Number.POSITIVE_INFINITY}
  87. />
  88. </div>
  89. </div>
  90. </div>
  91. );
  92. };
  93. export default InAppNotificationElm;