|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, { useState, useCallback, useMemo } from 'react'; |
| 4 | +import { motion, Reorder, AnimatePresence } from 'motion/react'; |
| 5 | +import { GripVertical, Clock, MapPin, Plus, Trash2, Edit3, Save, RotateCcw } from 'lucide-react'; |
| 6 | + |
| 7 | +interface Session { |
| 8 | + id: string; |
| 9 | + title: string; |
| 10 | + time: string; |
| 11 | + duration: string; |
| 12 | + location: string; |
| 13 | + speaker: string; |
| 14 | + type: 'workshop' | 'keynote' | 'break' | 'networking'; |
| 15 | +} |
| 16 | + |
| 17 | +interface ScheduleBuilderProps { |
| 18 | + initialSessions?: Session[]; |
| 19 | + onSave?: (sessions: Session[]) => void; |
| 20 | + className?: string; |
| 21 | +} |
| 22 | + |
| 23 | +const DEFAULT_SESSIONS: Session[] = [ |
| 24 | + { id: '1', title: 'Opening Ceremony', time: '09:00 AM', duration: '30 min', location: 'Main Stage', speaker: 'Alice Johnson', type: 'keynote' }, |
| 25 | + { id: '2', title: 'Blockchain Fundamentals', time: '10:00 AM', duration: '60 min', location: 'Room A', speaker: 'Bob Smith', type: 'workshop' }, |
| 26 | + { id: '3', title: 'Networking Lunch', time: '12:00 PM', duration: '60 min', location: 'Cafeteria', speaker: '-', type: 'networking' }, |
| 27 | + { id: '4', title: 'Future of DeFi', time: '01:30 PM', duration: '45 min', location: 'Main Stage', speaker: 'Charlie Brown', type: 'workshop' }, |
| 28 | +]; |
| 29 | + |
| 30 | +const ScheduleBuilder: React.FC<ScheduleBuilderProps> = ({ |
| 31 | + initialSessions = DEFAULT_SESSIONS, |
| 32 | + onSave, |
| 33 | + className = '', |
| 34 | +}) => { |
| 35 | + const [items, setItems] = useState<Session[]>(initialSessions); |
| 36 | + const [isEditing, setIsEditing] = useState(false); |
| 37 | + const [isHovering, setIsHovering] = useState<string | null>(null); |
| 38 | + |
| 39 | + const handleReorder = (newOrder: Session[]) => { |
| 40 | + setItems(newOrder); |
| 41 | + }; |
| 42 | + |
| 43 | + const addSession = () => { |
| 44 | + const newSession: Session = { |
| 45 | + id: Math.random().toString(36).substr(2, 9), |
| 46 | + title: 'New Session', |
| 47 | + time: '12:00 PM', |
| 48 | + duration: '45 min', |
| 49 | + location: 'TBD', |
| 50 | + speaker: 'TBD', |
| 51 | + type: 'workshop', |
| 52 | + }; |
| 53 | + setItems([...items, newSession]); |
| 54 | + }; |
| 55 | + |
| 56 | + const removeSession = (id: string) => { |
| 57 | + setItems(items.filter(item => item.id !== id)); |
| 58 | + }; |
| 59 | + |
| 60 | + const saveChanges = () => { |
| 61 | + onSave?.(items); |
| 62 | + setIsEditing(false); |
| 63 | + }; |
| 64 | + |
| 65 | + const resetSchedule = () => { |
| 66 | + setItems(initialSessions); |
| 67 | + }; |
| 68 | + |
| 69 | + const getTypeColor = (type: Session['type']) => { |
| 70 | + switch (type) { |
| 71 | + case 'keynote': return 'bg-amber-100 text-amber-700 dark:bg-amber-900/30 dark:text-amber-400'; |
| 72 | + case 'workshop': return 'bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-400'; |
| 73 | + case 'break': return 'bg-emerald-100 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-400'; |
| 74 | + case 'networking': return 'bg-indigo-100 text-indigo-700 dark:bg-indigo-900/30 dark:text-indigo-400'; |
| 75 | + default: return 'bg-gray-100 text-gray-700 dark:bg-gray-800/30 dark:text-gray-400'; |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + return ( |
| 80 | + <div className={`max-w-4xl mx-auto w-full p-6 bg-white dark:bg-gray-900 rounded-3xl shadow-xl border border-gray-100 dark:border-gray-800 ${className}`}> |
| 81 | + {/* Header */} |
| 82 | + <div className="flex flex-col md:flex-row md:items-center justify-between gap-4 mb-8"> |
| 83 | + <div> |
| 84 | + <h2 className="text-2xl font-extrabold text-gray-900 dark:text-white flex items-center gap-2"> |
| 85 | + Schedule Builder |
| 86 | + <span className="px-2 py-1 bg-indigo-100 dark:bg-indigo-900/30 text-indigo-600 dark:text-indigo-400 text-xs font-bold rounded-lg uppercase tracking-wider">Editor</span> |
| 87 | + </h2> |
| 88 | + <p className="text-sm text-gray-500 dark:text-gray-400 mt-1">Drag and drop to reorder sessions or add new ones.</p> |
| 89 | + </div> |
| 90 | + |
| 91 | + <div className="flex items-center gap-2"> |
| 92 | + <motion.button |
| 93 | + whileHover={{ scale: 1.05 }} |
| 94 | + whileTap={{ scale: 0.95 }} |
| 95 | + onClick={resetSchedule} |
| 96 | + className="p-2.5 text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-white bg-gray-50 dark:bg-gray-800 rounded-xl transition-all" |
| 97 | + title="Reset to initial" |
| 98 | + > |
| 99 | + <RotateCcw className="w-5 h-5" /> |
| 100 | + </motion.button> |
| 101 | + |
| 102 | + <motion.button |
| 103 | + whileHover={{ scale: 1.05 }} |
| 104 | + whileTap={{ scale: 0.95 }} |
| 105 | + onClick={addSession} |
| 106 | + className="flex items-center gap-2 px-4 py-2.5 bg-indigo-50 text-indigo-600 dark:bg-indigo-900/10 dark:text-indigo-400 rounded-xl font-semibold border border-indigo-100 dark:border-indigo-800/50 hover:bg-indigo-100 dark:hover:bg-indigo-900/20 transition-all" |
| 107 | + > |
| 108 | + <Plus className="w-5 h-5" /> |
| 109 | + Add Session |
| 110 | + </motion.button> |
| 111 | + |
| 112 | + <motion.button |
| 113 | + whileHover={{ scale: 1.05 }} |
| 114 | + whileTap={{ scale: 0.95 }} |
| 115 | + onClick={saveChanges} |
| 116 | + className="flex items-center gap-2 px-6 py-2.5 bg-indigo-600 text-white rounded-xl font-semibold shadow-lg shadow-indigo-500/25 hover:bg-indigo-700 transition-all" |
| 117 | + > |
| 118 | + <Save className="w-5 h-5" /> |
| 119 | + Save Changes |
| 120 | + </motion.button> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + |
| 124 | + {/* Schedule List */} |
| 125 | + <div className="space-y-4"> |
| 126 | + <Reorder.Group axis="y" values={items} onReorder={handleReorder} className="space-y-3"> |
| 127 | + <AnimatePresence mode="popLayout"> |
| 128 | + {items.map((session) => ( |
| 129 | + <Reorder.Item |
| 130 | + key={session.id} |
| 131 | + value={session} |
| 132 | + initial={{ opacity: 0, scale: 0.95 }} |
| 133 | + animate={{ opacity: 1, scale: 1 }} |
| 134 | + exit={{ opacity: 0, scale: 0.9 }} |
| 135 | + transition={{ duration: 0.2 }} |
| 136 | + onHoverStart={() => setIsHovering(session.id)} |
| 137 | + onHoverEnd={() => setIsHovering(null)} |
| 138 | + className="relative group list-none" |
| 139 | + > |
| 140 | + <div className={`p-5 flex items-center gap-4 bg-gray-50 dark:bg-gray-800/50 border border-transparent rounded-2xl transition-all duration-300 ${isHovering === session.id ? 'border-indigo-300/40 dark:border-indigo-700/40 shadow-sm' : ''}`}> |
| 141 | + {/* Grip Handle */} |
| 142 | + <div className="cursor-grab active:cursor-grabbing text-gray-400 hover:text-indigo-500 transition-colors p-1"> |
| 143 | + <GripVertical className="w-6 h-6" /> |
| 144 | + </div> |
| 145 | + |
| 146 | + {/* Content Container */} |
| 147 | + <div className="flex-1 min-w-0 grid grid-cols-1 md:grid-cols-12 gap-4 items-center"> |
| 148 | + {/* Time & Session Type */} |
| 149 | + <div className="md:col-span-3"> |
| 150 | + <div className="flex items-center gap-2 text-gray-900 dark:text-white font-bold text-lg mb-1"> |
| 151 | + <Clock className="w-4 h-4 text-indigo-500" /> |
| 152 | + {session.time} |
| 153 | + </div> |
| 154 | + <span className={`px-2 py-0.5 rounded-lg text-[10px] font-bold uppercase tracking-wider ${getTypeColor(session.type)}`}> |
| 155 | + {session.type} |
| 156 | + </span> |
| 157 | + </div> |
| 158 | + |
| 159 | + {/* Title & Speaker */} |
| 160 | + <div className="md:col-span-5"> |
| 161 | + <h3 className="font-bold text-gray-900 dark:text-white truncate group-hover:text-indigo-500 transition-colors"> |
| 162 | + {session.title} |
| 163 | + </h3> |
| 164 | + <p className="text-xs text-gray-500 dark:text-gray-400 flex items-center gap-1.5 mt-0.5"> |
| 165 | + <span className="font-medium text-gray-900 dark:text-gray-300">{session.speaker}</span> |
| 166 | + <span>• {session.duration}</span> |
| 167 | + </p> |
| 168 | + </div> |
| 169 | + |
| 170 | + {/* Location */} |
| 171 | + <div className="md:col-span-4 flex items-center gap-2 text-sm text-gray-600 dark:text-gray-400 truncate"> |
| 172 | + <MapPin className="w-4 h-4 text-gray-400" /> |
| 173 | + <span className="truncate">{session.location}</span> |
| 174 | + </div> |
| 175 | + </div> |
| 176 | + |
| 177 | + {/* Actions */} |
| 178 | + <div className="flex items-center gap-1"> |
| 179 | + <button className="p-2 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 hover:bg-white dark:hover:bg-gray-700 rounded-lg transition-all" title="Edit Session"> |
| 180 | + <Edit3 className="w-4 h-4" /> |
| 181 | + </button> |
| 182 | + <button |
| 183 | + onClick={() => removeSession(session.id)} |
| 184 | + className="p-2 text-gray-400 hover:text-red-500 hover:bg-red-50 dark:hover:bg-red-900/20 rounded-lg transition-all" |
| 185 | + title="Delete Session" |
| 186 | + > |
| 187 | + <Trash2 className="w-4 h-4" /> |
| 188 | + </button> |
| 189 | + </div> |
| 190 | + </div> |
| 191 | + </Reorder.Item> |
| 192 | + ))} |
| 193 | + </AnimatePresence> |
| 194 | + </Reorder.Group> |
| 195 | + |
| 196 | + {items.length === 0 && ( |
| 197 | + <motion.div |
| 198 | + initial={{ opacity: 0 }} |
| 199 | + animate={{ opacity: 1 }} |
| 200 | + className="flex flex-col items-center justify-center py-16 px-4 text-center bg-gray-50/50 dark:bg-gray-800/30 rounded-3xl border border-dashed border-gray-200 dark:border-gray-700" |
| 201 | + > |
| 202 | + <div className="w-16 h-16 bg-gray-100 dark:bg-gray-800 rounded-full flex items-center justify-center mb-4"> |
| 203 | + <Plus className="w-8 h-8 text-gray-300" /> |
| 204 | + </div> |
| 205 | + <h3 className="text-lg font-bold text-gray-900 dark:text-white">No sessions yet</h3> |
| 206 | + <p className="text-sm text-gray-500 dark:text-gray-400 mt-1 max-w-xs">Start building your event schedule by adding your first session.</p> |
| 207 | + <button |
| 208 | + onClick={addSession} |
| 209 | + className="mt-6 px-6 py-2.5 bg-indigo-600 text-white rounded-xl font-semibold shadow-md active:scale-95 transition-all" |
| 210 | + > |
| 211 | + Add First Session |
| 212 | + </button> |
| 213 | + </motion.div> |
| 214 | + )} |
| 215 | + </div> |
| 216 | + |
| 217 | + {/* Footer Info */} |
| 218 | + <div className="mt-10 p-4 rounded-2xl bg-indigo-50/50 dark:bg-indigo-900/10 border border-indigo-100/50 dark:border-indigo-800/20 flex items-start gap-4"> |
| 219 | + <div className="p-2 bg-indigo-100 dark:bg-indigo-900/30 rounded-xl"> |
| 220 | + <GripVertical className="w-5 h-5 text-indigo-600 dark:text-indigo-400" /> |
| 221 | + </div> |
| 222 | + <div> |
| 223 | + <h4 className="text-sm font-bold text-indigo-900 dark:text-indigo-100">Pro-Tip for Organizers</h4> |
| 224 | + <p className="text-xs text-indigo-700/70 dark:text-indigo-300/60 mt-0.5">Use the handle on the left of each session to reorder items. Your schedule will automatically refresh for all attendees once saved.</p> |
| 225 | + </div> |
| 226 | + </div> |
| 227 | + </div> |
| 228 | + ); |
| 229 | +}; |
| 230 | + |
| 231 | +export { ScheduleBuilder }; |
| 232 | +export type { ScheduleBuilderProps }; |
0 commit comments