"use client"; import { Activity, AlertCircle, ArrowLeft, Clock, ExternalLink, FileText, Globe, MessageSquare, User, } from "lucide-react"; import Link from "next/link"; import { useParams, useRouter } from "next/navigation"; import { useSession } from "next-auth/react"; import { useEffect, useState } from "react"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { formatCategory } from "@/lib/format-enums"; import MessageViewer from "../../../../components/MessageViewer"; import SessionDetails from "../../../../components/SessionDetails"; import type { ChatSession } from "../../../../lib/types"; /** * Custom hook for managing session data fetching and state */ function useSessionData(id: string | undefined, authStatus: string) { const [session, setSession] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const router = useRouter(); useEffect(() => { if (authStatus === "unauthenticated") { router.push("/login"); return; } if (authStatus === "authenticated" && id) { const fetchSession = async () => { setLoading(true); 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 (authStatus === "authenticated" && !id) { setError("Session ID is missing."); setLoading(false); } }, [id, authStatus, router]); return { session, loading, error }; } /** * Component for rendering loading state */ function LoadingCard({ message }: { message: string }) { return (
{message}
); } /** * Component for rendering error state */ function ErrorCard({ error }: { error: string }) { return (

Error: {error}

); } /** * Component for rendering session not found state */ function SessionNotFoundCard() { return (

Session not found.

); } /** * Component for rendering session header with navigation and badges */ function SessionHeader({ session }: { session: ChatSession }) { return (

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)} )}
); } /** * Component for rendering session overview cards */ function SessionOverview({ session }: { session: ChatSession }) { return (

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"}

); } export default function SessionViewPage() { const params = useParams(); const { status } = useSession(); const id = params?.id as string; const { session, loading, error } = useSessionData(id, status); if (status === "loading") { return ; } if (status === "unauthenticated") { return ; } if (loading && status === "authenticated") { return ; } if (error) { return ; } if (!session) { return ; } return (
{/* Session Details */} {/* Messages */} {session.messages && session.messages.length > 0 && ( Conversation ({session.messages.length} messages) )} {/* Transcript URL */} {session.fullTranscriptUrl && ( Source Transcript )}
); }