InAppNotificationElm.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import React, {
  2. FC, useRef,
  3. } from 'react';
  4. import { HasObjectId } from '@growi/core';
  5. import { UserPicture } from '@growi/ui/dist/components/User/UserPicture';
  6. import { DropdownItem } from 'reactstrap';
  7. import { IInAppNotificationOpenable } from '~/client/interfaces/in-app-notification-openable';
  8. import { apiv3Post } from '~/client/util/apiv3-client';
  9. import { IInAppNotification, InAppNotificationStatuses } from '~/interfaces/in-app-notification';
  10. // Change the display for each targetmodel
  11. import PageModelNotification from './PageNotification/PageModelNotification';
  12. interface Props {
  13. notification: IInAppNotification & HasObjectId
  14. elemClassName?: string,
  15. type?: 'button' | 'dropdown-item',
  16. }
  17. const InAppNotificationElm: FC<Props> = (props: Props) => {
  18. const { notification } = props;
  19. const notificationRef = useRef<IInAppNotificationOpenable>(null);
  20. const clickHandler = async(notification: IInAppNotification & HasObjectId): Promise<void> => {
  21. if (notification.status === InAppNotificationStatuses.STATUS_UNOPENED) {
  22. // set notification status "OPEND"
  23. await apiv3Post('/in-app-notification/open', { id: notification._id });
  24. }
  25. const currentInstance = notificationRef.current;
  26. if (currentInstance != null) {
  27. currentInstance.open();
  28. }
  29. };
  30. const getActionUsers = () => {
  31. const latestActionUsers = notification.actionUsers.slice(0, 3);
  32. const latestUsers = latestActionUsers.map((user) => {
  33. return `@${user.name}`;
  34. });
  35. let actionedUsers = '';
  36. const latestUsersCount = latestUsers.length;
  37. if (latestUsersCount === 1) {
  38. actionedUsers = latestUsers[0];
  39. }
  40. else if (notification.actionUsers.length >= 4) {
  41. actionedUsers = `${latestUsers.slice(0, 2).join(', ')} and ${notification.actionUsers.length - 2} others`;
  42. }
  43. else {
  44. actionedUsers = latestUsers.join(', ');
  45. }
  46. return actionedUsers;
  47. };
  48. const renderActionUserPictures = (): JSX.Element => {
  49. const actionUsers = notification.actionUsers;
  50. if (actionUsers.length < 1) {
  51. return <></>;
  52. }
  53. if (actionUsers.length === 1) {
  54. return <UserPicture user={actionUsers[0]} size="md" noTooltip />;
  55. }
  56. return (
  57. <div className="position-relative">
  58. <UserPicture user={actionUsers[0]} size="md" noTooltip />
  59. <div className="position-absolute" style={{ top: 10, left: 10 }}>
  60. <UserPicture user={actionUsers[1]} size="md" noTooltip />
  61. </div>
  62. </div>
  63. );
  64. };
  65. const actionUsers = getActionUsers();
  66. const actionType: string = notification.action;
  67. let actionMsg: string;
  68. let actionIcon: string;
  69. switch (actionType) {
  70. case 'PAGE_LIKE':
  71. actionMsg = 'liked';
  72. actionIcon = 'icon-like';
  73. break;
  74. case 'PAGE_BOOKMARK':
  75. actionMsg = 'bookmarked on';
  76. actionIcon = 'icon-star';
  77. break;
  78. case 'PAGE_UPDATE':
  79. actionMsg = 'updated on';
  80. actionIcon = 'ti ti-agenda';
  81. break;
  82. case 'PAGE_RENAME':
  83. actionMsg = 'renamed';
  84. actionIcon = 'icon-action-redo';
  85. break;
  86. case 'PAGE_DUPLICATE':
  87. actionMsg = 'duplicated';
  88. actionIcon = 'icon-docs';
  89. break;
  90. case 'PAGE_DELETE':
  91. actionMsg = 'deleted';
  92. actionIcon = 'icon-trash';
  93. break;
  94. case 'PAGE_DELETE_COMPLETELY':
  95. actionMsg = 'completely deleted';
  96. actionIcon = 'icon-fire';
  97. break;
  98. case 'PAGE_REVERT':
  99. actionMsg = 'reverted';
  100. actionIcon = 'icon-action-undo';
  101. break;
  102. case 'PAGE_RECURSIVELY_RENAME':
  103. actionMsg = 'renamed under';
  104. actionIcon = 'icon-action-redo';
  105. break;
  106. case 'PAGE_RECURSIVELY_DELETE':
  107. actionMsg = 'deleted under';
  108. actionIcon = 'icon-trash';
  109. break;
  110. case 'PAGE_RECURSIVELY_DELETE_COMPLETELY':
  111. actionMsg = 'deleted completely under';
  112. actionIcon = 'icon-fire';
  113. break;
  114. case 'PAGE_RECURSIVELY_REVERT':
  115. actionMsg = 'reverted under';
  116. actionIcon = 'icon-action-undo';
  117. break;
  118. case 'COMMENT_CREATE':
  119. actionMsg = 'commented on';
  120. actionIcon = 'icon-bubble';
  121. break;
  122. default:
  123. actionMsg = '';
  124. actionIcon = '';
  125. }
  126. const isDropdownItem = props.type === 'dropdown-item';
  127. // determine tag
  128. const TagElem = isDropdownItem
  129. ? DropdownItem
  130. // eslint-disable-next-line react/prop-types
  131. : props => <button type="button" {...props}>{props.children}</button>;
  132. return (
  133. <TagElem className={props.elemClassName} onClick={() => clickHandler(notification)}>
  134. <div className="d-flex align-items-center">
  135. <span
  136. className={`${notification.status === InAppNotificationStatuses.STATUS_UNOPENED
  137. ? 'grw-unopend-notification'
  138. : 'ml-2'
  139. } rounded-circle mr-3`}
  140. >
  141. </span>
  142. {renderActionUserPictures()}
  143. {notification.targetModel === 'Page' && (
  144. <PageModelNotification
  145. ref={notificationRef}
  146. notification={notification}
  147. actionMsg={actionMsg}
  148. actionIcon={actionIcon}
  149. actionUsers={actionUsers}
  150. />
  151. )}
  152. </div>
  153. </TagElem>
  154. );
  155. };
  156. export default InAppNotificationElm;