Revert text editor, move more into config file

This commit is contained in:
2025-02-08 16:53:30 -05:00
parent 68dce7ee2a
commit 62b7552609
7 changed files with 153 additions and 342 deletions

View File

@@ -1,57 +1,28 @@
import { useState, useEffect } from 'react'
import yaml from 'js-yaml'
import { ServiceCard } from '../types/services'
interface Config {
services: ServiceCard[]
}
import { Config } from '../types/services'
export function useConfig() {
const [config, setConfig] = useState<Config>({ services: [] })
const [config, setConfig] = useState<Config>({ services: [], sections: [] })
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(() => {
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)
}
}
// Load config immediately
loadConfig()
@@ -61,5 +32,5 @@ export function useConfig() {
return () => clearInterval(interval)
}, [])
return { config, error, saveConfig }
return { config, error }
}