Add config file and text editor + update server

This commit is contained in:
2025-02-08 14:02:16 -05:00
parent 44461ceef8
commit 68dce7ee2a
14 changed files with 722 additions and 166 deletions

65
src/hooks/useConfig.ts Normal file
View File

@@ -0,0 +1,65 @@
import { useState, useEffect } from 'react'
import yaml from 'js-yaml'
import { ServiceCard } from '../types/services'
interface Config {
services: ServiceCard[]
}
export function useConfig() {
const [config, setConfig] = useState<Config>({ services: [] })
const [error, setError] = useState<string | null>(null)
const loadConfig = async () => {
try {
const response = await fetch('/config.yaml')
if (!response.ok) {
throw new Error('Failed to load configuration')
}
const text = await response.text()
const parsed = yaml.load(text) as Config
setConfig(parsed)
setError(null)
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load configuration')
console.error('Error loading configuration:', err)
}
}
const saveConfig = async (newConfig: string) => {
try {
const response = await fetch('/api/config/update', {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'text/yaml',
},
body: newConfig,
})
if (!response.ok) {
const errorData = await response.json().catch(() => ({ error: 'Unknown error' }))
throw new Error(errorData.error || 'Failed to save configuration')
}
// Reload the config after saving
await loadConfig()
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to save configuration')
console.error('Error saving configuration:', err)
throw err
}
}
useEffect(() => {
// Load config immediately
loadConfig()
// Set up polling for config changes in production
const interval = setInterval(loadConfig, 30000) // Check every 30 seconds
return () => clearInterval(interval)
}, [])
return { config, error, saveConfig }
}