Files
OllaHub/hooks/use-system-health.ts
T
Evandro Fonseca Junior bd1e3ca2e6 refactor: optimize system health monitoring and UI interactions
- Removed unnecessary delay in chat redirection for immediate user feedback.
- Enhanced TitleBar component with a system health indicator, displaying RAM and CPU usage.
- Implemented optimistic UI updates for model downloads and Ollama installation, improving responsiveness.
- Introduced background health monitoring to prevent resource overload during operations.
- Added cleanup logic for child processes to ensure proper resource management during application shutdown.
2025-11-28 15:42:40 -03:00

54 lines
1.5 KiB
TypeScript

'use client';
import { useEffect, useState } from 'react';
import { listen } from '@tauri-apps/api/event';
import { useToast } from '@/hooks/use-toast';
export type HealthStatus = 'healthy' | 'warning' | 'critical';
export interface SystemHealth {
status: HealthStatus;
ram_percent: number;
cpu_percent: number;
message: string;
}
export function useSystemHealth() {
const [health, setHealth] = useState<SystemHealth>({
status: 'healthy',
ram_percent: 0,
cpu_percent: 0,
message: 'Sistema saudável',
});
const [hasShownCriticalToast, setHasShownCriticalToast] = useState(false);
const { toast } = useToast();
useEffect(() => {
const unlistenPromise = listen<SystemHealth>('system-state-change', (event) => {
const newHealth = event.payload;
setHealth(newHealth);
// Mostrar toast apenas quando entrar em modo Critical (uma vez)
if (newHealth.status === 'critical' && !hasShownCriticalToast) {
toast({
title: 'Modo de Economia Ativado',
description: 'Recursos limitados para manter estabilidade. Algumas funcionalidades podem estar temporariamente indisponíveis.',
variant: 'destructive',
duration: 5000,
});
setHasShownCriticalToast(true);
} else if (newHealth.status !== 'critical') {
// Reset flag quando sair do modo crítico
setHasShownCriticalToast(false);
}
});
return () => {
unlistenPromise.then((unlisten) => unlisten());
};
}, [hasShownCriticalToast, toast]);
return health;
}