"use client"; import { useEffect, useState } from "react"; import { useParams, useRouter } from "next/navigation"; // Import useRouter import { useSession } from "next-auth/react"; // Import useSession import SessionDetails from "../../../../components/SessionDetails"; import TranscriptViewer from "../../../../components/TranscriptViewer"; import { ChatSession } from "../../../../lib/types"; import Link from "next/link"; interface SessionApiResponse { session: ChatSession; } 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()) as { error: string; }; throw new Error( errorData.error || `Failed to fetch session: ${response.statusText}` ); } const data = (await response.json()) as SessionApiResponse; 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}

Back to Sessions List
); } if (!session) { return (

Session not found.

Back to Sessions List
); } return (
Back to Sessions List

Session: {session.sessionId || session.id}

{session.transcriptContent && session.transcriptContent.trim() !== "" ? (
) : (

Transcript

No transcript content available for this session.

{session.fullTranscriptUrl && process.env.NODE_ENV !== "production" && ( View Source Transcript URL )}
)}
); }