From 906c8c9cc9681215766718958626c7ea1fbc9f9a Mon Sep 17 00:00:00 2001 From: Nancy <3510671794@qq.com> Date: Mon, 4 Nov 2024 20:01:03 +0800 Subject: [PATCH 1/3] feature/retry-on-export-option --- app/page.tsx | 138 +++++++++++++++++++++++++-------------------------- 1 file changed, 69 insertions(+), 69 deletions(-) diff --git a/app/page.tsx b/app/page.tsx index d0606508..de765347 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -27,87 +27,87 @@ export default function Home() { return () => { window.removeEventListener("keydown", listener); }; - }); + }, []); return ( - <> -
- - - -
- {html && - ReactDOM.createPortal( -
setHtml(null)} - > - -
, - document.body - )} - + <> +
+ + + +
+ {html && + ReactDOM.createPortal( +
setHtml(null)} + > + +
, + document.body + )} + ); } function ExportButton({ setHtml }: { setHtml: (html: string) => void }) { const editor = useEditor(); const [loading, setLoading] = useState(false); - // A tailwind styled button that is pinned to the bottom right of the screen - return ( - + const message = json.choices[0].message.content; + const start = message.indexOf(""); + const end = message.indexOf(""); + const html = message.slice(start, end + "".length); + setHtml(html); + }; + + await sendRequest(); + } finally { + setLoading(false); + } + }; + + return ( + ); } From 81c394afda79961b653758c3674b79a361e31213 Mon Sep 17 00:00:00 2001 From: Nancy <3510671794@qq.com> Date: Mon, 4 Nov 2024 20:08:28 +0800 Subject: [PATCH 2/3] export-button-throttle-debounce --- app/page.tsx | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/app/page.tsx b/app/page.tsx index de765347..1b97d9af 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -55,6 +55,26 @@ function ExportButton({ setHtml }: { setHtml: (html: string) => void }) { const editor = useEditor(); const [loading, setLoading] = useState(false); + const throttle = (func: () => void, limit: number) => { + let lastFunc: NodeJS.Timeout | null = null; + let lastRan: number | null = null; + + return function () { + const context = this; + const args = arguments; + + if (lastRan === null || Date.now() - lastRan >= limit) { + func.apply(context, args); + lastRan = Date.now(); + } else { + clearTimeout(lastFunc!); + lastFunc = setTimeout(() => { + func.apply(context, args); + }, limit - (Date.now() - lastRan)); + } + }; + }; + const handleExport = async () => { setLoading(true); try { @@ -91,11 +111,13 @@ function ExportButton({ setHtml }: { setHtml: (html: string) => void }) { } }; + const throttledHandleExport = throttle(handleExport, 2000); // 2000ms 节流 + return (