|
| 1 | +import { useState } from "react"; |
| 2 | +import { CheckCircle, Circle, Plus, Trash2, ListTodo } from "lucide-react"; |
| 3 | +import { usePopup } from "../context/PopupContext"; |
| 4 | +import { useAuth } from "../store/auth"; |
| 5 | +import { motion, AnimatePresence } from "framer-motion"; |
| 6 | + |
| 7 | +export default function TodoList() { |
| 8 | + const { showTodo, closeAll, todos, addTodo, toggleTodoItem, deleteTodo } = usePopup(); |
| 9 | + const { isLoggedIn } = useAuth(); |
| 10 | + const [newTodo, setNewTodo] = useState(""); |
| 11 | + const [open, setOpen] = useState(false); // widget toggle state |
| 12 | + |
| 13 | + const handleAdd = () => { |
| 14 | + if (!isLoggedIn) return; |
| 15 | + if (newTodo.trim()) { |
| 16 | + addTodo(newTodo); |
| 17 | + setNewTodo(""); |
| 18 | + } |
| 19 | + }; |
| 20 | + |
| 21 | + // Ensure todos is always an array |
| 22 | + const todoList = Array.isArray(todos) ? todos : []; |
| 23 | + |
| 24 | + return ( |
| 25 | + <> |
| 26 | + {/* Floating Toggle Button */} |
| 27 | + <motion.button |
| 28 | + onClick={() => setOpen((prev) => !prev)} |
| 29 | + whileHover={{ scale: 1.1 }} |
| 30 | + whileTap={{ scale: 0.9, rotate: 15 }} |
| 31 | + animate={{ y: [0, -4, 0] }} |
| 32 | + transition={{ repeat: Infinity, duration: 3, ease: "easeInOut" }} |
| 33 | + className="fixed bottom-4 right-4 z-50 p-4 rounded-full shadow-lg bg-primary text-white hover:bg-primary-dark transition" |
| 34 | + > |
| 35 | + <ListTodo size={24} /> |
| 36 | + </motion.button> |
| 37 | + |
| 38 | + {/* Todo Widget */} |
| 39 | + <AnimatePresence> |
| 40 | + {open && showTodo && ( |
| 41 | + <motion.div |
| 42 | + key="todo-widget" |
| 43 | + initial={{ opacity: 0, scale: 0.8, y: 40 }} |
| 44 | + animate={{ opacity: 1, scale: 1, y: 0 }} |
| 45 | + exit={{ opacity: 0, scale: 0.8, y: 40 }} |
| 46 | + transition={{ duration: 0.35, ease: "easeOut" }} |
| 47 | + className="fixed bottom-20 right-4 z-50 w-80 max-w-full" |
| 48 | + > |
| 49 | + <motion.div |
| 50 | + layout |
| 51 | + className="p-4 rounded-2xl shadow-xl shadow-black/30 bg-white dark:bg-dark-bg-primary transition-colors" |
| 52 | + > |
| 53 | + {/* Header */} |
| 54 | + <motion.h2 |
| 55 | + initial={{ opacity: 0, x: 20 }} |
| 56 | + animate={{ opacity: 1, x: 0 }} |
| 57 | + transition={{ delay: 0.1, duration: 0.3 }} |
| 58 | + className="text-xl font-bold mb-4 flex justify-between items-center text-black dark:text-dark-text-primary" |
| 59 | + > |
| 60 | + Todo List |
| 61 | + <span className="text-sm text-gray-600 dark:text-dark-text-secondary flex items-center gap-2"> |
| 62 | + <CheckCircle className="text-success" />{" "} |
| 63 | + {todoList.filter((t) => t.completed).length} /{" "} |
| 64 | + {todoList.length} |
| 65 | + </span> |
| 66 | + </motion.h2> |
| 67 | + |
| 68 | + {!isLoggedIn && ( |
| 69 | + <p className="text-sm text-red-500 mb-2"> |
| 70 | + Please log in to manage your todos. |
| 71 | + </p> |
| 72 | + )} |
| 73 | + |
| 74 | + {/* Todos */} |
| 75 | + {todoList.length === 0 ? ( |
| 76 | + <motion.p |
| 77 | + initial={{ opacity: 0 }} |
| 78 | + animate={{ opacity: 1 }} |
| 79 | + transition={{ delay: 0.2 }} |
| 80 | + className="text-sm text-gray-500 dark:text-dark-text-secondary text-center py-4" |
| 81 | + > |
| 82 | + No tasks available. |
| 83 | + </motion.p> |
| 84 | + ) : ( |
| 85 | + <ul className="space-y-2 max-h-64 overflow-y-auto"> |
| 86 | + <AnimatePresence> |
| 87 | + {todoList.map((todo, idx) => ( |
| 88 | + <motion.li |
| 89 | + key={todo._id || todo.id} |
| 90 | + initial={{ opacity: 0, x: 30 }} |
| 91 | + animate={{ opacity: 1, x: 0 }} |
| 92 | + exit={{ opacity: 0, x: -30 }} |
| 93 | + transition={{ |
| 94 | + duration: 0.25, |
| 95 | + delay: idx * 0.05, |
| 96 | + type: "spring", |
| 97 | + stiffness: 120, |
| 98 | + }} |
| 99 | + layout |
| 100 | + className="flex items-center gap-2 p-2 border rounded border-gray-300 dark:border-dark-border" |
| 101 | + > |
| 102 | + <button |
| 103 | + disabled={!isLoggedIn} |
| 104 | + onClick={() => |
| 105 | + toggleTodoItem(todo._id || todo.id) |
| 106 | + } |
| 107 | + > |
| 108 | + {todo.completed ? ( |
| 109 | + <CheckCircle className="text-success" /> |
| 110 | + ) : ( |
| 111 | + <Circle className="text-gray-500 dark:text-dark-text-secondary" /> |
| 112 | + )} |
| 113 | + </button> |
| 114 | + <span |
| 115 | + className={`${ |
| 116 | + todo.completed |
| 117 | + ? "line-through text-gray-500 dark:text-dark-text-secondary" |
| 118 | + : "text-black dark:text-dark-text-primary" |
| 119 | + } flex-1`} |
| 120 | + > |
| 121 | + {todo.text} |
| 122 | + </span> |
| 123 | + <button |
| 124 | + disabled={!isLoggedIn} |
| 125 | + onClick={() => deleteTodo(todo._id || todo.id)} |
| 126 | + className="p-1 text-red-500 hover:text-red-700" |
| 127 | + > |
| 128 | + <Trash2 size={16} /> |
| 129 | + </button> |
| 130 | + </motion.li> |
| 131 | + ))} |
| 132 | + </AnimatePresence> |
| 133 | + </ul> |
| 134 | + )} |
| 135 | + |
| 136 | + {/* Input + Add Button */} |
| 137 | + <motion.div |
| 138 | + initial={{ opacity: 0, y: 10 }} |
| 139 | + animate={{ opacity: 1, y: 0 }} |
| 140 | + transition={{ delay: 0.2 }} |
| 141 | + className="flex gap-2 mt-4" |
| 142 | + > |
| 143 | + <input |
| 144 | + type="text" |
| 145 | + value={newTodo} |
| 146 | + onChange={(e) => setNewTodo(e.target.value)} |
| 147 | + onKeyDown={(e) => { |
| 148 | + if (e.key === "Enter") handleAdd(); |
| 149 | + }} |
| 150 | + placeholder={isLoggedIn ? "New task..." : "Login to add tasks"} |
| 151 | + disabled={!isLoggedIn} |
| 152 | + className="flex-1 p-2 border rounded border-gray-300 dark:border-dark-border bg-gray-50 dark:bg-dark-bg-secondary text-black dark:text-dark-text-primary" |
| 153 | + /> |
| 154 | + |
| 155 | + <motion.button |
| 156 | + whileTap={{ scale: 0.9 }} |
| 157 | + onClick={handleAdd} |
| 158 | + disabled={!isLoggedIn} |
| 159 | + className={`p-2 rounded flex items-center ${ |
| 160 | + isLoggedIn |
| 161 | + ? "bg-primary text-white hover:bg-primary-dark" |
| 162 | + : "bg-gray-300 text-gray-600 cursor-not-allowed" |
| 163 | + }`} |
| 164 | + > |
| 165 | + <Plus size={18} /> |
| 166 | + </motion.button> |
| 167 | + </motion.div> |
| 168 | + |
| 169 | + {/* Close button */} |
| 170 | + <motion.div |
| 171 | + initial={{ opacity: 0 }} |
| 172 | + animate={{ opacity: 1 }} |
| 173 | + transition={{ delay: 0.25 }} |
| 174 | + className="flex justify-end mt-2" |
| 175 | + > |
| 176 | + <button |
| 177 | + onClick={() => setOpen(false)} |
| 178 | + className="text-sm text-gray-500 dark:text-dark-text-secondary hover:underline" |
| 179 | + > |
| 180 | + Close |
| 181 | + </button> |
| 182 | + </motion.div> |
| 183 | + </motion.div> |
| 184 | + </motion.div> |
| 185 | + )} |
| 186 | + </AnimatePresence> |
| 187 | + </> |
| 188 | + ); |
| 189 | +} |
0 commit comments