InAppNotification.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 = () => {
  32. const actionUsers = notification.actionUsers;
  33. if (actionUsers.length < 1) {
  34. // what is this case?
  35. return '';
  36. }
  37. return <UserPicture user={actionUsers[0]} size="md" noTooltip />;
  38. };
  39. const componentName = `${notification.targetModel}:${notification.action}`;
  40. const propsNew = {
  41. actionUsers: getActionUsers(),
  42. ...props,
  43. };
  44. const renderInAppNotificationContent = () => {
  45. switch (componentName) {
  46. case 'Page:COMMENT':
  47. return <PageCommentNotification {...propsNew} onClick={props.onClick(props.notification)} />;
  48. default:
  49. return <></>;
  50. }
  51. };
  52. return (
  53. <>
  54. <div>
  55. {renderUserImage()}
  56. {renderInAppNotificationContent()}
  57. </div>
  58. <FormattedDistanceDate id={props.notification._id} date={props.notification.createdAt} isShowTooltip={false} />
  59. </>
  60. );
  61. };