// frontend/src/shell/NewSessionDialog.tsx import { useState } from "react"; import { useQuery } from "@tanstack/react-query"; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "../components/ui/dialog"; import { Button } from "../components/ui/button"; import { createSession } from "../api/sessions"; import { listWorkspaces } from "../api/workspaces"; import { listModels } from "../api/models"; interface Props { onCreated: (id: string) => void; } export function NewSessionDialog({ onCreated }: Props) { const [open, setOpen] = useState(false); const [question, setQuestion] = useState(""); const [workspace, setWorkspace] = useState(""); const [model, setModel] = useState(""); const [thinking, setThinking] = useState(""); const [provider, setProvider] = useState(""); const [error, setError] = useState(null); const [busy, setBusy] = useState(false); const { data: workspaces = [] } = useQuery({ queryKey: ["workspaces"], queryFn: listWorkspaces, enabled: open, }); const { data: modelsData } = useQuery({ queryKey: ["models"], queryFn: listModels, enabled: open, }); // Model entries may be plain strings or objects ({ id, provider, ... }); // normalize to { value, label } so the dropdown renders either shape. const modelList: { value: string; label: string }[] = Array.isArray( modelsData?.models, ) ? modelsData.models.map((m) => { if (typeof m === "string") return { value: m, label: m }; const o = m as { id?: string; provider?: string }; const value = o.id ?? ""; const label = o.provider ? `${o.provider}/${value}` : value; return { value, label }; }) : []; // When workspace list loads, pre-select the first one if nothing chosen yet const effectiveWorkspace = workspace || (workspaces.length > 0 ? workspaces[0].name : "default"); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setError(null); if (!question.trim()) { setError("La domanda non può essere vuota."); return; } setBusy(true); try { const { id } = await createSession({ workspace: effectiveWorkspace, question: question.trim(), ...(model ? { model } : {}), ...(thinking ? { thinking } : {}), ...(provider ? { provider } : {}), }); setOpen(false); setQuestion(""); setModel(""); setThinking(""); setProvider(""); onCreated(id); } catch (err) { setError(err instanceof Error ? err.message : "Errore nella creazione della sessione."); } finally { setBusy(false); } } return ( Nuova sessione