"use client" import { useState, useEffect } from "react" import { useRouter } from "next/navigation" import { Button } from "@/components/ui/button" import { Loader2, ArrowLeft, MapPin, Package, FolderKanban, RotateCcw, TrendingUp } from "lucide-react" import VideoPlayerSection from "@/components/video-player-section" import { SidebarNavigation } from "@/components/sidebar-navigation" import { type SessionContext, loadSession, loadVideoData, isSessionValid, clearSession } from "@/lib/api" import { getVideoFile, clearVideoFile } from "@/lib/video-storage" import { DetectionData, DetectionType } from "@/lib/types" const API_URL = process.env.NEXT_PUBLIC_API_URL || "http://127.0.0.1:8000/api/v1" export default function ResultsPage() { const router = useRouter() const [session, setSession] = useState(null) const [detectionData, setDetectionData] = useState(null) const [detectionType, setDetectionType] = useState("pothole-detection") const [videoId, setVideoId] = useState(null) const [videoFile, setVideoFile] = useState(null) const [isLoading, setIsLoading] = useState(true) const [error, setError] = useState(null) // Load session and video data on mount useEffect(() => { const storedSession = loadSession() const videoData = loadVideoData() if (!isSessionValid(storedSession) || !videoData) { router.replace("/new-analysis") return } setSession(storedSession) setVideoId(videoData.videoId) setDetectionType(videoData.detectionType as DetectionType) // Fetch detection results and video file const fetchResults = async () => { try { // Fetch detection data const response = await fetch(`${API_URL}/results/${videoData.videoId}`, { headers: { "ngrok-skip-browser-warning": "true" } }) if (!response.ok) { throw new Error(`Failed to load results: ${response.status}`) } const data = await response.json() setDetectionData(data as any) // Retrieve video file from IndexedDB const storedVideoFile = await getVideoFile(videoData.videoId) if (storedVideoFile) { setVideoFile(storedVideoFile) } } catch (err) { setError(err instanceof Error ? err.message : "Failed to load results") } finally { setIsLoading(false) } } fetchResults() }, [router]) const handleNewAnalysis = async () => { // Clear video from IndexedDB if (videoId) { try { await clearVideoFile(videoId) } catch (err) { console.error("Failed to clear video file:", err) } } clearSession() router.push("/new-analysis") } const handleBackToUpload = () => { router.push("/upload") } const getTitle = () => { if (detectionType === "pothole-detection") return "Pothole Detection Results" if (detectionType === "sign-board-detection") return "Signboard Detection Results" return "Pothole & Signboard Detection Results" } if (isLoading) { return (

Loading detection results...

) } if (error) { return (

{error}

) } return (
{/* Sidebar Navigation */} {/* Main Content */}
{/* Refined Header */}

{getTitle()}

View your AI-powered road analysis results

{/* Session Info Bar */} {session && (
Project {session.projectName}
Package {session.packageName}
Location {session.locationName}
)} {/* Video Player Section */} {detectionData && videoId && (
)}
) }