From c1b9fa705df074e192c7a6bf8f1729a85751e4be Mon Sep 17 00:00:00 2001 From: elrrrrrrr Date: Fri, 18 Oct 2024 16:10:41 +0800 Subject: [PATCH] feat: sync log (#93) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🎨 取消重复的查看日志信息弹框 cc @yoyo837 ## Summary by CodeRabbit - **New Features** - Introduced a new status display for log operations with improved messaging. - Enhanced button functionality to reflect log status and loading states. - **Bug Fixes** - Improved error handling for log polling with a defined maximum retry limit. - **Refactor** - Updated state management for better clarity and maintainability. --- src/components/Sync.tsx | 86 +++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 47 deletions(-) diff --git a/src/components/Sync.tsx b/src/components/Sync.tsx index c3c8d3b..29bfc45 100644 --- a/src/components/Sync.tsx +++ b/src/components/Sync.tsx @@ -1,22 +1,31 @@ 'use client'; import { REGISTRY, SYNC_REGISTRY } from '@/config'; import { Button, message, Modal } from 'antd'; -import Link from 'next/link'; import React from 'react'; +const MAX_RETRY = 30; + interface SyncProps { pkgName: string; } -const LogStatus = { - WAIT: 1, - ERROR: 2, - SUCCESS: 3, +enum LogStatus { + INIT, + WAIT, + ERROR, + SUCCESS, } +const LogStatusTextMap= { + [LogStatus.INIT]: '进行同步', + [LogStatus.WAIT]: '等待调度', + [LogStatus.ERROR]: '调度失败', + [LogStatus.SUCCESS]: '查看日志', +}; + export default function Sync({ pkgName }: SyncProps) { const [logId, setLogId] = React.useState(); - const [logState, setLogState] = React.useState(LogStatus.WAIT); + const [logState, setLogState] = React.useState(LogStatus.INIT); const retryCountRef = React.useRef(0); const [modal, contextHolder] = Modal.useModal(); @@ -24,20 +33,6 @@ export default function Sync({ pkgName }: SyncProps) { return `${REGISTRY}/-/package/${pkgName}/syncs/${id}/log`; } - async function showLog(id: string) { - modal.success({ - title: '等待调度', - content: ( - <> - 创建同步任务成功,正在等待调度,如遇日志 404 请稍后刷新重试,通常需要几十秒钟的时间 - - 查看日志 - - - ), - }); - } - async function logPolling(id:string) { if (!id) { return; @@ -51,13 +46,17 @@ export default function Sync({ pkgName }: SyncProps) { } throw new Error('Not ready'); } catch { - if (retryCountRef.current > 30) { + if (retryCountRef.current > MAX_RETRY) { setLogState(LogStatus.ERROR); return; + } else { + if (LogStatus.WAIT !== logState) { + setLogState(LogStatus.WAIT); + } + setTimeout(() => { + logPolling(id); + }, 1000); } - setTimeout(() => { - logPolling(id); - }, 1000); } } @@ -69,6 +68,7 @@ export default function Sync({ pkgName }: SyncProps) { const res = await response.json(); if (res.ok) { setLogId(res.id); + setLogState(LogStatus.WAIT); logPolling(res.id); return; } @@ -81,29 +81,21 @@ export default function Sync({ pkgName }: SyncProps) { return ( <> {contextHolder} - ); -} \ No newline at end of file +}