"use client"; import { useEffect, useState } from "react"; import { useParams, useRouter } from "next/navigation"; import { useSession } from "next-auth/react"; import SessionDetails from "../../../../components/SessionDetails"; import MessageViewer from "../../../../components/MessageViewer"; import { ChatSession } from "../../../../lib/types"; import { formatCategory } from "@/lib/format-enums"; import Link from "next/link"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { ArrowLeft, MessageSquare, Clock, Globe, ExternalLink, User, AlertCircle, FileText, Activity, } from "lucide-react"; export default function SessionViewPage() { const params = useParams(); const router = useRouter(); // Initialize useRouter const { status } = useSession(); // Get session status, removed unused sessionData const id = params?.id as string; const [session, setSession] = useState(null); const [loading, setLoading] = useState(true); // This will now primarily be for data fetching const [error, setError] = useState(null); useEffect(() => { if (status === "unauthenticated") { router.push("/login"); return; } if (status === "authenticated" && id) { const fetchSession = async () => { setLoading(true); // Always set loading before fetch setError(null); try { const response = await fetch(`/api/dashboard/session/${id}`); if (!response.ok) { const errorData = await response.json(); throw new Error( errorData.error || `Failed to fetch session: ${response.statusText}` ); } const data = await response.json(); setSession(data.session); } catch (err) { setError( err instanceof Error ? err.message : "An unknown error occurred" ); setSession(null); } finally { setLoading(false); } }; fetchSession(); } else if (status === "authenticated" && !id) { setError("Session ID is missing."); setLoading(false); } }, [id, status, router]); // session removed from dependencies if (status === "loading") { return (
Loading session...
); } if (status === "unauthenticated") { return (
Redirecting to login...
); } if (loading && status === "authenticated") { return (
Loading session details...
); } if (error) { return (

Error: {error}

); } if (!session) { return (

Session not found.

); } return (
{/* Header */}

Session Details

ID {(session.sessionId || session.id).slice(0, 8)}...
{session.category && ( {formatCategory(session.category)} )} {session.language && ( {session.language.toUpperCase()} )} {session.sentiment && ( {session.sentiment.charAt(0).toUpperCase() + session.sentiment.slice(1)} )}
{/* Session Overview */}

Start Time

{new Date(session.startTime).toLocaleString()}

Messages

{session.messages?.length || 0}

User ID

{session.userId || "N/A"}

Duration

{session.endTime && session.startTime ? `${Math.round( (new Date(session.endTime).getTime() - new Date(session.startTime).getTime()) / 60000 )} min` : "N/A"}

{/* Session Details */} {/* Messages */} {session.messages && session.messages.length > 0 && ( Conversation ({session.messages.length} messages) )} {/* Transcript URL */} {session.fullTranscriptUrl && ( Source Transcript )}
); }