feat(snapshot): 进度显示增加文件大小信息,完成项显示 "完成 (29.4 MB)"
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
ff28615fc3
commit
26e55a649f
|
|
@ -4,10 +4,19 @@ import { useEffect, useState, useRef, useCallback } from 'react';
|
||||||
import { snapshotApi } from '@/infrastructure/api/snapshot.api';
|
import { snapshotApi } from '@/infrastructure/api/snapshot.api';
|
||||||
import type { BackupTarget, SnapshotTask } from '@/types/snapshot.types';
|
import type { BackupTarget, SnapshotTask } from '@/types/snapshot.types';
|
||||||
|
|
||||||
|
function formatBytes(bytes: string | number): string {
|
||||||
|
const b = typeof bytes === 'string' ? parseInt(bytes, 10) : bytes;
|
||||||
|
if (!b || b === 0) return '0 B';
|
||||||
|
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||||
|
const i = Math.floor(Math.log(b) / Math.log(1024));
|
||||||
|
return `${(b / Math.pow(1024, i)).toFixed(2)} ${units[i]}`;
|
||||||
|
}
|
||||||
|
|
||||||
interface TargetProgress {
|
interface TargetProgress {
|
||||||
target: BackupTarget;
|
target: BackupTarget;
|
||||||
percent: number;
|
percent: number;
|
||||||
message: string;
|
message: string;
|
||||||
|
fileSize: string;
|
||||||
status: 'pending' | 'running' | 'completed' | 'failed';
|
status: 'pending' | 'running' | 'completed' | 'failed';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,14 +48,16 @@ export function useSnapshotPolling(taskId: string | null): UseSnapshotPollingRet
|
||||||
COMPLETED: 'completed',
|
COMPLETED: 'completed',
|
||||||
FAILED: 'failed',
|
FAILED: 'failed',
|
||||||
};
|
};
|
||||||
|
const sizeStr = d.fileSize && parseInt(d.fileSize, 10) > 0 ? formatBytes(d.fileSize) : '';
|
||||||
next.set(d.target, {
|
next.set(d.target, {
|
||||||
target: d.target,
|
target: d.target,
|
||||||
percent: d.status === 'COMPLETED' ? 100 : d.progress,
|
percent: d.status === 'COMPLETED' ? 100 : d.progress,
|
||||||
|
fileSize: sizeStr,
|
||||||
message:
|
message:
|
||||||
d.error
|
d.error
|
||||||
? d.error
|
? d.error
|
||||||
: d.status === 'COMPLETED'
|
: d.status === 'COMPLETED'
|
||||||
? '完成'
|
? (sizeStr ? `完成 (${sizeStr})` : '完成')
|
||||||
: d.status === 'RUNNING'
|
: d.status === 'RUNNING'
|
||||||
? (d.progressMsg || `备份中... ${d.progress.toFixed(1)}%`)
|
? (d.progressMsg || `备份中... ${d.progress.toFixed(1)}%`)
|
||||||
: '等待中',
|
: '等待中',
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,19 @@ import { useEffect, useState, useRef, useCallback } from 'react';
|
||||||
import { snapshotApi } from '@/lib/api/snapshot.api';
|
import { snapshotApi } from '@/lib/api/snapshot.api';
|
||||||
import type { BackupTarget, SnapshotTask } from '@/types/snapshot.types';
|
import type { BackupTarget, SnapshotTask } from '@/types/snapshot.types';
|
||||||
|
|
||||||
|
function formatBytes(bytes: string | number): string {
|
||||||
|
const b = typeof bytes === 'string' ? parseInt(bytes, 10) : bytes;
|
||||||
|
if (!b || b === 0) return '0 B';
|
||||||
|
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||||
|
const i = Math.floor(Math.log(b) / Math.log(1024));
|
||||||
|
return `${(b / Math.pow(1024, i)).toFixed(2)} ${units[i]}`;
|
||||||
|
}
|
||||||
|
|
||||||
interface TargetProgress {
|
interface TargetProgress {
|
||||||
target: BackupTarget;
|
target: BackupTarget;
|
||||||
percent: number;
|
percent: number;
|
||||||
message: string;
|
message: string;
|
||||||
|
fileSize: string;
|
||||||
status: 'pending' | 'running' | 'completed' | 'failed';
|
status: 'pending' | 'running' | 'completed' | 'failed';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,14 +48,16 @@ export function useSnapshotPolling(taskId: string | null): UseSnapshotPollingRet
|
||||||
COMPLETED: 'completed',
|
COMPLETED: 'completed',
|
||||||
FAILED: 'failed',
|
FAILED: 'failed',
|
||||||
};
|
};
|
||||||
|
const sizeStr = d.fileSize && parseInt(d.fileSize, 10) > 0 ? formatBytes(d.fileSize) : '';
|
||||||
next.set(d.target, {
|
next.set(d.target, {
|
||||||
target: d.target,
|
target: d.target,
|
||||||
percent: d.status === 'COMPLETED' ? 100 : d.progress,
|
percent: d.status === 'COMPLETED' ? 100 : d.progress,
|
||||||
|
fileSize: sizeStr,
|
||||||
message:
|
message:
|
||||||
d.error
|
d.error
|
||||||
? d.error
|
? d.error
|
||||||
: d.status === 'COMPLETED'
|
: d.status === 'COMPLETED'
|
||||||
? '完成'
|
? (sizeStr ? `完成 (${sizeStr})` : '完成')
|
||||||
: d.status === 'RUNNING'
|
: d.status === 'RUNNING'
|
||||||
? (d.progressMsg || `备份中... ${d.progress.toFixed(1)}%`)
|
? (d.progressMsg || `备份中... ${d.progress.toFixed(1)}%`)
|
||||||
: '等待中',
|
: '等待中',
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue