import { useState, useCallback, useEffect, useRef } from 'react'; import { Card, Text, Title, Loader, Stack, Container } from '@mantine/core'; import { DownloadCard } from '@/components/DownloadCard'; import { getPlatformDisplayName } from '@/utils/platform'; import { formatDownloadSize } from '@/utils/download'; import { getAssetDescription, sortDownloadsByType } from '@/utils/assets'; import { useKoboldVersions } from '@/hooks/useKoboldVersions'; import type { DownloadItem } from '@/types/electron'; interface DownloadScreenProps { onDownloadComplete: () => void; } export const DownloadScreen = ({ onDownloadComplete }: DownloadScreenProps) => { const { platformInfo, availableDownloads, loadingPlatform, loadingRemote, downloading, downloadProgress, handleDownload: sharedHandleDownload, } = useKoboldVersions(); const [downloadingAsset, setDownloadingAsset] = useState(null); const downloadingItemRef = useRef(null); const loading = loadingPlatform || loadingRemote; const sortedDownloads = sortDownloadsByType(availableDownloads); const handleDownload = useCallback( async (download: DownloadItem) => { setDownloadingAsset(download.name); try { const success = await sharedHandleDownload({ type: 'asset', item: download, isUpdate: false, wasCurrentBinary: false, }); if (success) { onDownloadComplete(); setTimeout(() => { setDownloadingAsset(null); }, 200); } } catch (error) { window.electronAPI.logs.logError( `Failed to download ${download.name}:`, error as Error ); } finally { setDownloadingAsset(null); } }, [sharedHandleDownload, onDownloadComplete] ); useEffect(() => { if (downloading && downloadingItemRef.current) { downloadingItemRef.current.scrollIntoView({ behavior: 'smooth', block: 'center', }); } }, [downloading]); return ( Available Binaries for Your Platform {loading ? ( Preparing download options... ) : ( <> {availableDownloads.length > 0 && ( <> {availableDownloads.length > 0 ? ( {sortedDownloads.map((download) => { const isDownloading = Boolean(downloading) && downloadingAsset === download.name; return (
{ e.stopPropagation(); handleDownload(download); }} />
); })}
) : ( No downloads available No downloads available for your platform ( {getPlatformDisplayName(platformInfo.platform)}). )} )} )}
); };