PageModelNotification.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import React, {
  2. forwardRef, ForwardRefRenderFunction, useImperativeHandle,
  3. } from 'react';
  4. import { HasObjectId } from '@growi/core';
  5. import { PagePathLabel } from '@growi/ui';
  6. import { useRouter } from 'next/router';
  7. import { IInAppNotificationOpenable } from '~/client/interfaces/in-app-notification-openable';
  8. import { IInAppNotification } from '~/interfaces/in-app-notification';
  9. import { parseSnapshot } from '../../../models/serializers/in-app-notification-snapshot/page';
  10. import FormattedDistanceDate from '../../FormattedDistanceDate';
  11. interface Props {
  12. notification: IInAppNotification & HasObjectId
  13. actionMsg: string
  14. actionIcon: string
  15. actionUsers: string
  16. }
  17. const PageModelNotification: ForwardRefRenderFunction<IInAppNotificationOpenable, Props> = (props: Props, ref) => {
  18. const {
  19. notification, actionMsg, actionIcon, actionUsers,
  20. } = props;
  21. const router = useRouter();
  22. const snapshot = parseSnapshot(notification.snapshot);
  23. // publish open()
  24. useImperativeHandle(ref, () => ({
  25. open() {
  26. if (notification.target != null) {
  27. // jump to target page
  28. const targetPagePath = notification.target.path;
  29. if (targetPagePath != null) {
  30. router.push(targetPagePath);
  31. }
  32. }
  33. },
  34. }));
  35. return (
  36. <div className="p-2 overflow-hidden">
  37. <div className="text-truncate">
  38. <b>{actionUsers}</b> {actionMsg} <PagePathLabel path={snapshot.path} />
  39. </div>
  40. <i className={`${actionIcon} mr-2`} />
  41. <FormattedDistanceDate
  42. id={notification._id}
  43. date={notification.createdAt}
  44. isShowTooltip={false}
  45. differenceForAvoidingFormat={Number.POSITIVE_INFINITY}
  46. />
  47. </div>
  48. );
  49. };
  50. export default forwardRef(PageModelNotification);