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 ConfirmDialog from '../Shared/ConfirmDialog'; import NoteModal from './NoteModal'; import MarkdownRenderer from '../Shared/MarkdownRenderer'; import { Note } from '../../entities/Note'; import { fetchNotes, deleteNote as apiDeleteNote, updateNote as apiUpdateNote, } from '../../utils/notesService'; const NoteDetails: React.FC = () => { const { id } = useParams<{ id: string }>(); const [note, setNote] = useState(null); const [isNoteModalOpen, setIsNoteModalOpen] = useState(false); const [isConfirmDialogOpen, setIsConfirmDialogOpen] = useState(false); const [noteToDelete, setNoteToDelete] = useState(null); const [isLoading, setIsLoading] = useState(true); const [isError, setIsError] = useState(false); const navigate = useNavigate(); // Dispatch global modal events useEffect(() => { const fetchNote = async () => { try { setIsLoading(true); const notes = await fetchNotes(); const foundNote = notes.find((n: Note) => n.id === Number(id)); setNote(foundNote || null); if (!foundNote) { setIsError(true); } } catch (err) { setIsError(true); console.error('Error fetching note:', err); } finally { setIsLoading(false); } }; fetchNote(); }, [id]); const handleDeleteNote = async () => { if (!noteToDelete) return; try { await apiDeleteNote(noteToDelete.id!); navigate('/notes'); } catch (err) { console.error('Error deleting note:', err); } }; const handleSaveNote = async (updatedNote: Note) => { try { if (updatedNote.id !== undefined) { const savedNote = await apiUpdateNote( updatedNote.id, updatedNote ); setNote(savedNote); } else { console.error('Error: Note ID is undefined.'); } } catch (err) { console.error('Error saving note:', err); } 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 */}
{/* Tags and Project */} {(note.tags && note.tags.length > 0) || (note.Tags && note.Tags.length > 0) || note.project || note.Project ? (
{/* Note Tags */} {((note.tags && note.tags.length > 0) || (note.Tags && note.Tags.length > 0)) && (
Tags:
{(note.tags || note.Tags || []).map( (tag) => ( ) )}
)} {/* Note Project */} {(note.project || note.Project) && (
0) || (note.Tags && note.Tags.length > 0) ? 'mt-4' : '' } >

Project

{(note.project || note.Project)?.name}
)}
) : null} {/* Note Content */}
{/* NoteModal for editing */} {isNoteModalOpen && ( setIsNoteModalOpen(false)} onSave={handleSaveNote} note={note} /> )} {/* ConfirmDialog */} {isConfirmDialogOpen && noteToDelete && ( { setIsConfirmDialogOpen(false); setNoteToDelete(null); }} /> )}
); }; export default NoteDetails;