chore: download progress
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import useSetChunkFetch, { HandlerOptions } from '@/hooks/use-chunk-fetch';
|
||||
import dayjs from 'dayjs';
|
||||
import { message } from 'antd';
|
||||
import { useEffect, useRef } from 'react';
|
||||
|
||||
export default function useDownloadStream() {
|
||||
@@ -7,18 +7,17 @@ export default function useDownloadStream() {
|
||||
const logParseWorker = useRef<any>(null);
|
||||
const clearScreen = useRef(false);
|
||||
const filename = useRef('log');
|
||||
const downloadNotificationRef = useRef<any>(null);
|
||||
|
||||
const { setChunkFetch } = useSetChunkFetch();
|
||||
|
||||
const downloadFile = (content: string) => {
|
||||
const timestamp = dayjs().format('YYYY-MM-DD_HH-mm-ss');
|
||||
const fileName = `${filename.current}_${timestamp}.txt`;
|
||||
|
||||
const blob = new Blob([content], { type: 'text/plain' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = fileName;
|
||||
a.download = filename.current;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
@@ -28,6 +27,13 @@ export default function useDownloadStream() {
|
||||
|
||||
const updateContent = (data: string, options?: HandlerOptions) => {
|
||||
const { isComplete } = options || {};
|
||||
|
||||
downloadNotificationRef.current?.({
|
||||
...options,
|
||||
duration: isComplete ? 1 : null,
|
||||
filename: filename.current
|
||||
});
|
||||
|
||||
logParseWorker.current?.postMessage({
|
||||
inputStr: data,
|
||||
page: 1,
|
||||
@@ -38,6 +44,18 @@ export default function useDownloadStream() {
|
||||
clearScreen.current = false;
|
||||
};
|
||||
|
||||
const handleError = (error: any) => {
|
||||
const errorMsg = error?.message || error;
|
||||
const msg =
|
||||
typeof errorMsg === 'string' ? errorMsg : JSON.stringify(errorMsg);
|
||||
message.error(msg);
|
||||
downloadNotificationRef.current?.({
|
||||
duration: 1,
|
||||
percent: 0,
|
||||
filename: filename.current
|
||||
});
|
||||
};
|
||||
|
||||
const downloadStream = async (props: {
|
||||
data?: any;
|
||||
url: string;
|
||||
@@ -46,20 +64,36 @@ export default function useDownloadStream() {
|
||||
method?: string;
|
||||
headers?: any;
|
||||
filename?: string;
|
||||
downloadNotification?: (data: any) => void;
|
||||
}) => {
|
||||
clearScreen.current = true;
|
||||
filename.current = props.filename || 'log';
|
||||
const { params, url } = props;
|
||||
try {
|
||||
clearScreen.current = true;
|
||||
filename.current = props.filename || 'log';
|
||||
downloadNotificationRef.current = props.downloadNotification;
|
||||
const { params, url } = props;
|
||||
|
||||
chunkRequedtRef.current?.current?.abort?.();
|
||||
downloadNotificationRef.current?.({
|
||||
filename: filename.current
|
||||
});
|
||||
|
||||
chunkRequedtRef.current = setChunkFetch({
|
||||
url,
|
||||
params,
|
||||
watch: false,
|
||||
contentType: 'text',
|
||||
handler: updateContent
|
||||
});
|
||||
chunkRequedtRef.current?.current?.abort?.();
|
||||
|
||||
chunkRequedtRef.current = setChunkFetch({
|
||||
url,
|
||||
params,
|
||||
watch: false,
|
||||
contentType: 'text',
|
||||
errorHandler: handleError,
|
||||
handler: updateContent
|
||||
});
|
||||
} catch (error) {
|
||||
//
|
||||
downloadNotificationRef.current?.({
|
||||
duration: 1,
|
||||
percent: 0,
|
||||
filename: filename.current
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
Reference in New Issue
Block a user