import React, { useState, useEffect } from 'react'; const FeedbackWeighting = ({ onComplete, onCancel }) => { const [feedbackWords, setFeedbackWords] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [submitting, setSubmitting] = useState(false); const [weights, setWeights] = useState({}); useEffect(() => { fetchQueuedFeedbackWords(); }, []); const fetchQueuedFeedbackWords = async () => { setLoading(true); setError(null); try { const response = await fetch('/api/v1/feedback/queued'); if (response.ok) { const data = await response.json(); const words = data.queued_words || []; setFeedbackWords(words); // Initialize weights state const initialWeights = {}; words.forEach(word => { initialWeights[word.word] = word.weight; }); setWeights(initialWeights); } else { throw new Error(`Failed to fetch feedback words: ${response.status}`); } } catch (err) { console.error('Error fetching feedback words:', err); setError('Failed to load feedback words. Please try again.'); // Fallback to mock data for development const mockWords = [ { key: 'feedback00', word: 'labyrinth', weight: 3 }, { key: 'feedback01', word: 'residue', weight: 3 }, { key: 'feedback02', word: 'tremor', weight: 3 }, { key: 'feedback03', word: 'effigy', weight: 3 }, { key: 'feedback04', word: 'quasar', weight: 3 }, { key: 'feedback05', word: 'gossamer', weight: 3 } ]; setFeedbackWords(mockWords); const initialWeights = {}; mockWords.forEach(word => { initialWeights[word.word] = word.weight; }); setWeights(initialWeights); } finally { setLoading(false); } }; const handleWeightChange = (word, newWeight) => { setWeights(prev => ({ ...prev, [word]: newWeight })); }; const handleSubmit = async () => { setSubmitting(true); setError(null); try { const response = await fetch('/api/v1/feedback/rate', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ ratings: weights }) }); if (response.ok) { const data = await response.json(); console.log('Feedback words rated successfully:', data); // Call onComplete callback if provided if (onComplete) { onComplete(data); } } else { const errorData = await response.json(); throw new Error(errorData.detail || `Failed to rate feedback words: ${response.status}`); } } catch (err) { console.error('Error rating feedback words:', err); setError(`Failed to submit ratings: ${err.message}`); } finally { setSubmitting(false); } }; if (loading) { return (
Loading feedback words...
); } return (

Rate Feedback Words

{error && (

{error}

)}
{feedbackWords.map((item, index) => (
{/* Increased height and flex column */}
{/* Pushes h3 to bottom */}

{item.word}

{/* Added margin-top for spacing */} handleWeightChange(item.word, parseInt(e.target.value))} className="w-full h-16 bg-gray-200 rounded-md appearance-none cursor-pointer blocky-slider" style={{ background: `linear-gradient(to right, #ef4444 0%, #f97316 16.67%, #eab308 33.33%, #22c55e 50%, #3b82f6 66.67%, #8b5cf6 83.33%, #a855f7 100%)` }} />
))}
); }; export default FeedbackWeighting;