import React, { useEffect, useState } from 'react'; import { useParams, Link, useNavigate } from 'react-router-dom'; import { PencilSquareIcon, TrashIcon, TagIcon, DocumentTextIcon } from '@heroicons/react/24/solid'; import { useDataContext } from '../../contexts/DataContext'; import ConfirmDialog from '../Shared/ConfirmDialog'; import NoteModal from './NoteModal'; import { Note } from '../../entities/Note'; const NoteDetails: React.FC = () => { const { id } = useParams<{ id: string }>(); const { notes, deleteNote, isLoading, isError } = useDataContext(); const [note, setNote] = useState(null); const [isNoteModalOpen, setIsNoteModalOpen] = useState(false); const [isConfirmDialogOpen, setIsConfirmDialogOpen] = useState(false); const [noteToDelete, setNoteToDelete] = useState(null); const navigate = useNavigate(); useEffect(() => { const foundNote = notes.find((n) => n.id === Number(id)); setNote(foundNote || null); }, [id, notes]); const handleDeleteNote = async () => { if (!noteToDelete) return; try { await deleteNote(noteToDelete.id!); navigate('/notes'); } catch (err) { console.error('Error deleting note:', err); } }; const handleSaveNote = (updatedNote: Note) => { setNote(updatedNote); setIsNoteModalOpen(false); }; const handleEditNote = () => { setIsNoteModalOpen(true); }; const handleOpenConfirmDialog = (note: Note) => { setNoteToDelete(note); setIsConfirmDialogOpen(true); }; if (isLoading) { return (
Loading note details...
); } if (isError || !note) { return (
{isError ? 'Error loading note details.' : 'Note not found.'}
); } return (
{/* Header Section with Title and Action Buttons */}

{note.title}

{/* Action Buttons */}
{/* Card with Tags and Metadata */}
{/* Note Tags */} {note.tags && note.tags.length > 0 && (
{note.tags.map((tag) => ( ))}
)} {/* Note Metadata */}

Created on: {new Date(note.created_at || '').toLocaleDateString()}

Last updated: {new Date(note.updated_at || '').toLocaleDateString()}

{/* Note Project */} {note.project && (

Project

{note.project.name}
)}
{/* Note Content */}

{note.content}

{/* NoteModal for editing */} {isNoteModalOpen && ( setIsNoteModalOpen(false)} onSave={handleSaveNote} note={note} /> )} {/* ConfirmDialog */} {isConfirmDialogOpen && noteToDelete && ( { setIsConfirmDialogOpen(false); setNoteToDelete(null); }} /> )}
); }; export default NoteDetails;