PageModelNotification.tsx 1.9 KB

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