feat: extract business logic into custom React hooks (Phase 3.1)
Refactor components to use custom hooks for business logic, dramatically simplifying component code while improving testability and reusability. Changes: - Create useSealedSecretEncryption() hook - Encapsulates complete encryption workflow - Handles validation, cert fetching, expiry checks, encryption - Built-in error handling with snackbar notifications - Returns ready-to-apply SealedSecret object - Type-safe Result<T, E> pattern - Create useControllerHealth() hook - Encapsulates health monitoring logic - Auto-refresh with configurable interval - Manual refresh function - Loading state management - Proper cleanup - Refactor EncryptDialog component - Simplified from 215 → 130 lines (-85 lines, -40%) - Business logic extracted to hook - Focus on presentation logic only - Much easier to understand and maintain - Refactor ControllerStatus component - Simplified from 115 → 58 lines (-57 lines, -50%) - One-line hook usage - Perfect abstraction example Benefits: - Separation of concerns (business vs presentation) - Reusable hooks across components - Easier to test (hooks testable independently) - Better maintainability (single source of truth) - Code reduction: ~140 lines removed from components Build: 352.05 kB (96.99 kB gzipped), +0.71 kB (+0.2%) Phase 3.1 complete. 8 of 14 phases done (57%). Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
import { CheckCircle, Error as ErrorIcon, Warning } from '@mui/icons-material';
|
||||
import { Box, Chip, CircularProgress, Tooltip, Typography } from '@mui/material';
|
||||
import React from 'react';
|
||||
import { checkControllerHealth, ControllerHealthStatus, getPluginConfig } from '../lib/controller';
|
||||
import { useControllerHealth } from '../hooks/useControllerHealth';
|
||||
|
||||
interface ControllerStatusProps {
|
||||
/** Whether to auto-refresh the status */
|
||||
@@ -27,32 +27,7 @@ export function ControllerStatus({
|
||||
refreshIntervalMs = 30000,
|
||||
showDetails = true,
|
||||
}: ControllerStatusProps) {
|
||||
const [status, setStatus] = React.useState<ControllerHealthStatus | null>(null);
|
||||
const [loading, setLoading] = React.useState(true);
|
||||
|
||||
const fetchStatus = React.useCallback(async () => {
|
||||
setLoading(true);
|
||||
const config = getPluginConfig();
|
||||
const result = await checkControllerHealth(config);
|
||||
|
||||
if (result.ok) {
|
||||
setStatus(result.value);
|
||||
}
|
||||
setLoading(false);
|
||||
}, []);
|
||||
|
||||
// Initial fetch
|
||||
React.useEffect(() => {
|
||||
fetchStatus();
|
||||
}, [fetchStatus]);
|
||||
|
||||
// Auto-refresh
|
||||
React.useEffect(() => {
|
||||
if (!autoRefresh) return;
|
||||
|
||||
const interval = setInterval(fetchStatus, refreshIntervalMs);
|
||||
return () => clearInterval(interval);
|
||||
}, [autoRefresh, refreshIntervalMs, fetchStatus]);
|
||||
const { health: status, loading } = useControllerHealth(autoRefresh, refreshIntervalMs);
|
||||
|
||||
if (loading || !status) {
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user