"use client" import { useState, useEffect } from "react" import { useRouter, useParams } from "next/navigation" import { Button } from "@/components/ui/button" import { Loader2, TrendingUp } from "lucide-react" import VideoPlayerSection from "@/components/video-player-section" import { SidebarNavigation } from "@/components/sidebar-navigation" import { PageHeader } from "@/components/page-header" import { type SessionContext, loadSession, 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 export default function VideoResultsPage() { const router = useRouter() const { videoId } = useParams() as { videoId: string } const [session, setSession] = useState(null) const [detectionData, setDetectionData] = useState(null) const [detectionType, setDetectionType] = useState("pothole-detection") const [videoFile, setVideoFile] = useState(null) const [isLoading, setIsLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { const storedSession = loadSession() setSession(storedSession) const fetchResults = async () => { try { // Fetch detection data from backend const response = await fetch(`${API_URL}/results/${videoId}`, { headers: { "ngrok-skip-browser-warning": "true" } }) if (!response.ok) { if (response.status === 404) { throw new Error("Results not found for this video.") } throw new Error(`Failed to load results: ${response.status}`) } const data = await response.json() setDetectionData(data as any) // Try to infer detection type from results if possible if (data.summary?.unique_signboards !== undefined && data.summary?.unique_signboards > 0) { setDetectionType("sign-board-detection") } else if (data.summary?.unique_potholes !== undefined && data.summary?.unique_potholes > 0) { setDetectionType("pothole-detection") } // Retrieve video file from IndexedDB const storedVideoFile = await getVideoFile(videoId) if (storedVideoFile) { setVideoFile(storedVideoFile) } } catch (err) { setError(err instanceof Error ? err.message : "Failed to load results") } finally { setIsLoading(false) } } if (videoId) { fetchResults() } }, [videoId]) const handleNewAnalysis = async () => { if (videoId) { try { await clearVideoFile(videoId) } catch (err) { console.error("Failed to clear video file:", err) } } clearSession() router.push("/new-analysis") } 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 (
{session && (
Project {session.projectName}
Package {session.packageName}
Location {session.locationName}
)} {detectionData && ( )}
) }