From 37b00e27ea9b7c1b4342fe218ab85b16275a5922 Mon Sep 17 00:00:00 2001 From: Chris Scott Date: Sat, 29 Nov 2025 09:48:40 -0500 Subject: [PATCH] feat: add global toast system with installation event notifications - Add SSE event types for installation updates - Implement global toast system using Sonner - Add restart functionality directly in toast actions - Update .gitignore to exclude temp directory --- .gitignore | 1 + frontend/src/App.tsx | 7 +++ frontend/src/api/types.ts | 16 +++++++ frontend/src/hooks/useSSE.ts | 85 ++++++++++++++++++++++++++++++++++++ frontend/src/lib/toast.ts | 39 +++++++++++++++++ 5 files changed, 148 insertions(+) create mode 100644 frontend/src/lib/toast.ts diff --git a/.gitignore b/.gitignore index aed20c54..2c95b307 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,5 @@ coverage/ data/ workspace/ /config/ +temp/ diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 99d84a76..e1224832 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,5 +1,6 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import { BrowserRouter, Routes, Route } from 'react-router-dom' +import { Toaster } from 'sonner' import { Repos } from './pages/Repos' import { RepoDetail } from './pages/RepoDetail' import { SessionDetail } from './pages/SessionDetail' @@ -29,6 +30,12 @@ function AppContent() { } /> + ) } diff --git a/frontend/src/api/types.ts b/frontend/src/api/types.ts index e4a14f3e..8ec93415 100644 --- a/frontend/src/api/types.ts +++ b/frontend/src/api/types.ts @@ -102,6 +102,20 @@ export interface SSEPermissionRepliedEvent { } } +export interface SSEInstallationUpdatedEvent { + type: 'installation.updated' + properties: { + version: string + } +} + +export interface SSEInstallationUpdateAvailableEvent { + type: 'installation.update-available' + properties: { + version: string + } +} + export type SSEEvent = | SSEMessagePartUpdatedEvent | SSEMessageUpdatedEvent @@ -113,6 +127,8 @@ export type SSEEvent = | SSETodoUpdatedEvent | SSEPermissionUpdatedEvent | SSEPermissionRepliedEvent + | SSEInstallationUpdatedEvent + | SSEInstallationUpdateAvailableEvent export type ContentPart = | { type: 'text', content: string } diff --git a/frontend/src/hooks/useSSE.ts b/frontend/src/hooks/useSSE.ts index 98477205..37c76262 100644 --- a/frontend/src/hooks/useSSE.ts +++ b/frontend/src/hooks/useSSE.ts @@ -3,10 +3,73 @@ import { useQueryClient } from '@tanstack/react-query' import { useOpenCodeClient } from './useOpenCode' import type { SSEEvent, MessageListResponse } from '@/api/types' import { permissionEvents } from './usePermissionRequests' +import { showToast } from '@/lib/toast' +import { settingsApi } from '@/api/settings' const MAX_RECONNECT_DELAY = 30000 const INITIAL_RECONNECT_DELAY = 1000 +const handleRestartServer = async () => { + showToast.loading('Restarting OpenCode server...', { + id: 'restart-server', + }) + + try { + const result = await settingsApi.restartOpenCodeServer() + if (result.success) { + showToast.success(result.message || 'OpenCode server restarted successfully', { + id: 'restart-server', + duration: 3000, + }) + setTimeout(() => { + window.location.reload() + }, 2000) + } else { + showToast.error(result.message || 'Failed to restart OpenCode server', { + id: 'restart-server', + duration: 5000, + }) + } + } catch (error) { + showToast.error(error instanceof Error ? error.message : 'Failed to restart OpenCode server', { + id: 'restart-server', + duration: 5000, + }) + } +} + +// Debug function to emulate toasts - remove in production +if (typeof window !== 'undefined') { + (window as any).testToasts = { + updateAvailable: () => { + showToast.info(`OpenCode v1.2.3 is available`, { + description: 'A new version is ready to install.', + action: { + label: 'Restart to Update', + onClick: handleRestartServer + }, + duration: 10000, + }) + }, + updated: () => { + showToast.success(`OpenCode updated to v1.2.3`, { + description: 'The server has been successfully upgraded.', + duration: 5000, + }) + }, + restartTest: () => { + handleRestartServer() + }, + basic: () => { + showToast.info('This is a basic info toast') + showToast.success('This is a success toast') + showToast.warning('This is a warning toast') + showToast.error('This is an error toast') + } + } + console.log('🍞 Toast test functions available: window.testToasts.updateAvailable(), window.testToasts.updated(), window.testToasts.restartTest(), window.testToasts.basic()') +} + export const useSSE = (opcodeUrl: string | null | undefined, directory?: string) => { const client = useOpenCodeClient(opcodeUrl, directory) const queryClient = useQueryClient() @@ -228,6 +291,28 @@ export const useSSE = (opcodeUrl: string | null | undefined, directory?: string) } break + case 'installation.updated': + if ('version' in event.properties) { + showToast.success(`OpenCode updated to v${event.properties.version}`, { + description: 'The server has been successfully upgraded.', + duration: 5000, + }) + } + break + + case 'installation.update-available': + if ('version' in event.properties) { + showToast.info(`OpenCode v${event.properties.version} is available`, { + description: 'A new version is ready to install.', + action: { + label: 'Restart to Update', + onClick: handleRestartServer + }, + duration: 10000, + }) + } + break + default: break } diff --git a/frontend/src/lib/toast.ts b/frontend/src/lib/toast.ts new file mode 100644 index 00000000..4ffb5ca3 --- /dev/null +++ b/frontend/src/lib/toast.ts @@ -0,0 +1,39 @@ +import { toast } from 'sonner' + +interface ToastOptions { + duration?: number + description?: string + action?: { + label: string + onClick: () => void + } + id?: string | number +} + +export const showToast = { + success: (message: string, options?: ToastOptions) => { + return toast.success(message, options) + }, + + error: (message: string, options?: ToastOptions) => { + return toast.error(message, options) + }, + + info: (message: string, options?: ToastOptions) => { + return toast.info(message, options) + }, + + warning: (message: string, options?: ToastOptions) => { + return toast.warning(message, options) + }, + + loading: (message: string, options?: ToastOptions) => { + return toast.loading(message, options) + }, + + dismiss: (id?: string | number) => { + toast.dismiss(id) + } +} + +export type ShowToast = typeof showToast \ No newline at end of file