import React, { useCallback, useContext, useEffect, useState } from 'react' import PropTypes from 'prop-types' import { Card, Button, Space, Typography, Flex, Modal, Form, Input, Switch, Spin, Alert, message, Divider, Tag, Dropdown } from 'antd' import { CaretLeftFilled, LoadingOutlined } from '@ant-design/icons' import PlusIcon from '../../Icons/PlusIcon' import BinIcon from '../../Icons/BinIcon' import PersonIcon from '../../Icons/PersonIcon' import TimeDisplay from './TimeDisplay' import MarkdownDisplay from './MarkdownDisplay' import axios from 'axios' import config from '../../../config' import { AuthContext } from '../context/AuthContext' import InfoCircleIcon from '../../Icons/InfoCircleIcon' import NoteTypeSelect from './NoteTypeSelect' import IdDisplay from './IdDisplay' import ReloadIcon from '../../Icons/ReloadIcon' import ExclamationOctagonIcon from '../../Icons/ExclamationOctagonIcon' const { Text, Title } = Typography const { TextArea } = Input const NoteItem = ({ note, expandedNotes, setExpandedNotes, fetchData, onNewNote, onDeleteNote, userProfile, onChildNoteAdded }) => { const [childNotes, setChildNotes] = useState({}) const [loadingChildNotes, setLoadingChildNotes] = useState(null) const isExpanded = expandedNotes[note._id] const hasChildNotes = childNotes[note._id] && childNotes[note._id].length > 0 const isThisNoteLoading = loadingChildNotes === note._id let transformValue = 'rotate(0deg)' if (isExpanded) { transformValue = 'rotate(-90deg)' } const handleNoteExpand = async (noteId) => { const newExpandedState = !expandedNotes[noteId] setExpandedNotes((prev) => ({ ...prev, [noteId]: newExpandedState })) if (newExpandedState && !childNotes[noteId]) { setLoadingChildNotes(noteId) try { const childNotesData = await fetchData(noteId) setChildNotes((prev) => ({ ...prev, [noteId]: childNotesData })) } catch (error) { console.error('Error fetching child notes:', error) } finally { setLoadingChildNotes(null) } } } const handleNewChildNote = () => { if (onNewNote) { onNewNote(note._id) } } const handleDeleteNote = () => { if (onDeleteNote) { onDeleteNote(note._id) } } // Reload child notes when a new child note is added const reloadChildNotes = async () => { // Always fetch child notes when this function is called // This ensures child notes are loaded even if the parent wasn't expanded before setLoadingChildNotes(note._id) try { const childNotesData = await fetchData(note._id) setChildNotes((prev) => ({ ...prev, [note._id]: childNotesData })) } catch (error) { console.error('Error fetching child notes:', error) } finally { setLoadingChildNotes(null) } } // Listen for child note additions useEffect(() => { if (onChildNoteAdded) { onChildNoteAdded(note._id, reloadChildNotes) } }, [note._id, onChildNoteAdded]) // Check if the current user can delete this note const canDeleteNote = userProfile && userProfile._id === note.user._id const dropdownItems = [ { key: 'new', icon: , label: 'New Note', onClick: handleNewChildNote } ] // Only add delete option if user owns the note if (canDeleteNote) { dropdownItems.push({ key: 'delete', label: 'Delete Note', icon: , onClick: handleDeleteNote, danger: true }) } return ( {note.user.name}:
Type: {note.noteType.name} User ID: Created At: