Refactor ConfigContext hooks

This commit is contained in:
2025-11-15 01:02:15 +01:00
parent ad772a05ce
commit 6565be3676
7 changed files with 58 additions and 71 deletions

View File

@@ -48,8 +48,8 @@ vi.mock('@/lib/healthService', () => ({
})), })),
})) }))
// Mock the ConfigContext hooks // Mock the ConfigContext helper hooks
vi.mock('@/contexts/ConfigContext', () => ({ vi.mock('@/hooks/useConfig', () => ({
useInstanceDefaults: () => ({ useInstanceDefaults: () => ({
autoRestart: true, autoRestart: true,
maxRestarts: 3, maxRestarts: 3,

View File

@@ -14,7 +14,7 @@ import ParseCommandDialog from "@/components/ParseCommandDialog";
import InstanceSettingsCard from "@/components/instance/InstanceSettingsCard"; import InstanceSettingsCard from "@/components/instance/InstanceSettingsCard";
import BackendConfigurationCard from "@/components/instance/BackendConfigurationCard"; import BackendConfigurationCard from "@/components/instance/BackendConfigurationCard";
import { Upload } from "lucide-react"; import { Upload } from "lucide-react";
import { useInstanceDefaults, useBackendSettings } from "@/contexts/ConfigContext"; import { useInstanceDefaults, useBackendSettings } from "@/hooks/useConfig";
interface InstanceDialogProps { interface InstanceDialogProps {
open: boolean; open: boolean;

View File

@@ -2,11 +2,10 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { render, screen, waitFor } from '@testing-library/react' import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event' import userEvent from '@testing-library/user-event'
import InstanceDialog from '@/components/InstanceDialog' import InstanceDialog from '@/components/InstanceDialog'
import type { Instance } from '@/types/instance' import { BackendType, type Instance } from '@/types/instance'
import { BackendType } from '@/types/instance'
// Mock the ConfigContext hooks // Mock the ConfigContext helper hooks
vi.mock('@/contexts/ConfigContext', () => ({ vi.mock('@/hooks/useConfig', () => ({
useInstanceDefaults: () => ({ useInstanceDefaults: () => ({
autoRestart: true, autoRestart: true,
maxRestarts: 3, maxRestarts: 3,

View File

@@ -3,7 +3,7 @@ import { BackendType, type CreateInstanceOptions } from '@/types/instance'
import CheckboxInput from '@/components/form/CheckboxInput' import CheckboxInput from '@/components/form/CheckboxInput'
import TextInput from '@/components/form/TextInput' import TextInput from '@/components/form/TextInput'
import EnvVarsInput from '@/components/form/EnvVarsInput' import EnvVarsInput from '@/components/form/EnvVarsInput'
import { useBackendSettings } from '@/contexts/ConfigContext' import { useBackendSettings } from '@/hooks/useConfig'
interface ExecutionContextSectionProps { interface ExecutionContextSectionProps {
formData: CreateInstanceOptions formData: CreateInstanceOptions

View File

@@ -147,16 +147,3 @@ export const useAuth = (): AuthContextType => {
} }
return context return context
} }
// Helper hook for getting auth headers
export const useAuthHeaders = (): HeadersInit => {
const { apiKey, isAuthenticated } = useAuth()
if (!isAuthenticated || !apiKey) {
return {}
}
return {
'Authorization': `Bearer ${apiKey}`
}
}

View File

@@ -60,53 +60,3 @@ export const useConfig = (): ConfigContextType => {
} }
return context 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 || '',
}
}

View File

@@ -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 || '',
}
}