|
| 1 | +import { useCallback, useEffect, useRef, useState } from 'react'; |
| 2 | +import { createPortal } from 'react-dom'; |
| 3 | + |
| 4 | +import { |
| 5 | + formatBytes, |
| 6 | + formatEta, |
| 7 | + progressFromDownloads, |
| 8 | + statusLabel, |
| 9 | +} from '../utils/localAiHelpers'; |
| 10 | +import { |
| 11 | + isTauri, |
| 12 | + type LocalAiDownloadsProgress, |
| 13 | + type LocalAiStatus, |
| 14 | + openhumanLocalAiDownloadsProgress, |
| 15 | + openhumanLocalAiStatus, |
| 16 | +} from '../utils/tauriCommands'; |
| 17 | + |
| 18 | +const POLL_INTERVAL = 2000; |
| 19 | + |
| 20 | +/** |
| 21 | + * Persistent snackbar that shows local AI download progress. |
| 22 | + * Anchored bottom-left to avoid conflict with ErrorReportNotification (bottom-right). |
| 23 | + * Dismiss hides the UI but does NOT cancel the download. |
| 24 | + */ |
| 25 | +const LocalAIDownloadSnackbar = () => { |
| 26 | + const [status, setStatus] = useState<LocalAiStatus | null>(null); |
| 27 | + const [downloads, setDownloads] = useState<LocalAiDownloadsProgress | null>(null); |
| 28 | + const [dismissed, setDismissed] = useState(false); |
| 29 | + const [collapsed, setCollapsed] = useState(false); |
| 30 | + const timerRef = useRef<ReturnType<typeof setInterval>>(undefined); |
| 31 | + |
| 32 | + // Check Tauri availability once at init |
| 33 | + const tauriAvailable = (() => { |
| 34 | + try { |
| 35 | + return isTauri(); |
| 36 | + } catch { |
| 37 | + return false; |
| 38 | + } |
| 39 | + })(); |
| 40 | + |
| 41 | + // Poll download status |
| 42 | + useEffect(() => { |
| 43 | + if (!tauriAvailable) return; |
| 44 | + |
| 45 | + const poll = async () => { |
| 46 | + try { |
| 47 | + const [statusRes, downloadsRes] = await Promise.all([ |
| 48 | + openhumanLocalAiStatus(), |
| 49 | + openhumanLocalAiDownloadsProgress(), |
| 50 | + ]); |
| 51 | + if (statusRes.result) setStatus(statusRes.result); |
| 52 | + if (downloadsRes.result) setDownloads(downloadsRes.result); |
| 53 | + } catch { |
| 54 | + // Silently ignore — core may not be ready |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + void poll(); |
| 59 | + timerRef.current = setInterval(poll, POLL_INTERVAL); |
| 60 | + return () => clearInterval(timerRef.current); |
| 61 | + }, [tauriAvailable]); |
| 62 | + |
| 63 | + const isDownloading = |
| 64 | + status?.state === 'downloading' || |
| 65 | + downloads?.state === 'downloading' || |
| 66 | + (downloads?.progress != null && downloads.progress > 0 && downloads.progress < 1); |
| 67 | + |
| 68 | + // Auto-show when a new download starts: track prior state in a ref and |
| 69 | + // reset dismissed on the transition edge (not-downloading → downloading). |
| 70 | + const wasDownloadingRef = useRef(false); |
| 71 | + if (isDownloading && !wasDownloadingRef.current && dismissed) { |
| 72 | + setDismissed(false); |
| 73 | + } |
| 74 | + wasDownloadingRef.current = !!isDownloading; |
| 75 | + |
| 76 | + const handleDismiss = useCallback(() => setDismissed(true), []); |
| 77 | + const handleToggleCollapse = useCallback(() => setCollapsed(prev => !prev), []); |
| 78 | + |
| 79 | + if (!tauriAvailable || !isDownloading || dismissed) return null; |
| 80 | + |
| 81 | + const progress = progressFromDownloads(downloads); |
| 82 | + const percent = progress != null ? Math.round(progress * 100) : null; |
| 83 | + const speed = downloads?.speed_bps; |
| 84 | + const eta = downloads?.eta_seconds; |
| 85 | + const downloaded = downloads?.downloaded_bytes; |
| 86 | + const total = downloads?.total_bytes; |
| 87 | + const label = statusLabel(downloads?.state ?? status?.state ?? 'downloading'); |
| 88 | + |
| 89 | + // Collapsed: small pill |
| 90 | + if (collapsed) { |
| 91 | + return createPortal( |
| 92 | + <div className="fixed bottom-4 left-4 z-[9998] animate-fade-up"> |
| 93 | + <button |
| 94 | + onClick={handleToggleCollapse} |
| 95 | + className="flex items-center gap-2 bg-stone-900 border border-stone-700/50 rounded-full px-3 py-2 shadow-large hover:border-stone-600 transition-colors" |
| 96 | + aria-label="Expand download progress"> |
| 97 | + <svg |
| 98 | + className="w-4 h-4 text-primary-400 animate-pulse" |
| 99 | + viewBox="0 0 20 20" |
| 100 | + fill="currentColor"> |
| 101 | + <path d="M10.75 2.75a.75.75 0 00-1.5 0v8.614L6.295 8.235a.75.75 0 10-1.09 1.03l4.25 4.5a.75.75 0 001.09 0l4.25-4.5a.75.75 0 00-1.09-1.03l-2.955 3.129V2.75z" /> |
| 102 | + <path d="M3.5 12.75a.75.75 0 00-1.5 0v2.5A2.75 2.75 0 004.75 18h10.5A2.75 2.75 0 0018 15.25v-2.5a.75.75 0 00-1.5 0v2.5c0 .69-.56 1.25-1.25 1.25H4.75c-.69 0-1.25-.56-1.25-1.25v-2.5z" /> |
| 103 | + </svg> |
| 104 | + <span className="text-xs font-medium text-stone-300"> |
| 105 | + {percent != null ? `${percent}%` : label} |
| 106 | + </span> |
| 107 | + </button> |
| 108 | + </div>, |
| 109 | + document.body |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + // Expanded: full snackbar |
| 114 | + return createPortal( |
| 115 | + <div className="fixed bottom-4 left-4 z-[9998] w-[320px] animate-fade-up"> |
| 116 | + <div className="bg-stone-900 border border-stone-700/50 rounded-2xl shadow-large overflow-hidden"> |
| 117 | + {/* Header */} |
| 118 | + <div className="flex items-center justify-between px-4 pt-3 pb-1"> |
| 119 | + <div className="flex items-center gap-2"> |
| 120 | + <svg |
| 121 | + className="w-4 h-4 text-primary-400 animate-pulse" |
| 122 | + viewBox="0 0 20 20" |
| 123 | + fill="currentColor"> |
| 124 | + <path d="M10.75 2.75a.75.75 0 00-1.5 0v8.614L6.295 8.235a.75.75 0 10-1.09 1.03l4.25 4.5a.75.75 0 001.09 0l4.25-4.5a.75.75 0 00-1.09-1.03l-2.955 3.129V2.75z" /> |
| 125 | + <path d="M3.5 12.75a.75.75 0 00-1.5 0v2.5A2.75 2.75 0 004.75 18h10.5A2.75 2.75 0 0018 15.25v-2.5a.75.75 0 00-1.5 0v2.5c0 .69-.56 1.25-1.25 1.25H4.75c-.69 0-1.25-.56-1.25-1.25v-2.5z" /> |
| 126 | + </svg> |
| 127 | + <span className="text-sm font-medium text-white">{label}</span> |
| 128 | + </div> |
| 129 | + <div className="flex items-center gap-1"> |
| 130 | + <button |
| 131 | + onClick={handleToggleCollapse} |
| 132 | + className="p-1 text-stone-500 hover:text-stone-300 transition-colors" |
| 133 | + aria-label="Collapse download progress"> |
| 134 | + <svg className="w-3.5 h-3.5" viewBox="0 0 16 16" fill="currentColor"> |
| 135 | + <path d="M3.75 7.25a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5z" /> |
| 136 | + </svg> |
| 137 | + </button> |
| 138 | + <button |
| 139 | + onClick={handleDismiss} |
| 140 | + className="p-1 text-stone-500 hover:text-stone-300 transition-colors" |
| 141 | + aria-label="Dismiss download notification"> |
| 142 | + <svg className="w-3.5 h-3.5" viewBox="0 0 16 16" fill="currentColor"> |
| 143 | + <path d="M4.28 3.22a.75.75 0 00-1.06 1.06L6.94 8l-3.72 3.72a.75.75 0 101.06 1.06L8 9.06l3.72 3.72a.75.75 0 101.06-1.06L9.06 8l3.72-3.72a.75.75 0 00-1.06-1.06L8 6.94 4.28 3.22z" /> |
| 144 | + </svg> |
| 145 | + </button> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + |
| 149 | + {/* Progress bar */} |
| 150 | + <div className="px-4 py-2"> |
| 151 | + <div className="h-1.5 w-full rounded-full bg-stone-800 overflow-hidden"> |
| 152 | + <div |
| 153 | + className="h-full rounded-full bg-gradient-to-r from-primary-500 to-primary-400 transition-all duration-500" |
| 154 | + style={{ width: `${percent ?? 0}%` }} |
| 155 | + /> |
| 156 | + </div> |
| 157 | + </div> |
| 158 | + |
| 159 | + {/* Details */} |
| 160 | + <div className="flex items-center justify-between px-4 pb-3 text-xs text-stone-400"> |
| 161 | + <span> |
| 162 | + {downloaded != null && total != null |
| 163 | + ? `${formatBytes(downloaded)} / ${formatBytes(total)}` |
| 164 | + : percent != null |
| 165 | + ? `${percent}%` |
| 166 | + : 'Preparing...'} |
| 167 | + </span> |
| 168 | + <span> |
| 169 | + {speed != null && speed > 0 ? `${formatBytes(speed)}/s` : ''} |
| 170 | + {eta != null && eta > 0 ? ` · ${formatEta(eta)}` : ''} |
| 171 | + </span> |
| 172 | + </div> |
| 173 | + </div> |
| 174 | + </div>, |
| 175 | + document.body |
| 176 | + ); |
| 177 | +}; |
| 178 | + |
| 179 | +export default LocalAIDownloadSnackbar; |
0 commit comments