|
|
@@ -1,29 +1,27 @@
|
|
|
import React, {
|
|
|
- forwardRef, ForwardRefRenderFunction, useCallback,
|
|
|
+ FC, useCallback,
|
|
|
} from 'react';
|
|
|
|
|
|
import type { IPage, HasObjectId } from '@growi/core';
|
|
|
import { useRouter } from 'next/router';
|
|
|
|
|
|
-import type { IInAppNotificationOpenable } from '~/client/interfaces/in-app-notification-openable';
|
|
|
+import { SupportedTargetModel } from '~/interfaces/activity';
|
|
|
import type { IInAppNotification } from '~/interfaces/in-app-notification';
|
|
|
import * as pageSerializers from '~/models/serializers/in-app-notification-snapshot/page';
|
|
|
|
|
|
import { ModelNotification } from './ModelNotification';
|
|
|
-import { useActionMsgAndIconForPageModelNotification } from './useActionAndMsg';
|
|
|
+import { useActionMsgAndIconForModelNotification } from './useActionAndMsg';
|
|
|
|
|
|
|
|
|
-interface Props {
|
|
|
- notification: IInAppNotification<IPage> & HasObjectId
|
|
|
+export interface ModelNotificationUtils {
|
|
|
+ Notification: FC
|
|
|
+ publishOpen: () => void
|
|
|
}
|
|
|
|
|
|
-const PageModelNotification: ForwardRefRenderFunction<IInAppNotificationOpenable, Props> = (props: Props, ref) => {
|
|
|
-
|
|
|
- const { notification } = props;
|
|
|
-
|
|
|
- const { actionMsg, actionIcon } = useActionMsgAndIconForPageModelNotification(notification);
|
|
|
+export const usePageModelNotification = (notification: IInAppNotification & HasObjectId): ModelNotificationUtils | null => {
|
|
|
|
|
|
const router = useRouter();
|
|
|
+ const { actionMsg, actionIcon } = useActionMsgAndIconForModelNotification(notification);
|
|
|
|
|
|
const getActionUsers = useCallback(() => {
|
|
|
const latestActionUsers = notification.actionUsers.slice(0, 3);
|
|
|
@@ -46,31 +44,42 @@ const PageModelNotification: ForwardRefRenderFunction<IInAppNotificationOpenable
|
|
|
return actionedUsers;
|
|
|
}, [notification.actionUsers]);
|
|
|
|
|
|
+ const isPageModelNotification = (notification: IInAppNotification & HasObjectId): notification is IInAppNotification<IPage> & HasObjectId => {
|
|
|
+ return notification.targetModel === SupportedTargetModel.MODEL_PAGE;
|
|
|
+ };
|
|
|
+
|
|
|
+ if (!isPageModelNotification(notification)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
const actionUsers = getActionUsers();
|
|
|
|
|
|
- // publish open()
|
|
|
+ notification.parsedSnapshot = pageSerializers.parseSnapshot(notification.snapshot);
|
|
|
+
|
|
|
+ const Notification = () => {
|
|
|
+ return (
|
|
|
+ <ModelNotification
|
|
|
+ notification={notification}
|
|
|
+ actionMsg={actionMsg}
|
|
|
+ actionIcon={actionIcon}
|
|
|
+ actionUsers={actionUsers}
|
|
|
+ />
|
|
|
+ );
|
|
|
+ };
|
|
|
+
|
|
|
const publishOpen = () => {
|
|
|
if (notification.target != null) {
|
|
|
// jump to target page
|
|
|
- const targetPagePath = notification.target.path;
|
|
|
+ const targetPagePath = (notification.target as IPage).path;
|
|
|
if (targetPagePath != null) {
|
|
|
router.push(targetPagePath);
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- notification.parsedSnapshot = pageSerializers.parseSnapshot(notification.snapshot);
|
|
|
+ return {
|
|
|
+ Notification,
|
|
|
+ publishOpen,
|
|
|
+ };
|
|
|
|
|
|
- return (
|
|
|
- <ModelNotification
|
|
|
- notification={notification}
|
|
|
- actionMsg={actionMsg}
|
|
|
- actionIcon={actionIcon}
|
|
|
- actionUsers={actionUsers}
|
|
|
- publishOpen={publishOpen}
|
|
|
- ref={ref}
|
|
|
- />
|
|
|
- );
|
|
|
};
|
|
|
-
|
|
|
-export default forwardRef(PageModelNotification);
|