melhore e otimize o código react import { FC, useMemo } from "react"; // Função auxiliar…
- User
melhore e otimize o código react
import { FC, useMemo } from "react";
// Função auxiliar para formatar bytes em KB, MB, GB ou Bytes const formatBytes = (bytes: number): string => { if (bytes >= 1024 * 1024 * 1024) { return (bytes / (1024 * 1024 * 1024)).toFixed(2) + " GB"; } else if (bytes >= 1024 * 1024) { return (bytes / (1024 * 1024)).toFixed(2) + " MB"; } else if (bytes >= 1024) { return (bytes / 1024).toFixed(2) + " KB"; } else { return bytes + " B"; } };
interface DownloadProgressProps { currentFile: number; totalFiles: number; fileName: string; downloadedBytes: number; totalBytes: number; }
const DownloadProgress: FC = ({ currentFile, totalFiles, fileName, downloadedBytes, totalBytes }) => { // Memoriza o cálculo de porcentagem para evitar cálculos desnecessários const progressPercent = useMemo(() => (downloadedBytes / totalBytes) * 100, [downloadedBytes, totalBytes]);
return ( Downloading file ({currentFile} / {totalFiles}):{" "} {fileName}{" "} ( {formatBytes(downloadedBytes)} {" "} / {formatBytes(totalBytes)})
<div className="w-[95%] bg-[#0E1622]/[0.95] rounded-md h-5 flex items-center"> <div className="bg-[#1164CA]/[0.95] ml-1 rounded-md h-4 transition-all duration-300" style={{ width: `${progressPercent}%` }} ></div> </div> </div>); };
export default DownloadProgress;
- Assistant