InAppNotification.tsx 2.2 KB

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