"use client"; import React from "react"; function MainComponent() { const [notes, setNotes] = useState([]); const [isCreating, setIsCreating] = useState(false); const [selectedColor, setSelectedColor] = useState("#FFE4B5"); const [isDragging, setIsDragging] = useState(false); const [deleteConfirm, setDeleteConfirm] = useState(null); const [editingNote, setEditingNote] = useState(null); const [feedback, setFeedback] = useState(null); const colors = ["#ffeb3b", "#ff80ab", "#90caf9", "#a5d6a7", "#ce93d8"]; useEffect(() => { try { const savedNotes = localStorage.getItem("stickyNotes"); if (savedNotes) { setNotes(JSON.parse(savedNotes)); } } catch (error) { console.error("Error loading notes:", error); } }, []); useEffect(() => { try { localStorage.setItem("stickyNotes", JSON.stringify(notes)); } catch (error) { console.error("Error saving notes:", error); } }, [notes]); const downloadAllNotes = () => { try { const date = new Date().toISOString().split("T")[0]; const filename = `sticky-notes-backup-${date}.json`; const data = JSON.stringify(notes, null, 2); const blob = new Blob([data], { type: "application/json" }); const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = filename; document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); setFeedback({ type: "success", message: "Notes downloaded successfully!", }); } catch (error) { console.error("Error downloading notes:", error); setFeedback({ type: "error", message: "Failed to download notes" }); } }; const downloadSingleNote = (note) => { try { const title = note.content.split("\n")[0].slice(0, 30).trim() || "untitled"; const filename = `${title}-${new Date().toISOString().split("T")[0]}.txt`; const content = `${note.content}\n\nCreated: ${new Date( note.lastModified ).toLocaleString()}`; const blob = new Blob([content], { type: "text/plain" }); const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = filename; document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); } catch (error) { console.error("Error downloading note:", error); setFeedback({ type: "error", message: "Failed to download note" }); } }; const handleImport = async (event) => { try { const file = event.target.files[0]; if (!file) return; const reader = new FileReader(); reader.onload = (e) => { try { const imported = JSON.parse(e.target.result); if (!Array.isArray(imported)) { throw new Error("Invalid file format"); } const validNotes = imported.filter( (note) => note.id && note.content !== undefined && note.color && note.position && note.size ); if (validNotes.length === 0) { throw new Error("No valid notes found in file"); } setNotes((prevNotes) => [...prevNotes, ...validNotes]); setFeedback({ type: "success", message: `Imported ${validNotes.length} notes successfully!`, }); } catch (error) { console.error("Error parsing import file:", error); setFeedback({ type: "error", message: "Invalid file format" }); } }; reader.readAsText(file); } catch (error) { console.error("Error importing notes:", error); setFeedback({ type: "error", message: "Failed to import notes" }); } }; const addNote = () => { const newNote = { id: Date.now(), content: "", color: selectedColor, position: { x: Math.random() * 100, y: Math.random() * 100 }, size: { width: 250, height: 250 }, lastModified: new Date().toISOString(), }; setNotes([...notes, newNote]); setIsCreating(false); }; const updateNote = (id, updates) => { setNotes( notes.map((note) => note.id === id ? { ...note, ...updates, lastModified: new Date().toISOString() } : note ) ); }; const deleteNote = (id) => { setNotes(notes.filter((note) => note.id !== id)); }; const handleDragStart = (e, note) => { const startPos = { x: e.clientX - note.position.x, y: e.clientY - note.position.y, }; setIsDragging(true); const handleDrag = (moveEvent) => { updateNote(note.id, { position: { x: moveEvent.clientX - startPos.x, y: moveEvent.clientY - startPos.y, }, }); }; const handleDragEnd = () => { setIsDragging(false); document.removeEventListener("mousemove", handleDrag); document.removeEventListener("mouseup", handleDragEnd); document.removeEventListener("touchmove", handleDrag); document.removeEventListener("touchend", handleDragEnd); }; document.addEventListener("mousemove", handleDrag); document.addEventListener("mouseup", handleDragEnd); document.addEventListener("touchmove", handleDrag); document.addEventListener("touchend", handleDragEnd); }; return (
{feedback.message}
Click the + button in the bottom right to create your first note
This action cannot be undone.