9d9bc5f22f
- Fix handleRotate bug ignoring Result from rotateSealedSecret() - Fix dead code branch in useControllerHealth - Replace all `any` types with `unknown` + type guards - Delete unused functions/exports (452 lines removed) - Add 18 new test files covering all hooks, libs, and components - 233 tests passing, zero tsc errors, zero lint issues Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
69 lines
1.9 KiB
TypeScript
69 lines
1.9 KiB
TypeScript
/**
|
|
* Custom Hook for Controller Health Monitoring
|
|
*
|
|
* Provides controller health status with automatic refresh capability.
|
|
*/
|
|
|
|
import React from 'react';
|
|
import { checkControllerHealth, ControllerHealthStatus, getPluginConfig } from '../lib/controller';
|
|
|
|
/**
|
|
* Custom hook for monitoring controller health
|
|
*
|
|
* Automatically checks controller health on mount and can optionally
|
|
* refresh at a specified interval.
|
|
*
|
|
* @param autoRefresh Whether to automatically refresh health status
|
|
* @param refreshIntervalMs Refresh interval in milliseconds (default: 30000ms = 30s)
|
|
* @returns Object with health status, loading state, and manual refresh function
|
|
*
|
|
* @example
|
|
* // Manual refresh only
|
|
* const { health, loading, refresh } = useControllerHealth();
|
|
*
|
|
* // Auto-refresh every 30 seconds
|
|
* const { health, loading } = useControllerHealth(true, 30000);
|
|
*
|
|
* // Auto-refresh every 10 seconds
|
|
* const { health, loading } = useControllerHealth(true, 10000);
|
|
*/
|
|
export function useControllerHealth(autoRefresh = false, refreshIntervalMs = 30000) {
|
|
const [health, setHealth] = React.useState<ControllerHealthStatus | null>(null);
|
|
const [loading, setLoading] = React.useState(true);
|
|
|
|
const fetchHealth = React.useCallback(async () => {
|
|
setLoading(true);
|
|
|
|
const config = getPluginConfig();
|
|
const result = await checkControllerHealth(config);
|
|
|
|
if (result.ok === false) {
|
|
setHealth({
|
|
healthy: false,
|
|
reachable: false,
|
|
error: result.error,
|
|
});
|
|
} else {
|
|
setHealth(result.value);
|
|
}
|
|
|
|
setLoading(false);
|
|
}, []);
|
|
|
|
// Initial fetch and auto-refresh setup
|
|
React.useEffect(() => {
|
|
fetchHealth();
|
|
|
|
if (autoRefresh) {
|
|
const interval = setInterval(fetchHealth, refreshIntervalMs);
|
|
return () => clearInterval(interval);
|
|
}
|
|
}, [autoRefresh, refreshIntervalMs, fetchHealth]);
|
|
|
|
return {
|
|
health,
|
|
loading,
|
|
refresh: fetchHealth,
|
|
};
|
|
}
|