import React, { useState, useEffect } from 'react'; import { useNavigate, useParams } from 'react-router-dom'; import { Button } from '@/components/ui/button'; import { ChevronRight } from 'lucide-react'; import { cn } from '@/lib/utils'; import { useTranslation } from '../i18n/useTranslation'; interface Emotion { id: number; slug: string; name: string; emoji: string; description?: string; } interface EmotionPickerProps { onSelect?: (emotion: Emotion) => void; variant?: 'standalone' | 'embedded'; title?: string; subtitle?: string; showSkip?: boolean; } export default function EmotionPicker({ onSelect, variant = 'standalone', title, subtitle, showSkip, }: EmotionPickerProps) { const { token } = useParams<{ token: string }>(); const eventKey = token ?? ''; const navigate = useNavigate(); const [emotions, setEmotions] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const { locale } = useTranslation(); // Fallback emotions (when API not available yet) const fallbackEmotions = React.useMemo( () => [ { 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 (!eventKey) return; async function fetchEmotions() { try { setLoading(true); setError(null); // Try API first const response = await fetch(`/api/v1/events/${encodeURIComponent(eventKey)}/emotions?locale=${encodeURIComponent(locale)}`, { headers: { Accept: 'application/json', 'X-Locale': locale, }, }); 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(); }, [eventKey, locale, fallbackEmotions]); const handleEmotionSelect = (emotion: Emotion) => { if (onSelect) { onSelect(emotion); } else { // Default: Navigate to tasks with emotion filter if (!eventKey) return; navigate(`/e/${encodeURIComponent(eventKey)}/tasks?emotion=${emotion.slug}`); } }; const headingTitle = title ?? 'Wie fühlst du dich?'; const headingSubtitle = subtitle ?? '(optional)'; const shouldShowSkip = showSkip ?? variant === 'standalone'; const content = (
{(variant === 'standalone' || title) && (

{headingTitle} {headingSubtitle && {headingSubtitle}}

{loading && Lade Emotionen…}
)}
{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 */} {shouldShowSkip && (
)}
); if (error) { return (
{error}
); } if (variant === 'embedded') { return content; } return
{content}
; }