import React, { useState, useCallback, useEffect } from 'react' import { useScale } from '../../context/ScaleContext' export interface Note { id: number text: string } interface NotesProps { /** Callback to get notes. If not provided, uses localStorage. */ onGetNotes?: () => Note[] | Promise /** Callback to save notes. If not provided, uses localStorage. */ onSaveNotes?: (notes: Note[]) => void | Promise /** Storage key for localStorage (default: 'mc-inv-notes') */ storageKey?: string className?: string style?: React.CSSProperties } const DEFAULT_STORAGE_KEY = 'mc-inv-notes' function loadNotesFromStorage(key: string): Note[] { try { const raw = localStorage.getItem(key) return raw ? JSON.parse(raw) : [] } catch { return [] } } function saveNotesToStorage(key: string, notes: Note[]) { try { localStorage.setItem(key, JSON.stringify(notes)) } catch {} } export function Notes({ onGetNotes, onSaveNotes, storageKey = DEFAULT_STORAGE_KEY, className, style, }: NotesProps) { const { scale } = useScale() const [notes, setNotes] = useState([]) const [draft, setDraft] = useState('') const [loading, setLoading] = useState(true) const [showInput, setShowInput] = useState(false) // Load notes on mount useEffect(() => { let cancelled = false setLoading(true) const loadNotes = async () => { if (onGetNotes) { try { const loaded = await onGetNotes() if (!cancelled) { setNotes(loaded) setLoading(false) } } catch (err) { console.error('Failed to load notes:', err) if (!cancelled) { setNotes([]) setLoading(false) } } } else { // Use localStorage const loaded = loadNotesFromStorage(storageKey) if (!cancelled) { setNotes(loaded) setLoading(false) } } } loadNotes() return () => { cancelled = true } }, [onGetNotes, storageKey]) const saveNotes = useCallback(async (newNotes: Note[]) => { setNotes(newNotes) if (onSaveNotes) { try { await onSaveNotes(newNotes) } catch (err) { console.error('Failed to save notes:', err) } } else { // Use localStorage saveNotesToStorage(storageKey, newNotes) } }, [onSaveNotes, storageKey]) const addNote = useCallback(() => { const text = draft.trim() if (!text) return const next = [...notes, { id: Date.now(), text }] saveNotes(next) setDraft('') }, [draft, notes, saveNotes]) const handleAddButtonClick = useCallback(() => { if (!showInput) { setShowInput(true) } else { addNote() } }, [showInput, addNote]) const removeNote = useCallback((id: number) => { const next = notes.filter((n) => n.id !== id) saveNotes(next) }, [notes, saveNotes]) const fs = Math.max(9, Math.round(7 * scale)) return (
{/*
Current Tasks
*/} {/* Task list */}
{loading ? (
Loading...
) : notes.length === 0 ? (
{/* No tasks yet */}
) : ( notes.map((note) => (
{note.text}
)) )}
{/* Add task input */}
{showInput && (