Explorar el Código

changed to remove conditional branches from index.tsx

WNomunomu hace 2 años
padre
commit
d40689831e

+ 8 - 1
apps/app/src/components/InAppNotification/InAppNotificationElm.tsx

@@ -20,7 +20,14 @@ const InAppNotificationElm: FC<Props> = (props: Props) => {
 
 
   const { notification } = props;
   const { notification } = props;
 
 
-  const { Notification, publishOpen } = useModelNotification(notification);
+  const modelNotificationUtils = useModelNotification(notification);
+
+  const Notification = modelNotificationUtils?.Notification;
+  const publishOpen = modelNotificationUtils?.publishOpen;
+
+  if (Notification == null || publishOpen == null) {
+    return <></>;
+  }
 
 
   const clickHandler = async(notification: IInAppNotification & HasObjectId): Promise<void> => {
   const clickHandler = async(notification: IInAppNotification & HasObjectId): Promise<void> => {
     if (notification.status === InAppNotificationStatuses.STATUS_UNOPENED) {
     if (notification.status === InAppNotificationStatuses.STATUS_UNOPENED) {

+ 44 - 19
apps/app/src/components/InAppNotification/PageNotification/PageModelNotification.tsx

@@ -1,26 +1,27 @@
 import React, {
 import React, {
-  forwardRef, ForwardRefRenderFunction, useCallback,
+  FC, useCallback,
 } from 'react';
 } from 'react';
 
 
 import type { IPage, HasObjectId } from '@growi/core';
 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 type { IInAppNotification } from '~/interfaces/in-app-notification';
 import * as pageSerializers from '~/models/serializers/in-app-notification-snapshot/page';
 import * as pageSerializers from '~/models/serializers/in-app-notification-snapshot/page';
 
 
 import { ModelNotification } from './ModelNotification';
 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) => {
+export const usePageModelNotification = (notification: IInAppNotification & HasObjectId): ModelNotificationUtils | null => {
 
 
-  const { notification } = props;
-
-  const { actionMsg, actionIcon } = useActionMsgAndIconForPageModelNotification(notification);
+  const router = useRouter();
+  const { actionMsg, actionIcon } = useActionMsgAndIconForModelNotification(notification);
 
 
   const getActionUsers = useCallback(() => {
   const getActionUsers = useCallback(() => {
     const latestActionUsers = notification.actionUsers.slice(0, 3);
     const latestActionUsers = notification.actionUsers.slice(0, 3);
@@ -43,18 +44,42 @@ const PageModelNotification: ForwardRefRenderFunction<IInAppNotificationOpenable
     return actionedUsers;
     return actionedUsers;
   }, [notification.actionUsers]);
   }, [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();
   const actionUsers = getActionUsers();
 
 
   notification.parsedSnapshot = pageSerializers.parseSnapshot(notification.snapshot);
   notification.parsedSnapshot = pageSerializers.parseSnapshot(notification.snapshot);
 
 
-  return (
-    <ModelNotification
-      notification={notification}
-      actionMsg={actionMsg}
-      actionIcon={actionIcon}
-      actionUsers={actionUsers}
-    />
-  );
-};
+  const Notification = () => {
+    return (
+      <ModelNotification
+        notification={notification}
+        actionMsg={actionMsg}
+        actionIcon={actionIcon}
+        actionUsers={actionUsers}
+      />
+    );
+  };
 
 
-export default forwardRef(PageModelNotification);
+  const publishOpen = () => {
+    if (notification.target != null) {
+      // jump to target page
+      const targetPagePath = (notification.target as IPage).path;
+      if (targetPagePath != null) {
+        router.push(targetPagePath);
+      }
+    }
+  };
+
+  return {
+    Notification,
+    publishOpen,
+  };
+
+};

+ 35 - 21
apps/app/src/components/InAppNotification/PageNotification/UserModelNotification.tsx

@@ -1,35 +1,49 @@
-import React, {
-  forwardRef, ForwardRefRenderFunction,
-} from 'react';
+import React from 'react';
 
 
 import type { IUser, HasObjectId } from '@growi/core';
 import type { IUser, 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 type { IInAppNotification } from '~/interfaces/in-app-notification';
 
 
 import { ModelNotification } from './ModelNotification';
 import { ModelNotification } from './ModelNotification';
-import { useActionMsgAndIconForUserModelNotification } from './useActionAndMsg';
+import { ModelNotificationUtils } from './PageModelNotification';
+import { useActionMsgAndIconForModelNotification } from './useActionAndMsg';
 
 
-interface Props {
-  notification: IInAppNotification<IUser> & HasObjectId
-}
 
 
-const UserModelNotification: ForwardRefRenderFunction<IInAppNotificationOpenable, Props> = (props: Props) => {
+export const useUserModelNotification = (notification: IInAppNotification & HasObjectId): ModelNotificationUtils | null => {
 
 
-  const { notification } = props;
+  const { actionMsg, actionIcon } = useActionMsgAndIconForModelNotification(notification);
+  const router = useRouter();
 
 
-  const { actionMsg, actionIcon } = useActionMsgAndIconForUserModelNotification(notification);
+  const isUserModelNotification = (notification: IInAppNotification & HasObjectId): notification is IInAppNotification<IUser> & HasObjectId => {
+    return notification.targetModel === SupportedTargetModel.MODEL_USER;
+  };
+
+  if (!isUserModelNotification(notification)) {
+    return null;
+  }
 
 
   const actionUsers = notification.target.username;
   const actionUsers = notification.target.username;
 
 
-  return (
-    <ModelNotification
-      notification={notification}
-      actionMsg={actionMsg}
-      actionIcon={actionIcon}
-      actionUsers={actionUsers}
-    />
-  );
-};
+  const Notification = () => {
+    return (
+      <ModelNotification
+        notification={notification}
+        actionMsg={actionMsg}
+        actionIcon={actionIcon}
+        actionUsers={actionUsers}
+      />
+    );
+  };
+
+  const publishOpen = () => {
+    router.push('/admin/users');
+  };
+
+  return {
+    Notification,
+    publishOpen,
+  };
 
 
-export default forwardRef(UserModelNotification);
+};

+ 8 - 53
apps/app/src/components/InAppNotification/PageNotification/index.tsx

@@ -1,64 +1,19 @@
-import { FC } from 'react';
+import type { HasObjectId } from '@growi/core';
 
 
-import type { HasObjectId, IPage, IUser } from '@growi/core';
-import { useRouter } from 'next/router';
-
-import { SupportedTargetModel } from '~/interfaces/activity';
 import type { IInAppNotification } from '~/interfaces/in-app-notification';
 import type { 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): ModelNotificationUtils => {
-
-  const targetModel = notification.targetModel;
-
-  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;
+import { usePageModelNotification, type ModelNotificationUtils } from './PageModelNotification';
+import { useUserModelNotification } from './UserModelNotification';
 
 
-    case SupportedTargetModel.MODEL_USER:
 
 
-      Notification = () => {
-        return <UserModelNotification notification={notification as IInAppNotification<IUser> & HasObjectId} />;
-      };
+export const useModelNotification = (notification: IInAppNotification & HasObjectId): ModelNotificationUtils | null => {
 
 
-      publishOpen = () => {
-        router.push('/admin/users');
-      };
+  const pageModelNotificationUtils = usePageModelNotification(notification);
+  const userModelNotificationUtils = useUserModelNotification(notification);
 
 
-      break;
-  }
+  const modelNotificationUtils = pageModelNotificationUtils ?? userModelNotificationUtils;
 
 
-  return {
-    Notification,
-    publishOpen,
-  };
 
 
+  return modelNotificationUtils;
 };
 };

+ 2 - 19
apps/app/src/components/InAppNotification/PageNotification/useActionAndMsg.ts

@@ -1,4 +1,4 @@
-import type { IUser, IPage, HasObjectId } from '@growi/core';
+import type { HasObjectId } from '@growi/core';
 
 
 import { SupportedAction } from '~/interfaces/activity';
 import { SupportedAction } from '~/interfaces/activity';
 import type { IInAppNotification } from '~/interfaces/in-app-notification';
 import type { IInAppNotification } from '~/interfaces/in-app-notification';
@@ -8,7 +8,7 @@ export type ActionMsgAndIconType = {
   actionIcon: string
   actionIcon: string
 }
 }
 
 
-export const useActionMsgAndIconForPageModelNotification = (notification: IInAppNotification<IPage> & HasObjectId): ActionMsgAndIconType => {
+export const useActionMsgAndIconForModelNotification = (notification: IInAppNotification & HasObjectId): ActionMsgAndIconType => {
   const actionType: string = notification.action;
   const actionType: string = notification.action;
   let actionMsg: string;
   let actionMsg: string;
   let actionIcon: string;
   let actionIcon: string;
@@ -66,23 +66,6 @@ export const useActionMsgAndIconForPageModelNotification = (notification: IInApp
       actionMsg = 'commented on';
       actionMsg = 'commented on';
       actionIcon = 'icon-bubble';
       actionIcon = 'icon-bubble';
       break;
       break;
-    default:
-      actionMsg = '';
-      actionIcon = '';
-  }
-
-  return {
-    actionMsg,
-    actionIcon,
-  };
-};
-
-export const useActionMsgAndIconForUserModelNotification = (notification: IInAppNotification<IUser> & HasObjectId): ActionMsgAndIconType => {
-  const actionType: string = notification.action;
-  let actionMsg: string;
-  let actionIcon: string;
-
-  switch (actionType) {
     case SupportedAction.ACTION_USER_REGISTRATION_APPROVAL_REQUEST:
     case SupportedAction.ACTION_USER_REGISTRATION_APPROVAL_REQUEST:
       actionMsg = 'requested registration approval';
       actionMsg = 'requested registration approval';
       actionIcon = 'icon-bubble';
       actionIcon = 'icon-bubble';