PageModelNotification.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import React, {
  2. forwardRef, ForwardRefRenderFunction, useCallback,
  3. } from 'react';
  4. import type { IPage, HasObjectId } from '@growi/core';
  5. import { useRouter } from 'next/router';
  6. import type { IInAppNotificationOpenable } from '~/client/interfaces/in-app-notification-openable';
  7. import type { IInAppNotification } from '~/interfaces/in-app-notification';
  8. import * as pageSerializers from '~/models/serializers/in-app-notification-snapshot/page';
  9. import { ModelNotification } from './ModelNotification';
  10. import { useActionMsgAndIconForPageModelNotification } from './useActionAndMsg';
  11. interface Props {
  12. notification: IInAppNotification<IPage> & HasObjectId
  13. }
  14. const PageModelNotification: ForwardRefRenderFunction<IInAppNotificationOpenable, Props> = (props: Props, ref) => {
  15. const { notification } = props;
  16. const { actionMsg, actionIcon } = useActionMsgAndIconForPageModelNotification(notification);
  17. const router = useRouter();
  18. const getActionUsers = useCallback(() => {
  19. const latestActionUsers = notification.actionUsers.slice(0, 3);
  20. const latestUsers = latestActionUsers.map((user) => {
  21. return `@${user.name}`;
  22. });
  23. let actionedUsers = '';
  24. const latestUsersCount = latestUsers.length;
  25. if (latestUsersCount === 1) {
  26. actionedUsers = latestUsers[0];
  27. }
  28. else if (notification.actionUsers.length >= 4) {
  29. actionedUsers = `${latestUsers.slice(0, 2).join(', ')} and ${notification.actionUsers.length - 2} others`;
  30. }
  31. else {
  32. actionedUsers = latestUsers.join(', ');
  33. }
  34. return actionedUsers;
  35. }, [notification.actionUsers]);
  36. const actionUsers = getActionUsers();
  37. // publish open()
  38. const publishOpen = () => {
  39. if (notification.target != null) {
  40. // jump to target page
  41. const targetPagePath = notification.target.path;
  42. if (targetPagePath != null) {
  43. router.push(targetPagePath);
  44. }
  45. }
  46. };
  47. notification.parsedSnapshot = pageSerializers.parseSnapshot(notification.snapshot);
  48. return (
  49. <ModelNotification
  50. notification={notification}
  51. actionMsg={actionMsg}
  52. actionIcon={actionIcon}
  53. actionUsers={actionUsers}
  54. publishOpen={publishOpen}
  55. ref={ref}
  56. />
  57. );
  58. };
  59. export default forwardRef(PageModelNotification);