'use client' /* eslint-disable @next/next/no-img-element */ import { useState, useEffect } from 'react' import { Clock3, Trash2 } from 'lucide-react' interface PredictionRecord { id: string timestamp: string crop: string disease: string confidence: number imageUrl: string } interface PredictionHistoryProps { onSelectHistory: (record: PredictionRecord) => void } export default function PredictionHistory({ onSelectHistory }: PredictionHistoryProps) { const [history, setHistory] = useState([]) const [isOpen, setIsOpen] = useState(false) useEffect(() => { // Load history from localStorage (only in browser) if (typeof window === 'undefined') return try { const saved = localStorage.getItem('cropintel_history') if (saved) { const parsed = JSON.parse(saved) // Validate it's an array if (Array.isArray(parsed)) { setHistory(parsed) } } } catch (e) { console.error('Failed to load history:', e) // Clear corrupted data localStorage.removeItem('cropintel_history') } }, []) const clearHistory = () => { if (typeof window === 'undefined') return if (confirm('Are you sure you want to clear all prediction history?')) { localStorage.removeItem('cropintel_history') setHistory([]) } } if (history.length === 0) { return (

Saved field checks

{isOpen && (

No saved checks yet. Run a crop health check and the result will appear here.

)}
) } return (

Saved field checks ({history.length})

{isOpen && (
{history.map((record) => (
onSelectHistory(record)} className="cursor-pointer rounded-xl border border-field-soil/10 bg-white p-4 shadow-sm transition-all duration-200 hover:border-primary-300 hover:bg-primary-50/50" >
History { console.error('Image failed to load:', record.imageUrl) }} />
{Math.round(record.confidence)}
{record.disease} {new Date(record.timestamp).toLocaleDateString()}
{record.crop} {record.confidence.toFixed(1)}% match
))}
)}
) } export function savePredictionToHistory( crop: string, disease: string, confidence: number, imageUrl: string ) { // Only run in browser if (typeof window === 'undefined') return try { const record: PredictionRecord = { id: Date.now().toString(), timestamp: new Date().toISOString(), crop, disease, confidence, imageUrl } const saved = localStorage.getItem('cropintel_history') let history: PredictionRecord[] = [] if (saved) { try { const parsed = JSON.parse(saved) // Validate it's an array if (Array.isArray(parsed)) { history = parsed } else { // If corrupted, start fresh history = [] } } catch (e) { console.error('Failed to parse history:', e) // Clear corrupted data and start fresh localStorage.removeItem('cropintel_history') history = [] } } // Add new record at the beginning history.unshift(record) // Keep only last 50 records if (history.length > 50) { history = history.slice(0, 50) } localStorage.setItem('cropintel_history', JSON.stringify(history)) } catch (e) { console.error('Failed to save prediction to history:', e) } }