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 277b7434cd005e03d9b3d14df31e9e43686f4c69 Author: robin <[email protected]> AuthorDate: Wed Sep 25 16:40:41 2024 +0800 refactor(embed-basic): Update useRenderEmbed hook to accept a request object --- embed-basic/hooks.tsx | 11 ++++++----- embed-basic/types.ts | 7 +++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/embed-basic/hooks.tsx b/embed-basic/hooks.tsx index be023dd..c5e48c1 100644 --- a/embed-basic/hooks.tsx +++ b/embed-basic/hooks.tsx @@ -20,9 +20,9 @@ import { useEffect, useState, - RefObject, ReactElement, isValidElement, + FC, } from 'react'; import { createRoot } from 'react-dom/client'; import { @@ -36,15 +36,16 @@ import { DropboxEmbed, TwitterEmbed, } from './components'; +import { pluginHookProps, Request } from './types'; interface Config { platform: string; enable: boolean; } -const useRenderEmbed = ( - element: HTMLElement | RefObject<HTMLElement> | null, -) => { +const useRenderEmbed : FC<pluginHookProps> = (element, request: Request = { + get: fetch, +})=> { const [configs, setConfigs] = useState<Config[] | null>(null); const embeds = [ @@ -247,7 +248,7 @@ const useRenderEmbed = ( }; const getConfig = () => { - fetch('/answer/api/v1/embed/config') + request.get('/answer/api/v1/embed/config') .then((response) => response.json()) .then((result) => setConfigs(result.data)); }; diff --git a/embed-basic/types.ts b/embed-basic/types.ts new file mode 100644 index 0000000..ad5aebd --- /dev/null +++ b/embed-basic/types.ts @@ -0,0 +1,7 @@ +import { RefObject } from 'react'; + +export type pluginHookProps = HTMLElement | RefObject<HTMLElement> | null; + +export interface Request { + get: (url: string) => Promise<Response>; +}
