From 31da88fca89507e7db13f9f6734ef418baf8ff8b Mon Sep 17 00:00:00 2001 From: ayang <75017711+ayangweb@users.noreply.github.com> Date: Fri, 13 Sep 2024 09:34:56 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E4=BA=86?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=BC=B9=E6=A1=86=E7=9A=84=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86=20(#513)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Update/index.tsx | 45 ++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/components/Update/index.tsx b/src/components/Update/index.tsx index 03ec38ca71..61986f55dd 100644 --- a/src/components/Update/index.tsx +++ b/src/components/Update/index.tsx @@ -16,7 +16,7 @@ import styles from "./index.module.scss"; interface State { open?: boolean; - loading?: boolean; + downloading?: boolean; manifest?: UpdateManifest; } @@ -26,12 +26,11 @@ let timer: Timeout; const Update = () => { const { env } = useSnapshot(globalStore); const { t } = useTranslation(); - const state = useReactive({}); - const [messageApi, contextHolder] = message.useMessage(); useMount(() => { + // 监听更新事件 listen(LISTEN_KEY.UPDATE, async ({ payload }) => { check(payload); @@ -45,6 +44,7 @@ const Update = () => { }); }); + // 监听自动更新配置变化 watchKey(globalStore.update, "auto", (value) => { clearInterval(timer); @@ -56,12 +56,14 @@ const Update = () => { }); }); + // 本地化更新时间 const updateTime = useCreation(() => { const date = state.manifest?.date?.split(" ")?.slice(0, 2)?.join(" "); return dayjs.utc(date).local().format("YYYY-MM-DD HH:mm:ss"); }, [state.manifest?.date]); + // 检查更新 const check = async (showMessage = false) => { try { const { shouldUpdate, manifest } = await checkUpdate(); @@ -72,15 +74,7 @@ const Update = () => { const isBeta = /[a-z]/.test(version); if (isBeta && !globalStore.update.beta) { - if (showMessage) { - messageApi.open({ - key: MESSAGE_KEY, - type: "success", - content: t("component.app_update.hints.latest_version"), - }); - } - - return; + return showLatestMessage(showMessage); } showWindow(); @@ -91,11 +85,7 @@ const Update = () => { Object.assign(state, { manifest, open: true }); } else if (showMessage) { - messageApi.open({ - key: MESSAGE_KEY, - type: "success", - content: t("component.app_update.hints.latest_version"), - }); + showLatestMessage(); } } catch { if (!showMessage) return; @@ -108,6 +98,7 @@ const Update = () => { } }; + // 替换更新日志里的内容 const replaceManifestBody = (body: string) => { return ( body @@ -124,8 +115,19 @@ const Update = () => { ); }; + // 显示最新版本的提示信息 + const showLatestMessage = (show = true) => { + if (!show) return; + + messageApi.open({ + key: MESSAGE_KEY, + type: "success", + content: t("component.app_update.hints.latest_version"), + }); + }; + const handleOk = async () => { - state.loading = true; + state.downloading = true; installUpdate(); @@ -150,6 +152,8 @@ const Update = () => { type: "error", content: error, }); + + state.downloading = false; break; case "UPTODATE": messageApi.open({ @@ -171,6 +175,7 @@ const Update = () => { { okText={t("component.app_update.button.confirm_update")} cancelText={t("component.app_update.button.cancel_update")} className={styles.modal} - confirmLoading={state.loading} - cancelButtonProps={{ disabled: state.loading }} + confirmLoading={state.downloading} + cancelButtonProps={{ disabled: state.downloading }} onOk={handleOk} onCancel={handleCancel} >