RenderPageModelNotification.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import React, { useCallback } from 'react';
  2. import { PagePathLabel } from '@growi/ui';
  3. import { apiv3Post } from '~/client/util/apiv3-client';
  4. import { IInAppNotification } from '~/interfaces/in-app-notification';
  5. import { HasObjectId } from '~/interfaces/has-object-id';
  6. import FormattedDistanceDate from '../../FormattedDistanceDate';
  7. interface Props {
  8. notification: IInAppNotification & HasObjectId
  9. actionMsg: string
  10. actionIcon: string
  11. actionUsers: string
  12. }
  13. export const RenderPageModelNotification = (props: Props): JSX.Element => {
  14. const {
  15. notification, actionMsg, actionIcon, actionUsers,
  16. } = props;
  17. const snapshot = JSON.parse(notification.snapshot);
  18. const pagePath = { path: snapshot.path };
  19. const notificationClickHandler = useCallback(() => {
  20. // set notification status "OPEND"
  21. apiv3Post('/in-app-notification/open', { id: notification._id });
  22. // jump to target page
  23. const targetPagePath = notification.target?.path;
  24. if (targetPagePath != null) {
  25. window.location.href = targetPagePath;
  26. }
  27. }, []);
  28. return (
  29. <div className="p-2">
  30. <div onClick={notificationClickHandler}>
  31. <div>
  32. <b>{actionUsers}</b> {actionMsg} <PagePathLabel page={pagePath} />
  33. </div>
  34. <i className={`${actionIcon} mr-2`} />
  35. <FormattedDistanceDate
  36. id={notification._id}
  37. date={notification.createdAt}
  38. isShowTooltip={false}
  39. differenceForAvoidingFormat={Number.POSITIVE_INFINITY}
  40. />
  41. </div>
  42. </div>
  43. );
  44. };