From 6565be3676a6521912dbe0398f55a9edaa2aff8c Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sat, 15 Nov 2025 01:02:15 +0100 Subject: [PATCH] Refactor ConfigContext hooks --- webui/src/__tests__/App.test.tsx | 4 +- webui/src/components/InstanceDialog.tsx | 2 +- .../__tests__/InstanceModal.test.tsx | 7 ++- .../instance/ExecutionContextSection.tsx | 2 +- webui/src/contexts/AuthContext.tsx | 13 ----- webui/src/contexts/ConfigContext.tsx | 50 ------------------ webui/src/hooks/useConfig.ts | 51 +++++++++++++++++++ 7 files changed, 58 insertions(+), 71 deletions(-) create mode 100644 webui/src/hooks/useConfig.ts diff --git a/webui/src/__tests__/App.test.tsx b/webui/src/__tests__/App.test.tsx index 79fafe5..eb212a4 100644 --- a/webui/src/__tests__/App.test.tsx +++ b/webui/src/__tests__/App.test.tsx @@ -48,8 +48,8 @@ vi.mock('@/lib/healthService', () => ({ })), })) -// Mock the ConfigContext hooks -vi.mock('@/contexts/ConfigContext', () => ({ +// Mock the ConfigContext helper hooks +vi.mock('@/hooks/useConfig', () => ({ useInstanceDefaults: () => ({ autoRestart: true, maxRestarts: 3, diff --git a/webui/src/components/InstanceDialog.tsx b/webui/src/components/InstanceDialog.tsx index 58432a7..638a45a 100644 --- a/webui/src/components/InstanceDialog.tsx +++ b/webui/src/components/InstanceDialog.tsx @@ -14,7 +14,7 @@ import ParseCommandDialog from "@/components/ParseCommandDialog"; import InstanceSettingsCard from "@/components/instance/InstanceSettingsCard"; import BackendConfigurationCard from "@/components/instance/BackendConfigurationCard"; import { Upload } from "lucide-react"; -import { useInstanceDefaults, useBackendSettings } from "@/contexts/ConfigContext"; +import { useInstanceDefaults, useBackendSettings } from "@/hooks/useConfig"; interface InstanceDialogProps { open: boolean; diff --git a/webui/src/components/__tests__/InstanceModal.test.tsx b/webui/src/components/__tests__/InstanceModal.test.tsx index 3010195..3b84d25 100644 --- a/webui/src/components/__tests__/InstanceModal.test.tsx +++ b/webui/src/components/__tests__/InstanceModal.test.tsx @@ -2,11 +2,10 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' import { render, screen, waitFor } from '@testing-library/react' import userEvent from '@testing-library/user-event' import InstanceDialog from '@/components/InstanceDialog' -import type { Instance } from '@/types/instance' -import { BackendType } from '@/types/instance' +import { BackendType, type Instance } from '@/types/instance' -// Mock the ConfigContext hooks -vi.mock('@/contexts/ConfigContext', () => ({ +// Mock the ConfigContext helper hooks +vi.mock('@/hooks/useConfig', () => ({ useInstanceDefaults: () => ({ autoRestart: true, maxRestarts: 3, diff --git a/webui/src/components/instance/ExecutionContextSection.tsx b/webui/src/components/instance/ExecutionContextSection.tsx index 7f4ddb1..cdd73cd 100644 --- a/webui/src/components/instance/ExecutionContextSection.tsx +++ b/webui/src/components/instance/ExecutionContextSection.tsx @@ -3,7 +3,7 @@ import { BackendType, type CreateInstanceOptions } from '@/types/instance' import CheckboxInput from '@/components/form/CheckboxInput' import TextInput from '@/components/form/TextInput' import EnvVarsInput from '@/components/form/EnvVarsInput' -import { useBackendSettings } from '@/contexts/ConfigContext' +import { useBackendSettings } from '@/hooks/useConfig' interface ExecutionContextSectionProps { formData: CreateInstanceOptions diff --git a/webui/src/contexts/AuthContext.tsx b/webui/src/contexts/AuthContext.tsx index d3b8a12..edeb501 100644 --- a/webui/src/contexts/AuthContext.tsx +++ b/webui/src/contexts/AuthContext.tsx @@ -147,16 +147,3 @@ export const useAuth = (): AuthContextType => { } return context } - -// Helper hook for getting auth headers -export const useAuthHeaders = (): HeadersInit => { - const { apiKey, isAuthenticated } = useAuth() - - if (!isAuthenticated || !apiKey) { - return {} - } - - return { - 'Authorization': `Bearer ${apiKey}` - } -} diff --git a/webui/src/contexts/ConfigContext.tsx b/webui/src/contexts/ConfigContext.tsx index fe19e96..a42e9b1 100644 --- a/webui/src/contexts/ConfigContext.tsx +++ b/webui/src/contexts/ConfigContext.tsx @@ -60,53 +60,3 @@ export const useConfig = (): ConfigContextType => { } return context } - -// Helper hook to get instance default values from config -export const useInstanceDefaults = () => { - const { config } = useConfig() - - if (!config || !config.instances) { - return null - } - - return { - autoRestart: config.instances.default_auto_restart, - maxRestarts: config.instances.default_max_restarts, - restartDelay: config.instances.default_restart_delay, - onDemandStart: config.instances.default_on_demand_start, - } -} - -// Helper hook to get specific backend settings by backend type -export const useBackendSettings = (backendType: string | undefined) => { - const { config } = useConfig() - - if (!config || !config.backends || !backendType) { - return null - } - - // Map backend type to config key - const backendKey = backendType === 'llama_cpp' - ? 'llama-cpp' - : backendType === 'mlx_lm' - ? 'mlx' - : backendType === 'vllm' - ? 'vllm' - : null - - if (!backendKey) { - return null - } - - const backendConfig = config.backends[backendKey as keyof typeof config.backends] - - if (!backendConfig) { - return null - } - - return { - command: backendConfig.command || '', - dockerEnabled: backendConfig.docker?.enabled ?? false, - dockerImage: backendConfig.docker?.image || '', - } -} diff --git a/webui/src/hooks/useConfig.ts b/webui/src/hooks/useConfig.ts new file mode 100644 index 0000000..4615be5 --- /dev/null +++ b/webui/src/hooks/useConfig.ts @@ -0,0 +1,51 @@ +import { useConfig } from '@/contexts/ConfigContext' + +// Helper hook to get instance default values from config +export const useInstanceDefaults = () => { + const { config } = useConfig() + + if (!config || !config.instances) { + return null + } + + return { + autoRestart: config.instances.default_auto_restart, + maxRestarts: config.instances.default_max_restarts, + restartDelay: config.instances.default_restart_delay, + onDemandStart: config.instances.default_on_demand_start, + } +} + +// Helper hook to get specific backend settings by backend type +export const useBackendSettings = (backendType: string | undefined) => { + const { config } = useConfig() + + if (!config || !config.backends || !backendType) { + return null + } + + // Map backend type to config key + const backendKey = backendType === 'llama_cpp' + ? 'llama-cpp' + : backendType === 'mlx_lm' + ? 'mlx' + : backendType === 'vllm' + ? 'vllm' + : null + + if (!backendKey) { + return null + } + + const backendConfig = config.backends[backendKey as keyof typeof config.backends] + + if (!backendConfig) { + return null + } + + return { + command: backendConfig.command || '', + dockerEnabled: backendConfig.docker?.enabled ?? false, + dockerImage: backendConfig.docker?.image || '', + } +}