Spaces:
Sleeping
Sleeping
File size: 1,278 Bytes
6cc8ae1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import { useState, useCallback } from 'react';
const HISTORY_KEY = 'cattle_classifier_history';
const MAX_HISTORY = 20;
export function usePredictionHistory() {
const [history, setHistory] = useState(() => {
try {
const stored = localStorage.getItem(HISTORY_KEY);
return stored ? JSON.parse(stored) : [];
} catch {
return [];
}
});
const addPrediction = useCallback((prediction, imagePreview) => {
const entry = {
id: Date.now(),
timestamp: new Date().toISOString(),
predictedBreed: prediction.predicted_breed,
confidence: prediction.confidence,
topK: prediction.top_k,
imagePreview: imagePreview?.substring(0, 200), // Truncate for storage
};
setHistory(prev => {
const updated = [entry, ...prev].slice(0, MAX_HISTORY);
try {
localStorage.setItem(HISTORY_KEY, JSON.stringify(updated));
} catch { /* Storage full */ }
return updated;
});
}, []);
const clearHistory = useCallback(() => {
setHistory([]);
localStorage.removeItem(HISTORY_KEY);
}, []);
return { history, addPrediction, clearHistory };
}
|