import { useState, useEffect } from 'react' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { AlertCircle, Loader2 } from 'lucide-react' import { settingsApi } from '@/api/settings' import * as reposApi from '@/api/repos' import type { OpenCodeConfig } from '@/api/types/settings' interface SwitchConfigDialogProps { open: boolean onOpenChange: (open: boolean) => void repoId: number currentConfigName?: string onConfigSwitched: (configName: string) => void } export function SwitchConfigDialog({ open, onOpenChange, repoId, currentConfigName, onConfigSwitched, }: SwitchConfigDialogProps) { const [configs, setConfigs] = useState([]) const [selectedConfig, setSelectedConfig] = useState('') const [loading, setLoading] = useState(true) const [switching, setSwitching] = useState(false) const [error, setError] = useState(null) useEffect(() => { if (!open) return const fetchConfigs = async () => { try { setLoading(true) setError(null) const response = await settingsApi.getOpenCodeConfigs() setConfigs(response.configs || []) setSelectedConfig(currentConfigName || '') } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load configs') } finally { setLoading(false) } } fetchConfigs() }, [open, currentConfigName]) const handleSwitch = async () => { if (!selectedConfig) { setError('Please select a config') return } if (selectedConfig === currentConfigName) { onOpenChange(false) return } try { setSwitching(true) setError(null) await reposApi.switchRepoConfig(repoId, selectedConfig) onConfigSwitched(selectedConfig) onOpenChange(false) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to switch config') } finally { setSwitching(false) } } return ( Switch Config Select a different OpenCode configuration for this repository
{currentConfigName && (
Current config: {currentConfigName}
)} {loading ? (
Loading configs...
) : configs.length === 0 ? (
No configs available
) : ( )} {error && (

{error}

)}
) }