diff --git a/package.json b/package.json index 2bbbddd..70dc1c0 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "next": "^13.4.7", "npm-package-arg": "^11.0.1", "react": "^18.2.0", + "react-ansi": "^3.0.2", "react-chartjs-2": "^5.2.0", "react-dom": "^18.2.0", "react-icons": "^4.3.1", @@ -50,6 +51,9 @@ "overrides": { "npm-package-arg": { "validate-npm-package-name": "5.0.0" + }, + "react-ansi": { + "react": "^18.2.0" } } } diff --git a/src/components/Sync.tsx b/src/components/Sync.tsx index 29bfc45..fea9d95 100644 --- a/src/components/Sync.tsx +++ b/src/components/Sync.tsx @@ -1,6 +1,8 @@ 'use client'; import { REGISTRY, SYNC_REGISTRY } from '@/config'; +import useSyncLog from '@/hooks/useSyncLog'; import { Button, message, Modal } from 'antd'; +import ReactAnsi from 'react-ansi'; import React from 'react'; const MAX_RETRY = 30; @@ -27,7 +29,8 @@ export default function Sync({ pkgName }: SyncProps) { const [logId, setLogId] = React.useState(); const [logState, setLogState] = React.useState(LogStatus.INIT); const retryCountRef = React.useRef(0); - const [modal, contextHolder] = Modal.useModal(); + const [showLog, setShowLog] = React.useState(false); + const { data: logContent } = useSyncLog(pkgName, logId); function genLogFileUrl(id: string) { return `${REGISTRY}/-/package/${pkgName}/syncs/${id}/log`; @@ -80,14 +83,15 @@ export default function Sync({ pkgName }: SyncProps) { return ( <> - {contextHolder} + + setShowLog(false)} + > + + ); } diff --git a/src/hooks/useSyncLog.ts b/src/hooks/useSyncLog.ts new file mode 100644 index 0000000..18156b9 --- /dev/null +++ b/src/hooks/useSyncLog.ts @@ -0,0 +1,17 @@ +import { REGISTRY } from '@/config'; +import useSwr from 'swr'; + +export default function useSyncLog(pkgName: string, logId?: string) { + return useSwr(logId ? `sync_log_${logId}` : null, async () => { + try { + const res = await fetch(`${REGISTRY}/-/package/${pkgName}/syncs/${logId}/log`).then((res) => res.text()); + return res.split('\n'); + } catch (e) { + return null; + } + }, { + refreshInterval() { + return 1000; + }, + }); +}