Sfoglia il codice sorgente

refactor component

yuken 3 anni fa
parent
commit
76d765ebac

+ 3 - 3
packages/app/src/components/Admin/Customize/CustomizeCssSetting.tsx

@@ -1,4 +1,4 @@
-import React from 'react';
+import React, { useCallback } from 'react';
 
 import { useTranslation } from 'react-i18next';
 import { Card, CardBody } from 'reactstrap';
@@ -21,7 +21,7 @@ const CustomizeCssSetting = (props: Props): JSX.Element => {
   const { adminCustomizeContainer } = props;
   const { t } = useTranslation();
 
-  const onClickSubmit = async() => {
+  const onClickSubmit = useCallback(async() => {
     try {
       await adminCustomizeContainer.updateCustomizeCss();
       toastSuccess(t('toaster.update_successed', { target: t('admin:customize_setting.custom_css') }));
@@ -29,7 +29,7 @@ const CustomizeCssSetting = (props: Props): JSX.Element => {
     catch (err) {
       toastError(err);
     }
-  };
+  }, [t, adminCustomizeContainer]);
 
   return (
     <React.Fragment>

+ 3 - 3
packages/app/src/components/Admin/Customize/CustomizeFunctionSetting.tsx

@@ -1,4 +1,4 @@
-import React from 'react';
+import React, { useCallback } from 'react';
 
 import { useTranslation } from 'react-i18next';
 import { Card, CardBody } from 'reactstrap';
@@ -23,7 +23,7 @@ const CustomizeFunctionSetting = (props: Props): JSX.Element => {
   const { adminCustomizeContainer } = props;
   const { t } = useTranslation();
 
-  const onClickSubmit = async() => {
+  const onClickSubmit = useCallback(async() => {
 
     try {
       await adminCustomizeContainer.updateCustomizeFunction();
@@ -32,7 +32,7 @@ const CustomizeFunctionSetting = (props: Props): JSX.Element => {
     catch (err) {
       toastError(err);
     }
-  };
+  }, [t, adminCustomizeContainer]);
 
   return (
     <React.Fragment>

+ 3 - 3
packages/app/src/components/Admin/Customize/CustomizeHeaderSetting.tsx

@@ -1,4 +1,4 @@
-import React from 'react';
+import React, { useCallback } from 'react';
 
 import { useTranslation } from 'react-i18next';
 import { Card, CardBody } from 'reactstrap';
@@ -19,7 +19,7 @@ const CustomizeHeaderSetting = (props: Props): JSX.Element => {
   const { adminCustomizeContainer } = props;
   const { t } = useTranslation();
 
-  const onClickSubmit = async() => {
+  const onClickSubmit = useCallback(async() => {
     try {
       await adminCustomizeContainer.updateCustomizeHeader();
       toastSuccess(t('toaster.update_successed', { target: t('admin:customize_setting.custom_header') }));
@@ -27,7 +27,7 @@ const CustomizeHeaderSetting = (props: Props): JSX.Element => {
     catch (err) {
       toastError(err);
     }
-  };
+  }, [t, adminCustomizeContainer]);
 
   return (
     <React.Fragment>

+ 41 - 38
packages/app/src/components/Admin/Customize/CustomizeHighlightSetting.tsx

@@ -16,6 +16,27 @@ type Props = {
   adminCustomizeContainer: AdminCustomizeContainer
 }
 
+const renderHljsDemo = React.memo((adminCustomizeContainer: AdminCustomizeContainer): JSX.Element => {
+
+  /* eslint-disable max-len */
+  const html = `<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MersenneTwister</span>(<span class="hljs-params">seed</span>) </span>{
+<span class="hljs-keyword">if</span> (<span class="hljs-built_in">arguments</span>.length == <span class="hljs-number">0</span>) {
+  seed = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().getTime();
+}
+
+<span class="hljs-keyword">this</span>._mt = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Array</span>(<span class="hljs-number">624</span>);
+<span class="hljs-keyword">this</span>.setSeed(seed);
+}</span>`;
+  /* eslint-enable max-len */
+
+  return (
+    <pre className={`hljs ${!adminCustomizeContainer.state.isHighlightJsStyleBorderEnabled && 'hljs-no-border'}`}>
+      {/* eslint-disable-next-line react/no-danger */}
+      <code dangerouslySetInnerHTML={{ __html: html }}></code>
+    </pre>
+  );
+});
+
 const CustomizeHighlightSetting = (props: Props): JSX.Element => {
   const { adminCustomizeContainer } = props;
   const { t } = useTranslation();
@@ -36,44 +57,26 @@ const CustomizeHighlightSetting = (props: Props): JSX.Element => {
     }
   }, [t, adminCustomizeContainer]);
 
-  const renderHljsDemo = () => {
+  const renderMenuItems = useCallback(() => {
 
-    /* eslint-disable max-len */
-    const html = `<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MersenneTwister</span>(<span class="hljs-params">seed</span>) </span>{
-  <span class="hljs-keyword">if</span> (<span class="hljs-built_in">arguments</span>.length == <span class="hljs-number">0</span>) {
-    seed = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().getTime();
-  }
+    const items = Object.entries(options).map((option) => {
+      const styleId = option[0];
+      const styleName = option[1].name;
+      const isBorderEnable = option[1].border;
 
-  <span class="hljs-keyword">this</span>._mt = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Array</span>(<span class="hljs-number">624</span>);
-  <span class="hljs-keyword">this</span>.setSeed(seed);
-}</span>`;
-    /* eslint-enable max-len */
-
-    return (
-      <pre className={`hljs ${!adminCustomizeContainer.state.isHighlightJsStyleBorderEnabled && 'hljs-no-border'}`}>
-        {/* eslint-disable-next-line react/no-danger */}
-        <code dangerouslySetInnerHTML={{ __html: html }}></code>
-      </pre>
-    );
-  };
-
-  const menuItem: React.ReactNode[] = [];
-
-  Object.entries(options).forEach((option) => {
-    const styleId = option[0];
-    const styleName = option[1].name;
-    const isBorderEnable = option[1].border;
-
-    menuItem.push(
-      <DropdownItem
-        key={styleId}
-        role="presentation"
-        onClick={() => adminCustomizeContainer.switchHighlightJsStyle(styleId, styleName, isBorderEnable)}
-      >
-        <a role="menuitem">{styleName}</a>
-      </DropdownItem>,
-    );
-  });
+      return (
+        <DropdownItem
+          key={styleId}
+          role="presentation"
+          onClick={() => adminCustomizeContainer.switchHighlightJsStyle(styleId, styleName, isBorderEnable)}
+        >
+          <a role="menuitem">{styleName}</a>
+        </DropdownItem>
+      );
+    });
+
+    return items;
+  }, [adminCustomizeContainer, options]);
 
   return (
     <React.Fragment>
@@ -91,7 +94,7 @@ const CustomizeHighlightSetting = (props: Props): JSX.Element => {
                   <span className="float-left">{adminCustomizeContainer.state.currentHighlightJsStyleName}</span>
                 </DropdownToggle>
                 <DropdownMenu className="dropdown-menu" role="menu">
-                  {menuItem}
+                  {renderMenuItems()}
                 </DropdownMenu>
               </Dropdown>
               <p className="form-text text-warning">
@@ -121,7 +124,7 @@ const CustomizeHighlightSetting = (props: Props): JSX.Element => {
           <div className="form-text text-muted">
             <label>Examples:</label>
             <div className="wiki">
-              {renderHljsDemo()}
+              {renderHljsDemo(adminCustomizeContainer)}
             </div>
           </div>
 

+ 35 - 35
packages/app/src/components/Admin/Customize/CustomizeThemeOptions.jsx

@@ -10,6 +10,41 @@ import { withUnstatedContainers } from '../../UnstatedUtils';
 
 import ThemeColorBox from './ThemeColorBox';
 
+/* eslint-disable no-multi-spaces */
+const lightNDarkTheme = [{
+  name: 'default',    bg: '#ffffff', topbar: '#2a2929', sidebar: '#122c55', theme: '#209fd8',
+}, {
+  name: 'mono-blue',  bg: '#F7FBFD', topbar: '#2a2929', sidebar: '#00587A', theme: '#00587A',
+}, {
+  name: 'hufflepuff',  bg: '#EFE2CF', topbar: '#2a2929', sidebar: '#EAAB20', theme: '#993439',
+}, {
+  name: 'fire-red',  bg: '#FDFDFD', topbar: '#2c2c2c', sidebar: '#BFBFBF', theme: '#EA5532',
+}, {
+  name: 'jade-green',  bg: '#FDFDFD', topbar: '#2c2c2c', sidebar: '#BFBFBF', theme: '#38B48B',
+}];
+
+const uniqueTheme = [{
+  name: 'nature',     bg: '#f9fff3', topbar: '#234136', sidebar: '#118050', theme: '#460039',
+}, {
+  name: 'wood',       bg: '#fffefb', topbar: '#2a2929', sidebar: '#aaa45f', theme: '#aaa45f',
+}, {
+  name: 'island',     bg: '#cef2ef', topbar: '#2a2929', sidebar: '#0c2a44', theme: 'rgba(183, 226, 219, 1)',
+}, {
+  name: 'christmas',  bg: '#fffefb', topbar: '#b3000c', sidebar: '#30882c', theme: '#d3c665',
+}, {
+  name: 'antarctic',  bg: '#ffffff', topbar: '#2a2929', sidebar: '#000080', theme: '#fa9913',
+}, {
+  name: 'spring',     bg: '#ffffff', topbar: '#d3687c', sidebar: '#ffb8c6', theme: '#67a856',
+}, {
+  name: 'future',     bg: '#16282d', topbar: '#2a2929', sidebar: '#00b5b7', theme: '#00b5b7',
+}, {
+  name: 'halloween',  bg: '#030003', topbar: '#aa4a04', sidebar: '#162b33', theme: '#e9af2b',
+}, {
+  name: 'kibela',  bg: '#f4f5f6', topbar: '#1256a3', sidebar: '#5882fa', theme: '#b5cbf79c',
+}, {
+  name: 'blackboard',  bg: '#223729', topbar: '#563E23', sidebar: '#7B5932', theme: '#DA8506',
+}];
+
 
 const CustomizeThemeOptions = (props) => {
 
@@ -17,41 +52,6 @@ const CustomizeThemeOptions = (props) => {
   const { t } = useTranslation();
   const { currentLayout, currentTheme } = adminCustomizeContainer.state;
 
-  /* eslint-disable no-multi-spaces */
-  const lightNDarkTheme = [{
-    name: 'default',    bg: '#ffffff', topbar: '#2a2929', sidebar: '#122c55', theme: '#209fd8',
-  }, {
-    name: 'mono-blue',  bg: '#F7FBFD', topbar: '#2a2929', sidebar: '#00587A', theme: '#00587A',
-  }, {
-    name: 'hufflepuff',  bg: '#EFE2CF', topbar: '#2a2929', sidebar: '#EAAB20', theme: '#993439',
-  }, {
-    name: 'fire-red',  bg: '#FDFDFD', topbar: '#2c2c2c', sidebar: '#BFBFBF', theme: '#EA5532',
-  }, {
-    name: 'jade-green',  bg: '#FDFDFD', topbar: '#2c2c2c', sidebar: '#BFBFBF', theme: '#38B48B',
-  }];
-
-  const uniqueTheme = [{
-    name: 'nature',     bg: '#f9fff3', topbar: '#234136', sidebar: '#118050', theme: '#460039',
-  }, {
-    name: 'wood',       bg: '#fffefb', topbar: '#2a2929', sidebar: '#aaa45f', theme: '#aaa45f',
-  }, {
-    name: 'island',     bg: '#cef2ef', topbar: '#2a2929', sidebar: '#0c2a44', theme: 'rgba(183, 226, 219, 1)',
-  }, {
-    name: 'christmas',  bg: '#fffefb', topbar: '#b3000c', sidebar: '#30882c', theme: '#d3c665',
-  }, {
-    name: 'antarctic',  bg: '#ffffff', topbar: '#2a2929', sidebar: '#000080', theme: '#fa9913',
-  }, {
-    name: 'spring',     bg: '#ffffff', topbar: '#d3687c', sidebar: '#ffb8c6', theme: '#67a856',
-  }, {
-    name: 'future',     bg: '#16282d', topbar: '#2a2929', sidebar: '#00b5b7', theme: '#00b5b7',
-  }, {
-    name: 'halloween',  bg: '#030003', topbar: '#aa4a04', sidebar: '#162b33', theme: '#e9af2b',
-  }, {
-    name: 'kibela',  bg: '#f4f5f6', topbar: '#1256a3', sidebar: '#5882fa', theme: '#b5cbf79c',
-  }, {
-    name: 'blackboard',  bg: '#223729', topbar: '#563E23', sidebar: '#7B5932', theme: '#DA8506',
-  }];
-
   return (
     <div id="themeOptions" className={`${currentLayout === 'kibela' && 'disabled'}`}>
       {/* Light and Dark Themes */}