InAppNotification.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. switch (componentName) {
  45. case 'Page:COMMENT':
  46. return (
  47. <>
  48. <div>
  49. {renderUserImage()}
  50. <PageCommentNotification {...propsNew} onClick={props.onClick(props.notification)} />
  51. </div>
  52. <FormattedDistanceDate id={props.notification._id} date={props.notification.createdAt} isShowTooltip={false} />
  53. </>
  54. );
  55. default:
  56. return <></>;
  57. }
  58. };