'use client'; import { forwardRef, useState, useTransition, useRef, ReactNode } from 'react'; import type { WeeklyCheckInCategory } from '@prisma/client'; import { createWeeklyCheckInItem } from '@/actions/weekly-checkin'; import { WEEKLY_CHECK_IN_BY_CATEGORY, EMOTION_BY_TYPE } from '@/lib/types'; import { IconPlus, InlineAddItem } from '@/components/ui'; import { Select } from '@/components/ui/Select'; interface WeeklyCheckInSectionProps { category: WeeklyCheckInCategory; sessionId: string; isDraggingOver: boolean; children: ReactNode; } export const WeeklyCheckInSection = forwardRef( ({ category, sessionId, isDraggingOver, children, ...props }, ref) => { const [isAdding, setIsAdding] = useState(false); const [newContent, setNewContent] = useState(''); const [newEmotion, setNewEmotion] = useState<'NONE'>('NONE'); const [isPending, startTransition] = useTransition(); const isSubmittingRef = useRef(false); const config = WEEKLY_CHECK_IN_BY_CATEGORY[category]; async function handleAdd() { if (isSubmittingRef.current || !newContent.trim()) { setIsAdding(false); return; } isSubmittingRef.current = true; startTransition(async () => { await createWeeklyCheckInItem(sessionId, { content: newContent.trim(), category, emotion: newEmotion, }); setNewContent(''); setNewEmotion('NONE'); setIsAdding(false); isSubmittingRef.current = false; }); } return (
{/* Header */}
{config.icon}

{config.title}

{config.description}

{/* Items */}
{children} {/* Add Form */} {isAdding && ( { setIsAdding(false); setNewContent(''); setNewEmotion('NONE'); }} isPending={isPending} placeholder={`Décrivez ${config.title.toLowerCase()}...`} extra={