'use client'; import { useState, useTransition, useEffect } from 'react'; import { createOrUpdateWeatherEntry } from '@/actions/weather'; import { Avatar } from '@/components/ui/Avatar'; import { Textarea } from '@/components/ui/Textarea'; import { Select } from '@/components/ui/Select'; import { WEATHER_EMOJIS, getEmojiEvolution } from '@/lib/weather-utils'; interface WeatherEntry { id: string; userId: string; performanceEmoji: string | null; moralEmoji: string | null; fluxEmoji: string | null; valueCreationEmoji: string | null; notes: string | null; user: { id: string; name: string | null; email: string; }; } type PreviousEntry = { performanceEmoji: string | null; moralEmoji: string | null; fluxEmoji: string | null; valueCreationEmoji: string | null; }; interface WeatherCardProps { sessionId: string; currentUserId: string; entry: WeatherEntry; canEdit: boolean; previousEntry?: PreviousEntry | null; } function EvolutionIndicator({ current, previous, }: { current: string | null; previous: string | null | undefined; }) { const direction = getEmojiEvolution(current, previous); if (direction === null) return null; if (direction === 'up') { return ↑; } if (direction === 'down') { return ↓; } return →; } export function WeatherCard({ sessionId, currentUserId, entry, canEdit, previousEntry }: WeatherCardProps) { const [isPending, startTransition] = useTransition(); const [notes, setNotes] = useState(entry.notes || ''); const [performanceEmoji, setPerformanceEmoji] = useState(entry.performanceEmoji || null); const [moralEmoji, setMoralEmoji] = useState(entry.moralEmoji || null); const [fluxEmoji, setFluxEmoji] = useState(entry.fluxEmoji || null); const [valueCreationEmoji, setValueCreationEmoji] = useState(entry.valueCreationEmoji || null); const isCurrentUser = entry.userId === currentUserId; const canEditThis = canEdit && isCurrentUser; // Sync local state with props when they change (e.g., from SSE refresh) useEffect(() => { // eslint-disable-next-line react-hooks/set-state-in-effect setNotes(entry.notes || ''); setPerformanceEmoji(entry.performanceEmoji || null); setMoralEmoji(entry.moralEmoji || null); setFluxEmoji(entry.fluxEmoji || null); setValueCreationEmoji(entry.valueCreationEmoji || null); }, [entry.notes, entry.performanceEmoji, entry.moralEmoji, entry.fluxEmoji, entry.valueCreationEmoji]); function handleEmojiChange(axis: 'performance' | 'moral' | 'flux' | 'valueCreation', emoji: string | null) { if (!canEditThis) return; // Calculate new values const newPerformanceEmoji = axis === 'performance' ? emoji : performanceEmoji; const newMoralEmoji = axis === 'moral' ? emoji : moralEmoji; const newFluxEmoji = axis === 'flux' ? emoji : fluxEmoji; const newValueCreationEmoji = axis === 'valueCreation' ? emoji : valueCreationEmoji; // Update local state immediately if (axis === 'performance') { setPerformanceEmoji(emoji); } else if (axis === 'moral') { setMoralEmoji(emoji); } else if (axis === 'flux') { setFluxEmoji(emoji); } else if (axis === 'valueCreation') { setValueCreationEmoji(emoji); } // Save to server with new values startTransition(async () => { await createOrUpdateWeatherEntry(sessionId, { performanceEmoji: newPerformanceEmoji, moralEmoji: newMoralEmoji, fluxEmoji: newFluxEmoji, valueCreationEmoji: newValueCreationEmoji, notes, }); }); } function handleNotesChange(newNotes: string) { if (!canEditThis) return; setNotes(newNotes); } function handleNotesBlur() { if (!canEditThis) return; startTransition(async () => { await createOrUpdateWeatherEntry(sessionId, { performanceEmoji, moralEmoji, fluxEmoji, valueCreationEmoji, notes, }); }); } // For current user without entry, we need to get user info from somewhere // For now, we'll use a placeholder - in real app, you'd pass user info as prop const user = entry.user; return (