PageModelNotification.tsx 1.6 KB

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