import React, { useState, useEffect } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { X, Sparkles, Calendar as CalendarIcon, Clock, Briefcase, Heart, Home, Smile, Tag, Trash2 } from 'lucide-react'; import { UserRole, CalendarEvent, EventCategory } from '../types'; import { Button } from './Button'; import { parseNaturalLanguageEvent } from '../services/aiService'; import { getSettings } from '../services/storageService'; import { v4 as uuidv4 } from 'uuid'; interface EventModalProps { isOpen: boolean; onClose: () => void; onSave: (event: CalendarEvent) => void; onDelete?: (id: string) => void; currentUserRole: UserRole; selectedDate?: Date; initialEvent?: CalendarEvent | null; } export const EventModal: React.FC = ({ isOpen, onClose, onSave, onDelete, currentUserRole, selectedDate, initialEvent }) => { const [activeTab, setActiveTab] = useState<'manual' | 'ai'>('manual'); const [aiPrompt, setAiPrompt] = useState(''); const [isProcessingAi, setIsProcessingAi] = useState(false); // Form State const [title, setTitle] = useState(''); const [startDate, setStartDate] = useState(''); const [startTime, setStartTime] = useState('09:00'); const [endTime, setEndTime] = useState('10:00'); const [description, setDescription] = useState(''); const [category, setCategory] = useState('general'); // Effect to load initial data for editing or reset for new useEffect(() => { if (isOpen) { if (initialEvent) { setTitle(initialEvent.title); setDescription(initialEvent.description || ''); setCategory(initialEvent.category); const start = new Date(initialEvent.startTime); const end = new Date(initialEvent.endTime); setStartDate(start.toISOString().split('T')[0]); setStartTime(start.toTimeString().slice(0, 5)); setEndTime(end.toTimeString().slice(0, 5)); setActiveTab('manual'); } else { // Reset setTitle(''); setDescription(''); setCategory('general'); setAiPrompt(''); if (selectedDate) { const year = selectedDate.getFullYear(); const month = String(selectedDate.getMonth() + 1).padStart(2, '0'); const day = String(selectedDate.getDate()).padStart(2, '0'); setStartDate(`${year}-${month}-${day}`); } else { setStartDate(new Date().toISOString().split('T')[0]); } setStartTime('09:00'); setEndTime('10:00'); setActiveTab('manual'); } } }, [isOpen, initialEvent, selectedDate]); const handleAiSubmit = async () => { if (!aiPrompt.trim()) return; setIsProcessingAi(true); const settings = getSettings(); const result = await parseNaturalLanguageEvent(aiPrompt, settings); setIsProcessingAi(false); if (result) { setTitle(result.title); setDescription(result.description || ''); const start = new Date(result.startTime); const end = new Date(result.endTime); setStartDate(start.toISOString().split('T')[0]); setStartTime(start.toTimeString().slice(0, 5)); setEndTime(end.toTimeString().slice(0, 5)); setCategory((result.category as EventCategory) || 'general'); setActiveTab('manual'); // Switch to manual to review } }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); const startDateTime = new Date(`${startDate}T${startTime}`); const endDateTime = new Date(`${startDate}T${endTime}`); const newEvent: CalendarEvent = { id: initialEvent ? initialEvent.id : uuidv4(), title, description, startTime: startDateTime.toISOString(), endTime: endDateTime.toISOString(), createdBy: initialEvent ? initialEvent.createdBy : currentUserRole, category }; onSave(newEvent); onClose(); }; const handleDelete = () => { if (initialEvent && onDelete) { onDelete(initialEvent.id); onClose(); } }; const categories: { id: EventCategory; label: string; icon: any; color: string }[] = [ { id: 'general', label: 'General', icon: Tag, color: 'bg-gray-800 text-gray-300 border-gray-600' }, { id: 'work', label: 'Work', icon: Briefcase, color: 'bg-orange-900/30 text-orange-400 border-orange-800' }, { id: 'chore', label: 'Chore', icon: Home, color: 'bg-green-900/30 text-green-400 border-green-800' }, { id: 'date', label: 'Date', icon: Heart, color: 'bg-red-900/30 text-red-400 border-red-800' }, { id: 'fun', label: 'Fun', icon: Smile, color: 'bg-yellow-900/30 text-yellow-400 border-yellow-800' }, { id: 'magic', label: 'Magic', icon: Sparkles, color: 'bg-purple-900/30 text-purple-400 border-purple-800' }, ]; return ( {isOpen && ( <>
{/* Header */}

{initialEvent ? 'Edit Event' : 'New Event'}

{/* Tabs (Only for New Events) */} {!initialEvent && (
)} {/* Content */}
{activeTab === 'ai' && !initialEvent ? (

Try saying...

"Dinner with Mom next Friday at 7pm"

"Weekend getaway to the beach July 12-14"