|
1 | 1 | import React, { useState, useEffect } from "react"; |
2 | 2 | import globalStyles from "../App.css"; |
3 | 3 | import styles from "./Notepad.css"; |
| 4 | +import { PushPinIcon } from "@phosphor-icons/react"; |
| 5 | + |
| 6 | +interface NoteState { |
| 7 | + id: string; |
| 8 | + title: string; |
| 9 | + content: string; |
| 10 | + pinned: boolean; |
| 11 | +} |
4 | 12 |
|
5 | 13 | export function Notepad() { |
6 | | - const [note, setNote] = useState(""); |
| 14 | + const [notes, setNotes] = useState<NoteState[]>([ |
| 15 | + { id: "1", title: "Note 1", content: "", pinned: false }, |
| 16 | + ]); |
| 17 | + const [activeNoteId, setActiveNoteId] = useState("1"); |
| 18 | + const [editingId, setEditingId] = useState<string>(null); |
| 19 | + const [tempTitle, setTempTitle] = useState(""); |
7 | 20 |
|
8 | | - // Load saved note on startup |
| 21 | + // Load from localStorage |
9 | 22 | useEffect(() => { |
10 | | - const savedNote = localStorage.getItem("notepad-note"); |
11 | | - if (savedNote) setNote(savedNote); |
| 23 | + const saved = localStorage.getItem("multi-notes"); |
| 24 | + if (saved) { |
| 25 | + const parsed = JSON.parse(saved); |
| 26 | + setNotes(parsed); |
| 27 | + if (parsed.length > 0) setActiveNoteId(parsed[0].id); |
| 28 | + } else { |
| 29 | + createNewNote(true); |
| 30 | + } |
12 | 31 | }, []); |
13 | 32 |
|
14 | | - // Save note to localStorage on every change |
| 33 | + // Save to localStorage |
15 | 34 | useEffect(() => { |
16 | | - localStorage.setItem("notepad-note", note); |
17 | | - }, [note]); |
| 35 | + localStorage.setItem("multi-notes", JSON.stringify(notes)); |
| 36 | + }, [notes]); |
| 37 | + |
| 38 | + const createNewNote = (isInitial = false) => { |
| 39 | + const id = Date.now().toString(); |
| 40 | + const nextNumber = isInitial || notes.length === 0 ? 1 : notes.length + 1; |
| 41 | + const newNote = { |
| 42 | + id, |
| 43 | + title: `Note ${nextNumber}`, |
| 44 | + content: "", |
| 45 | + pinned: false, |
| 46 | + }; |
| 47 | + setNotes((prev) => [...prev, newNote]); |
| 48 | + setActiveNoteId(id); |
| 49 | + }; |
| 50 | + |
| 51 | + const deleteNote = (id: string) => { |
| 52 | + const remaining = notes.filter((n) => n.id !== id); |
| 53 | + setNotes(remaining); |
| 54 | + if (remaining.length > 0) { |
| 55 | + setActiveNoteId(remaining[0].id); |
| 56 | + } else { |
| 57 | + createNewNote(true); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + const renameNote = (id: string, newTitle: string) => { |
| 62 | + setNotes((prev) => |
| 63 | + prev.map((n) => (n.id === id ? { ...n, title: newTitle } : n)), |
| 64 | + ); |
| 65 | + }; |
| 66 | + |
| 67 | + const updateContent = (id: string, newContent: string) => { |
| 68 | + setNotes((prev) => |
| 69 | + prev.map((n) => (n.id === id ? { ...n, content: newContent } : n)), |
| 70 | + ); |
| 71 | + }; |
| 72 | + |
| 73 | + const togglePin = (id: string) => { |
| 74 | + setNotes( |
| 75 | + (prev) => |
| 76 | + prev |
| 77 | + .map((n) => (n.id === id ? { ...n, pinned: !n.pinned } : n)) |
| 78 | + .sort((a, b) => (b.pinned ? 1 : 0) - (a.pinned ? 1 : 0)), // pinned notes first |
| 79 | + ); |
| 80 | + }; |
| 81 | + |
| 82 | + const startEditing = (id: string, currentTitle: string) => { |
| 83 | + setEditingId(id); |
| 84 | + setTempTitle(currentTitle); |
| 85 | + }; |
| 86 | + |
| 87 | + const finishEditing = () => { |
| 88 | + if (editingId) renameNote(editingId, tempTitle.trim() || "Untitled"); |
| 89 | + setEditingId(null); |
| 90 | + setTempTitle(""); |
| 91 | + }; |
| 92 | + |
| 93 | + const activeNote = notes.find((n) => n.id === activeNoteId); |
18 | 94 |
|
19 | 95 | return ( |
20 | 96 | <div className={[globalStyles.container, styles.body].join(" ")}> |
21 | | - <h3 className={styles.title}>My Notes</h3> |
22 | | - <textarea |
23 | | - className={[globalStyles.container, styles.textarea].join(" ")} |
24 | | - value={note} |
25 | | - onChange={(e) => setNote(e.target.value)} |
26 | | - placeholder="Type your notes here..." |
27 | | - /> |
| 97 | + {/* Tabs */} |
| 98 | + <div className={styles.tabs}> |
| 99 | + {notes.map((note) => ( |
| 100 | + <div |
| 101 | + key={note.id} |
| 102 | + className={`${styles.tab} ${ |
| 103 | + note.id === activeNoteId ? styles.activeTab : "" |
| 104 | + }`} |
| 105 | + onClick={() => setActiveNoteId(note.id)} |
| 106 | + > |
| 107 | + {editingId === note.id ? ( |
| 108 | + <input |
| 109 | + className={styles.renameInput} |
| 110 | + value={tempTitle} |
| 111 | + autoFocus |
| 112 | + onChange={(e) => setTempTitle(e.target.value)} |
| 113 | + onBlur={finishEditing} |
| 114 | + onKeyDown={(e) => e.key === "Enter" && finishEditing()} |
| 115 | + /> |
| 116 | + ) : ( |
| 117 | + <span |
| 118 | + onDoubleClick={() => startEditing(note.id, note.title)} |
| 119 | + title="Double-click to rename" |
| 120 | + > |
| 121 | + {note.title} |
| 122 | + </span> |
| 123 | + )} |
| 124 | + |
| 125 | + {/* Close button */} |
| 126 | + <button |
| 127 | + className={styles.closeBtn} |
| 128 | + onClick={(e) => { |
| 129 | + e.stopPropagation(); |
| 130 | + deleteNote(note.id); |
| 131 | + }} |
| 132 | + > |
| 133 | + × |
| 134 | + </button> |
| 135 | + </div> |
| 136 | + ))} |
| 137 | + <button className={styles.addBtn} onClick={() => createNewNote()}> |
| 138 | + + |
| 139 | + </button> |
| 140 | + </div> |
| 141 | + |
| 142 | + {/* Note editor */} |
| 143 | + {activeNote && ( |
| 144 | + <> |
| 145 | + <h3 className={styles.title}> |
| 146 | + {activeNote.title} |
| 147 | + |
| 148 | + {/* Pin button using Phosphor PinIcon */} |
| 149 | + <button |
| 150 | + className={styles.pinBtn} |
| 151 | + onClick={(e) => { |
| 152 | + e.stopPropagation(); |
| 153 | + togglePin(activeNote.id); |
| 154 | + }} |
| 155 | + title={activeNote.pinned ? "Unpin note" : "Pin note"} |
| 156 | + > |
| 157 | + <PushPinIcon |
| 158 | + size={16} |
| 159 | + weight={activeNote.pinned ? "fill" : "regular"} |
| 160 | + color={activeNote.pinned ? "#ffd700" : "#fff9"} |
| 161 | + /> |
| 162 | + </button> |
| 163 | + </h3> |
| 164 | + <textarea |
| 165 | + className={[globalStyles.container, styles.textarea].join(" ")} |
| 166 | + value={activeNote.content} |
| 167 | + onChange={(e) => updateContent(activeNote.id, e.target.value)} |
| 168 | + placeholder="Type your notes here..." |
| 169 | + /> |
| 170 | + </> |
| 171 | + )} |
28 | 172 | </div> |
29 | 173 | ); |
30 | 174 | } |
0 commit comments