import React, { useEffect, useState } from 'react'; import { Button } from './components/ui/button'; import { Input } from './components/ui/input'; import { Label } from './components/ui/label'; import { Card } from './components/ui/card'; import { useNavigate } from 'react-router-dom'; import BackButton from './components/ui/BackButton'; import { useConfig } from './hooks/useConfig'; export default function ConfigPage() { const [configs, setConfigs] = useState>({}); const [newKey, setNewKey] = useState(''); const [newValue, setNewValue] = useState(''); const navigate = useNavigate(); const { loading, error, loadConfigs, addConfig, removeConfig } = useConfig(); // Fetch all configs on component mount useEffect(() => { const fetchConfigs = async () => { const result = await loadConfigs(); setConfigs(result); }; fetchConfigs(); }, [loadConfigs]); const handleAddConfig = async () => { if (!newKey || !newValue) { return; } let parsedValue = newValue; // Try to parse as JSON if it looks like JSON if (newValue.trim().startsWith('{') || newValue.trim().startsWith('[')) { try { parsedValue = JSON.parse(newValue); } catch (e) { // If parsing fails, use the original string value console.log('Value is not valid JSON, using as string'); } } const success = await addConfig(newKey, parsedValue); if (success) { setNewKey(''); setNewValue(''); const updatedConfigs = await loadConfigs(); setConfigs(updatedConfigs); } }; const handleRemoveConfig = async (key: string) => { const success = await removeConfig(key); if (success) { const updatedConfigs = await loadConfigs(); setConfigs(updatedConfigs); } }; return (
navigate('/settings')} />

Configuration

{/* Add new config form */}

Add New Configuration

setNewKey(e.target.value)} placeholder="Enter config key" className="mt-1" />
setNewValue(e.target.value)} placeholder="Enter config value (string or JSON)" className="mt-1" />
{/* Error display */} {error && (
{error.message}
)} {/* Config list */}

Current Configurations

{loading ? (
Loading configurations...
) : Object.keys(configs).length === 0 ? (
No configurations found
) : ( Object.entries(configs).map(([key, value]) => (
{key}:{' '} {typeof value === 'object' ? JSON.stringify(value, null, 2) : String(value)}
)) )}
); }