InAppNotification.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. interface Props {
  6. notification: IInAppNotification
  7. onClick: any
  8. }
  9. export const InAppNotification = (props: Props): JSX.Element => {
  10. const { notification } = props;
  11. // TODO get actionUsers with mongoose virtual method by #79077
  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 renderUserImage = () => {
  31. const actionUsers = notification.actionUsers;
  32. if (actionUsers.length < 1) {
  33. // what is this case?
  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. switch (componentName) {
  44. case 'Page:COMMENT':
  45. return (
  46. <>
  47. {renderUserImage()}
  48. <PageCommentNotification {...propsNew} onClick={props.onClick(props.notification)} />
  49. </>
  50. );
  51. default:
  52. return <></>;
  53. }
  54. };