From ccd3855a9d6a95085152c2b4c290c43aa819932a Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Fri, 5 Sep 2025 10:03:38 +0000 Subject: [PATCH] OK display sessioni ed editing --- frontend/src/SessionTable.jsx | 170 ++++++++++++++++++---------------- 1 file changed, 91 insertions(+), 79 deletions(-) diff --git a/frontend/src/SessionTable.jsx b/frontend/src/SessionTable.jsx index 6941c2c..6ceecc8 100644 --- a/frontend/src/SessionTable.jsx +++ b/frontend/src/SessionTable.jsx @@ -1,15 +1,17 @@ // src/SessionTable.jsx import React, { useEffect, useState, useRef } from "react"; import { getSessionId } from "./useSessionId"; +import SessionEditor from "./SessionEditor"; export default function SessionTable({ userId, onSelectSession, onClosePanel }) { const [sessions, setSessions] = useState([]); const [loading, setLoading] = useState(false); - const [editingSessionId, setEditingSessionId] = useState(null); - const [editName, setEditName] = useState(""); const activeSessionId = getSessionId(); const wsRef = useRef(null); + const [showEditor, setShowEditor] = useState(false); + const [editorSession, setEditorSession] = useState(null); + const fetchSessions = async () => { if (!userId) return; setLoading(true); @@ -27,10 +29,8 @@ export default function SessionTable({ userId, onSelectSession, onClosePanel }) useEffect(() => { if (!userId) return; - // Caricamento iniziale fetchSessions(); - // Apri WebSocket const wsUrl = `${location.protocol === "https:" ? "wss" : "ws"}://${location.host}/v1/ws/sessions?user_id=${userId}`; const ws = new WebSocket(wsUrl); wsRef.current = ws; @@ -42,7 +42,6 @@ export default function SessionTable({ userId, onSelectSession, onClosePanel }) ws.onmessage = (event) => { try { const msg = JSON.parse(event.data); - // Possibili tipi di evento: full_list, created, updated, deleted if (msg.type === "full_list") { setSessions(msg.sessions); } else if (msg.type === "created") { @@ -73,84 +72,76 @@ export default function SessionTable({ userId, onSelectSession, onClosePanel }) }, [userId]); const startEditing = (session) => { - setEditingSessionId(session.session_id); - setEditName(session.session_name); + setEditorSession(session); + setShowEditor(true); }; - const saveName = async (sessionId) => { + const handleEditorConfirm = async ({ session_name, model_name }) => { try { - await fetch(`/v1/sessions/${sessionId}?user_id=${userId}`, { + await fetch(`/v1/sessions/${editorSession.session_id}?user_id=${userId}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ session_name: editName }), + body: JSON.stringify({ session_name, model_name }) }); - // Aggiornamento ottimistico setSessions((prev) => prev.map((s) => - s.session_id === sessionId ? { ...s, session_name: editName } : s + s.session_id === editorSession.session_id + ? { ...s, session_name, model_name } + : s ) ); - setEditingSessionId(null); + setShowEditor(false); + setEditorSession(null); } catch (err) { - console.error("Failed to rename session", err); + console.error("Failed to update session", err); } }; + const handleEditorCancel = () => { + setShowEditor(false); + setEditorSession(null); + }; + const deleteSession = async (sessionId) => { if (!window.confirm("Delete this session and its history?")) return; try { await fetch(`/v1/sessions/${sessionId}?user_id=${userId}`, { method: "DELETE" }); - // Non serve fetchSessions: il WS notificherà la cancellazione } catch (err) { console.error("Failed to delete session", err); } }; return ( -
- - - - - - - - - - - - {loading && } - {!loading && sessions.length === 0 && ( - - )} - {!loading && sessions.map((s) => ( - - - + + + + ))} + +
SessionCreated# mess
Loading…
No sessions found
- - - {editingSessionId === s.session_id ? ( - setEditName(e.target.value)} - onBlur={() => saveName(s.session_id)} - onKeyDown={(e) => { - if (e.key === "Enter") saveName(s.session_id); - if (e.key === "Escape") setEditingSessionId(null); - }} - autoFocus - /> - ) : ( - +
+ + + + + + + + + + + {loading && } + {!loading && sessions.length === 0 && ( + + )} + {!loading && sessions.map((s) => ( + + + - - - - - ))} - -
Session/ModelCreated/Mess
Loading…
No sessions found
+ + +
{ @@ -160,26 +151,47 @@ export default function SessionTable({ userId, onSelectSession, onClosePanel }) title="Click to load this session" > {s.session_name} - - )} -
{new Date(s.created_at).toLocaleString()} - {s.message_count} - - -
-
+ +
+ {s.model_name + ? s.model_name.length > 20 + ? `${s.model_name.slice(0, 20)}…` + : s.model_name + : "default"} +
+
+
{new Date(s.created_at).toLocaleString()}
+
+ # mess: {s.message_count} +
+
+ +
+
+ + {showEditor && ( + <> +
+ + + )} + ); } -