Files
SpotiFLAC/frontend/src/hooks/useDownloadProgress.ts
T
afkarxyz 9260adc2d2 v7.0.1
2026-01-11 08:39:14 +07:00

35 lines
1.1 KiB
TypeScript

import { useState, useEffect, useRef } from "react";
import { GetDownloadProgress } from "../../wailsjs/go/main/App";
export interface DownloadProgressInfo {
is_downloading: boolean;
mb_downloaded: number;
speed_mbps: number;
}
export function useDownloadProgress() {
const [progress, setProgress] = useState<DownloadProgressInfo>({
is_downloading: false,
mb_downloaded: 0,
speed_mbps: 0,
});
const intervalRef = useRef<number | null>(null);
useEffect(() => {
const pollProgress = async () => {
try {
const progressInfo = await GetDownloadProgress();
setProgress(progressInfo);
}
catch (error) {
console.error("Failed to get download progress:", error);
}
};
intervalRef.current = window.setInterval(pollProgress, 200);
pollProgress();
return () => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
}
};
}, []);
return progress;
}