import React, { useEffect, useState } from "react"; import EnvironmentEditor from "./EnvironmentEditor.jsx"; /** * EnvironmentSelector — Claude-Code-on-Web parity environment dropdown. * * Shows current environment name + gear icon. Gear opens the editor modal. * Fetches environments from /api/environments. */ export default function EnvironmentSelector({ activeEnvId, onEnvChange }) { const [envs, setEnvs] = useState([]); const [editorOpen, setEditorOpen] = useState(false); const [editingEnv, setEditingEnv] = useState(null); const fetchEnvs = async () => { try { const res = await fetch("/api/environments", { cache: "no-cache" }); if (!res.ok) return; const data = await res.json(); setEnvs(data.environments || []); } catch (err) { console.warn("Failed to fetch environments:", err); } }; useEffect(() => { fetchEnvs(); }, []); const activeEnv = envs.find((e) => e.id === activeEnvId) || envs[0] || { name: "Default", id: "default" }; const handleSave = async (config) => { try { const method = config.id ? "PUT" : "POST"; const url = config.id ? `/api/environments/${config.id}` : "/api/environments"; await fetch(url, { method, headers: { "Content-Type": "application/json" }, body: JSON.stringify(config), }); await fetchEnvs(); setEditorOpen(false); setEditingEnv(null); } catch (err) { console.warn("Failed to save environment:", err); } }; const handleDelete = async (envId) => { try { await fetch(`/api/environments/${envId}`, { method: "DELETE" }); await fetchEnvs(); if (activeEnvId === envId) { onEnvChange?.(null); } } catch (err) { console.warn("Failed to delete environment:", err); } }; return (