This is an automated email from the ASF dual-hosted git repository.

robin0716 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-answer-plugins.git

commit a5975505afaff7b22cb2ea3a6ba5e48e45b2595d
Author: robin <[email protected]>
AuthorDate: Fri Nov 15 15:39:26 2024 +0800

    refactor(hooks): Simplify request handling and improve get function for 
embeds
---
 embed-basic/hooks.tsx | 38 ++++++++++++++++++++------------------
 embed-basic/types.ts  |  7 +------
 2 files changed, 21 insertions(+), 24 deletions(-)

diff --git a/embed-basic/hooks.tsx b/embed-basic/hooks.tsx
index c5e48c1..951f062 100644
--- a/embed-basic/hooks.tsx
+++ b/embed-basic/hooks.tsx
@@ -17,13 +17,7 @@
  * under the License.
  */
 
-import {
-  useEffect,
-  useState,
-  ReactElement,
-  isValidElement,
-  FC,
-} from 'react';
+import { useEffect, useState, ReactElement, isValidElement } from 'react';
 import { createRoot } from 'react-dom/client';
 import {
   GithubGistEmbed,
@@ -36,16 +30,25 @@ import {
   DropboxEmbed,
   TwitterEmbed,
 } from './components';
-import { pluginHookProps, Request } from './types';
+import { Request } from './types';
 
 interface Config {
   platform: string;
   enable: boolean;
 }
 
-const useRenderEmbed : FC<pluginHookProps> = (element, request: Request = {
-  get: fetch,
-})=> {
+const get = async (url: string) => {
+  const response = await fetch(url);
+  const { data } = await response.json();
+  return data;
+};
+
+const useRenderEmbed = (
+  element,
+  request: Request = {
+    get,
+  },
+) => {
   const [configs, setConfigs] = useState<Config[] | null>(null);
 
   const embeds = [
@@ -234,13 +237,12 @@ const useRenderEmbed : FC<pluginHookProps> = (element, 
request: Request = {
       styleElement = document.createElement('style');
       styleElement.id = 'embed-style';
       if (hasDefaultStyle) {
-        styleElement.textContent =  `
+        styleElement.textContent = `
          .embed-light:hover {
            --bs-bg-opacity: 1;
            background-color: rgba(var(--bs-light-rgb), var(--bs-bg-opacity)) 
!important;
          }
-        `
-        // style 插入 header
+        `;
         const head = document.querySelector('head') as HTMLElement;
         head.appendChild(styleElement);
       }
@@ -248,9 +250,9 @@ const useRenderEmbed : FC<pluginHookProps> = (element, 
request: Request = {
   };
 
   const getConfig = () => {
-    request.get('/answer/api/v1/embed/config')
-      .then((response) => response.json())
-      .then((result) => setConfigs(result.data));
+    request
+      .get('/answer/api/v1/embed/config')
+      .then((result) => setConfigs(result));
   };
   useEffect(() => {
     getConfig();
@@ -261,7 +263,7 @@ const useRenderEmbed : FC<pluginHookProps> = (element, 
request: Request = {
       if (styleEle) {
         head.removeChild(styleEle);
       }
-    }
+    };
   }, []);
 
   useEffect(() => {
diff --git a/embed-basic/types.ts b/embed-basic/types.ts
index 6a034fc..afbc831 100644
--- a/embed-basic/types.ts
+++ b/embed-basic/types.ts
@@ -16,11 +16,6 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
-import { RefObject } from 'react';
-
-export type pluginHookProps = HTMLElement | RefObject<HTMLElement> | null;
-
 export interface Request {
-    get: (url: string) => Promise<Response>;
+    get: (url: string) => Promise<any>;
 }

Reply via email to