Просмотр исходного кода

create useModelNotification hooks

WNomunomu 2 лет назад
Родитель
Сommit
72ac5a8505
1 измененных файлов с 63 добавлено и 0 удалено
  1. 63 0
      apps/app/src/components/InAppNotification/PageNotification/index.tsx

+ 63 - 0
apps/app/src/components/InAppNotification/PageNotification/index.tsx

@@ -0,0 +1,63 @@
+import { FC } from 'react';
+
+import type { HasObjectId, IPage, IUser } from '@growi/core';
+import { useRouter } from 'next/router';
+
+
+import { SupportedTargetModel } from '~/interfaces/activity';
+import { IInAppNotification } from '~/interfaces/in-app-notification';
+
+
+import PageModelNotification from './PageModelNotification';
+import UserModelNotification from './UserModelNotification';
+
+type ModelNotificationUtils = {
+  Notification: FC
+  publishOpen: () => void
+}
+
+export const useModelNotification = (notification: IInAppNotification & HasObjectId, targetModel: string): ModelNotificationUtils => {
+
+  const router = useRouter();
+
+  let Notification;
+  let publishOpen;
+
+  switch (targetModel) {
+    case SupportedTargetModel.MODEL_PAGE:
+
+      Notification = () => {
+        return <PageModelNotification notification={notification as IInAppNotification<IPage> & HasObjectId} />;
+      };
+
+      publishOpen = () => {
+        if (notification.target != null) {
+          // jump to target page
+          const targetPagePath = (notification.target as IPage).path;
+          if (targetPagePath != null) {
+            router.push(targetPagePath);
+          }
+        }
+      };
+
+      break;
+
+    case SupportedTargetModel.MODEL_USER:
+
+      Notification = () => {
+        return <UserModelNotification notification={notification as IInAppNotification<IUser> & HasObjectId} />;
+      };
+
+      publishOpen = () => {
+        router.push('/admin/users');
+      };
+
+      break;
+  }
+
+  return {
+    Notification,
+    publishOpen,
+  };
+
+};