diff --git a/frontend/src/components/ImageResults.tsx b/frontend/src/components/ImageResults.tsx index 1766df4..98faecb 100644 --- a/frontend/src/components/ImageResults.tsx +++ b/frontend/src/components/ImageResults.tsx @@ -1,13 +1,37 @@ -import { useState } from "react"; +import { useState, useEffect, useCallback } from "react"; import type { ImageResult } from "@/lib/api"; -import { X } from "lucide-react"; +import { X, ChevronLeft, ChevronRight } from "lucide-react"; interface ImageResultsProps { results: ImageResult[]; } export function ImageResults({ results }: ImageResultsProps) { - const [selected, setSelected] = useState(null); + const [selectedIndex, setSelectedIndex] = useState(null); + + const selected = selectedIndex !== null ? results[selectedIndex] : null; + + const goPrev = useCallback(() => { + setSelectedIndex((i) => (i !== null && i > 0 ? i - 1 : i)); + }, []); + + const goNext = useCallback(() => { + setSelectedIndex((i) => (i !== null && i < results.length - 1 ? i + 1 : i)); + }, [results.length]); + + const close = useCallback(() => setSelectedIndex(null), []); + + // Keyboard navigation + useEffect(() => { + if (selectedIndex === null) return; + const onKey = (e: KeyboardEvent) => { + if (e.key === "ArrowLeft") { e.preventDefault(); goPrev(); } + else if (e.key === "ArrowRight") { e.preventDefault(); goNext(); } + else if (e.key === "Escape") { close(); } + }; + window.addEventListener("keydown", onKey); + return () => window.removeEventListener("keydown", onKey); + }, [selectedIndex, goPrev, goNext, close]); if (results.length === 0) return null; @@ -18,7 +42,7 @@ export function ImageResults({ results }: ImageResultsProps) { {results.map((img, i) => ( + + {/* Next button */} + +
e.stopPropagation()} > @@ -68,6 +110,9 @@ export function ImageResults({ results }: ImageResultsProps) {

{selected.title}

Source: {selected.source} • Engine: {selected.engine} + + {selectedIndex + 1} / {results.length} +