diff --git a/webui/src/components/layout/SettingsPanel.tsx b/webui/src/components/layout/SettingsPanel.tsx index 655592d2..ab9970b0 100644 --- a/webui/src/components/layout/SettingsPanel.tsx +++ b/webui/src/components/layout/SettingsPanel.tsx @@ -1,10 +1,11 @@ import { useState, useEffect } from 'react' -import { X, FolderOpen, Clipboard, Lock, LockOpen, RefreshCw, Download, CheckCircle2, AlertCircle, Loader2 } from 'lucide-react' +import { X, FolderOpen, Clipboard, Lock, LockOpen, RefreshCw, Download, CheckCircle2, AlertCircle, Loader2, Play, Shield, ExternalLink } from 'lucide-react' import { Button } from '../ui/button' import { Switch } from '../ui/switch' import { Label } from '../ui/label' import { cn } from '@/lib/utils' import { toast } from 'sonner' +import { useProxyContext } from '@/hooks/useProxyContext' interface SettingsPanelProps { isOpen: boolean @@ -15,12 +16,26 @@ interface UpdateInfo { available: boolean version: string download_url: string + release_notes?: string } +interface UpdateStatus { + downloading: boolean + progress: { total_bytes: number; downloaded_bytes: number; percent: number } + ready: boolean + error: string +} + +type UpdateStage = 'idle' | 'checking' | 'available' | 'downloading' | 'verifying' | 'ready' | 'installing' + export function SettingsPanel({ isOpen, onClose }: SettingsPanelProps) { + const { mgmtFetch } = useProxyContext() const [privateOAuth, setPrivateOAuth] = useState(false) const [checkingUpdates, setCheckingUpdates] = useState(false) const [updateInfo, setUpdateInfo] = useState(null) + const [updateStage, setUpdateStage] = useState('idle') + const [downloadProgress, setDownloadProgress] = useState(0) + const [updateError, setUpdateError] = useState(null) // Load settings on mount useEffect(() => { @@ -36,6 +51,40 @@ export function SettingsPanel({ isOpen, onClose }: SettingsPanelProps) { })() }, []) + // Poll for download status when downloading + useEffect(() => { + if (updateStage !== 'downloading') return + + const pollStatus = async () => { + try { + let status: UpdateStatus + if (mgmtFetch) { + status = await mgmtFetch('/v0/management/updates/status') + } else { + return + } + + if (status.error) { + setUpdateError(status.error) + setUpdateStage('available') + return + } + + if (status.downloading) { + setDownloadProgress(status.progress?.percent || 0) + } else if (status.ready) { + setUpdateStage('ready') + setDownloadProgress(100) + } + } catch (e) { + console.error('Failed to poll update status:', e) + } + } + + const interval = setInterval(pollStatus, 500) + return () => clearInterval(interval) + }, [updateStage, mgmtFetch]) + const handlePrivateOAuthChange = async (checked: boolean) => { try { if (window.pp_set_oauth_private) { @@ -83,17 +132,26 @@ export function SettingsPanel({ isOpen, onClose }: SettingsPanelProps) { const handleCheckUpdates = async () => { setCheckingUpdates(true) + setUpdateStage('checking') + setUpdateError(null) try { + let info: UpdateInfo | null = null if (window.pp_check_updates) { - const info = await window.pp_check_updates() - setUpdateInfo(info) - if (info?.available) { - toast.success(`Update available: ${info.version}`) - } else { - toast.success('You are on the latest version') - } + info = await window.pp_check_updates() + } else if (mgmtFetch) { + info = await mgmtFetch('/v0/management/updates/check') + } + + setUpdateInfo(info) + if (info?.available) { + setUpdateStage('available') + toast.success(`Update available: v${info.version}`) + } else { + setUpdateStage('idle') + toast.success('You are on the latest version') } } catch (e) { + setUpdateStage('idle') toast.error(e instanceof Error ? e.message : String(e)) } finally { setCheckingUpdates(false) @@ -101,17 +159,57 @@ export function SettingsPanel({ isOpen, onClose }: SettingsPanelProps) { } const handleDownloadUpdate = async () => { - if (!updateInfo?.download_url) return + if (!updateInfo?.version) return + setUpdateStage('downloading') + setDownloadProgress(0) + setUpdateError(null) + try { - if (window.pp_download_update) { + if (mgmtFetch) { + await mgmtFetch('/v0/management/updates/download', { + method: 'POST', + body: JSON.stringify({ version: updateInfo.version }), + }) + } else if (window.pp_download_update) { + // Fallback to opening download page await window.pp_download_update(updateInfo.download_url) + setUpdateStage('available') toast.success('Opening download page...') } } catch (e) { + setUpdateStage('available') + setUpdateError(e instanceof Error ? e.message : String(e)) toast.error(e instanceof Error ? e.message : String(e)) } } + const handleInstallUpdate = async () => { + setUpdateStage('installing') + try { + if (mgmtFetch) { + const result = await mgmtFetch('/v0/management/updates/install', { + method: 'POST', + }) + if (result?.success) { + toast.success(result.message || 'Update installed! Restarting...') + // Application will restart + } else { + throw new Error(result?.message || 'Installation failed') + } + } + } catch (e) { + setUpdateStage('ready') + setUpdateError(e instanceof Error ? e.message : String(e)) + toast.error(e instanceof Error ? e.message : String(e)) + } + } + + const handleOpenReleasePage = () => { + if (updateInfo?.download_url) { + window.open(updateInfo.download_url, '_blank') + } + } + if (!isOpen) return null return ( @@ -255,11 +353,12 @@ export function SettingsPanel({ isOpen, onClose }: SettingsPanelProps) { Updates + {/* Check for Updates Button */} - {updateInfo && ( + {/* Update Status Card */} + {(updateInfo || updateStage !== 'idle') && (
+ {/* Header */}
- {updateInfo.available ? ( + {updateStage === 'ready' ? ( + + ) : updateStage === 'installing' ? ( + + ) : updateInfo?.available ? ( ) : ( )}

- {updateInfo.available ? `v${updateInfo.version} available` : 'Up to date'} + {updateStage === 'downloading' && 'Downloading...'} + {updateStage === 'ready' && `v${updateInfo?.version} ready to install`} + {updateStage === 'installing' && 'Installing update...'} + {updateStage === 'available' && `v${updateInfo?.version} available`} + {updateStage === 'idle' && !updateInfo?.available && 'Up to date'}

- {updateInfo.available && ( + {updateInfo?.release_notes && !updateInfo?.available && ( +

+ {updateInfo.release_notes} +

+ )} +
+
+ + {/* Download Progress */} + {updateStage === 'downloading' && ( +
+
+
+
+

+ {downloadProgress.toFixed(0)}% complete +

+
+ )} + + {/* Error Message */} + {updateError && ( +
+ {updateError} +
+ )} + + {/* Action Buttons */} +
+ {updateStage === 'available' && ( + <> - )} -
+ + + )} + + {updateStage === 'ready' && ( + + )} + + {updateStage === 'installing' && ( +
+ Please wait, do not close the application... +
+ )}
)}