(onComplete?: () => void)
| 14 | } |
| 15 | |
| 16 | export function useModelDownload(onComplete?: () => void) { |
| 17 | const [downloads, setDownloads] = useState<Map<string, DownloadState>>(new Map()); |
| 18 | |
| 19 | useEffect(() => { |
| 20 | const setupListener = async () => { |
| 21 | const unlistenDownload = await listenToDownloadProgress((progress: DownloadProgress) => { |
| 22 | setDownloads((prev) => { |
| 23 | const newMap = new Map(prev); |
| 24 | const percentage = progress.completed && progress.total |
| 25 | ? calculateProgress(progress.completed, progress.total) |
| 26 | : 0; |
| 27 | |
| 28 | const existing = newMap.get(progress.model_name); |
| 29 | |
| 30 | newMap.set(progress.model_name, { |
| 31 | status: progress.status, |
| 32 | percentage, |
| 33 | isDownloading: progress.status !== 'success', |
| 34 | error: null, |
| 35 | downloadedBytes: progress.completed ?? existing?.downloadedBytes, |
| 36 | totalBytes: progress.total ?? existing?.totalBytes, |
| 37 | speedMbps: existing?.speedMbps, |
| 38 | }); |
| 39 | |
| 40 | // Call onComplete when download finishes |
| 41 | if (progress.status === 'success' && onComplete) { |
| 42 | onComplete(); |
| 43 | } |
| 44 | |
| 45 | return newMap; |
| 46 | }); |
| 47 | }); |
| 48 | |
| 49 | const unlistenHf = await listenToHfDownloadProgress((progress) => { |
| 50 | setDownloads((prev) => { |
| 51 | const newMap = new Map(prev); |
| 52 | const existing = newMap.get(progress.model_name); |
| 53 | |
| 54 | newMap.set(progress.model_name, { |
| 55 | status: existing?.status ?? 'downloading', |
| 56 | percentage: Math.round(progress.percentage), |
| 57 | isDownloading: true, |
| 58 | error: null, |
| 59 | downloadedBytes: progress.bytes_downloaded, |
| 60 | totalBytes: progress.total_bytes, |
| 61 | speedMbps: progress.speed_mbps, |
| 62 | }); |
| 63 | |
| 64 | return newMap; |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | return () => { |
| 69 | unlistenDownload(); |
| 70 | unlistenHf(); |
| 71 | }; |
| 72 | }; |
| 73 |
no test coverage detected