PageModelNotification.tsx 1.6 KB

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