Selaa lähdekoodia

refactor: extract MethodButton component for better reusability and clarity

Shun Miyazawa 8 kuukautta sitten
vanhempi
sitoutus
16e5ecfe89

+ 61 - 23
apps/app/src/features/openai/client/components/AiAssistant/AiAssistantManagementModal/SelectablePagePageList.tsx

@@ -1,4 +1,4 @@
-import React, { useMemo } from 'react';
+import React, { useMemo, memo } from 'react';
 
 
 import { useTranslation } from 'react-i18next';
 import { useTranslation } from 'react-i18next';
 
 
@@ -8,7 +8,42 @@ import styles from './SelectablePagePageList.module.scss';
 
 
 const moduleClass = styles['selectable-page-page-list'] ?? '';
 const moduleClass = styles['selectable-page-page-list'] ?? '';
 
 
-type Props = {
+type MethodButtonProps = {
+  page: SelectedPage;
+  disablePagePaths: string[];
+  methodButtonColor: string;
+  methodButtonIconName: string;
+  onClickMethodButton: (page: SelectedPage) => void;
+}
+
+const MethodButton = memo((props: MethodButtonProps) => {
+  const {
+    page,
+    disablePagePaths,
+    methodButtonColor,
+    methodButtonIconName,
+    onClickMethodButton,
+  } = props;
+
+  return (
+    <button
+      type="button"
+      className={`btn border-0 ${methodButtonColor}`}
+      disabled={disablePagePaths.includes(page.path)}
+      onClick={(e) => {
+        e.stopPropagation();
+        onClickMethodButton(page);
+      }}
+    >
+      <span className="material-symbols-outlined">
+        {methodButtonIconName}
+      </span>
+    </button>
+  );
+});
+
+
+type SelectablePagePageListProps = {
   pages: SelectedPage[],
   pages: SelectedPage[],
   method: 'add' | 'remove' | 'delete'
   method: 'add' | 'remove' | 'delete'
   methodButtonPosition?: 'left' | 'right',
   methodButtonPosition?: 'left' | 'right',
@@ -16,12 +51,12 @@ type Props = {
   onClickMethodButton: (page: SelectedPage) => void,
   onClickMethodButton: (page: SelectedPage) => void,
 }
 }
 
 
-export const SelectablePagePageList = (props: Props): JSX.Element => {
+export const SelectablePagePageList = (props: SelectablePagePageListProps): JSX.Element => {
   const {
   const {
     pages,
     pages,
     method,
     method,
     methodButtonPosition = 'left',
     methodButtonPosition = 'left',
-    disablePagePaths,
+    disablePagePaths = [],
     onClickMethodButton,
     onClickMethodButton,
   } = props;
   } = props;
 
 
@@ -53,23 +88,6 @@ export const SelectablePagePageList = (props: Props): JSX.Element => {
     }
     }
   }, [method]);
   }, [method]);
 
 
-  const methodButton = (page: SelectedPage) => {
-    return (
-      <button
-        type="button"
-        className={`btn border-0 ${methodButtonColor}`}
-        disabled={disablePagePaths?.includes(page.path)}
-        onClick={(e) => {
-          e.stopPropagation();
-          onClickMethodButton(page);
-        }}
-      >
-        <span className="material-symbols-outlined">
-          {methodButtonIconName}
-        </span>
-      </button>
-    );
-  };
 
 
   if (pages.length === 0) {
   if (pages.length === 0) {
     return (
     return (
@@ -94,7 +112,17 @@ export const SelectablePagePageList = (props: Props): JSX.Element => {
             }}
             }}
           >
           >
 
 
-            {methodButtonPosition === 'left' && methodButton(page)}
+            {methodButtonPosition === 'left'
+              && (
+                <MethodButton
+                  page={page}
+                  disablePagePaths={disablePagePaths}
+                  methodButtonColor={methodButtonColor}
+                  methodButtonIconName={methodButtonIconName}
+                  onClickMethodButton={onClickMethodButton}
+                />
+              )
+            }
 
 
             <div className={`flex-grow-1 ${methodButtonPosition === 'left' ? 'me-4' : 'ms-2'}`}>
             <div className={`flex-grow-1 ${methodButtonPosition === 'left' ? 'me-4' : 'ms-2'}`}>
               <span>
               <span>
@@ -108,7 +136,17 @@ export const SelectablePagePageList = (props: Props): JSX.Element => {
               </span>
               </span>
             </span>
             </span>
 
 
-            {methodButtonPosition === 'right' && methodButton(page)}
+            {methodButtonPosition === 'right'
+              && (
+                <MethodButton
+                  page={page}
+                  disablePagePaths={disablePagePaths}
+                  methodButtonColor={methodButtonColor}
+                  methodButtonIconName={methodButtonIconName}
+                  onClickMethodButton={onClickMethodButton}
+                />
+              )
+            }
           </button>
           </button>
         );
         );
       })}
       })}