import React, { useState, useEffect } from 'react'; import { useNavigate, useParams } from 'react-router-dom'; import { Button } from '@/components/ui/button'; import { ChevronRight } from 'lucide-react'; interface Emotion { id: number; slug: string; name: string; emoji: string; description?: string; } interface EmotionPickerProps { onSelect?: (emotion: Emotion) => void; } export default function EmotionPicker({ onSelect }: EmotionPickerProps) { const { slug } = useParams<{ slug: string }>(); const navigate = useNavigate(); const [emotions, setEmotions] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); // Fallback emotions (when API not available yet) const fallbackEmotions: Emotion[] = [ { id: 1, slug: 'happy', name: 'Glücklich', emoji: '😊' }, { id: 2, slug: 'love', name: 'Verliebt', emoji: '❤️' }, { id: 3, slug: 'excited', name: 'Aufgeregt', emoji: '🎉' }, { id: 4, slug: 'relaxed', name: 'Entspannt', emoji: '😌' }, { id: 5, slug: 'sad', name: 'Traurig', emoji: '😢' }, { id: 6, slug: 'surprised', name: 'Überrascht', emoji: '😲' }, ]; useEffect(() => { if (!slug) return; async function fetchEmotions() { try { setLoading(true); setError(null); // Try API first const response = await fetch(`/api/v1/events/${slug}/emotions`); if (response.ok) { const data = await response.json(); setEmotions(Array.isArray(data) ? data : fallbackEmotions); } else { // Fallback to predefined emotions console.warn('Emotions API not available, using fallback'); setEmotions(fallbackEmotions); } } catch (err) { console.error('Failed to fetch emotions:', err); setError('Emotions konnten nicht geladen werden'); setEmotions(fallbackEmotions); } finally { setLoading(false); } } fetchEmotions(); }, [slug]); const handleEmotionSelect = (emotion: Emotion) => { if (onSelect) { onSelect(emotion); } else { // Default: Navigate to tasks with emotion filter navigate(`/e/${slug}/tasks?emotion=${emotion.slug}`); } }; if (loading) { return (
Lade Emotionen...
); } if (error) { return (
{error}
); } return (

Wie fühlst du dich? (optional)

{emotions.map((emotion) => { // Localize name and description if they are JSON const localize = (value: string | object, defaultValue: string = ''): string => { if (typeof value === 'string' && value.startsWith('{')) { try { const data = JSON.parse(value as string); return data.de || data.en || defaultValue || ''; } catch { return value as string; } } return value as string; }; const localizedName = localize(emotion.name, emotion.name); const localizedDescription = localize(emotion.description || '', ''); return ( ); })}
{/* Skip option */}
); }