'use client'; import { useCallback, useEffect, useState } from 'react'; import { fetchApi } from '@/lib/utils/client'; import { formatNoteCounterparty } from '@/lib/utils/note'; import Loading from '@/app/component/Loading'; import { NotePreview } from '@/types/notification'; import { NoteInboxResponse } from '@/types/response/note/inbox'; import type { RecipientItem } from '@/types/response/note/searchRecipient'; import NoteComposeModal from '../_components/NoteComposeModal'; import NoteDetailModal from '../_components/NoteDetailModal'; export default function NoteInboxPage() { const [notes, setNotes] = useState([]); const [total, setTotal] = useState(0); const [unreadCount, setUnreadCount] = useState(0); const [page, setPage] = useState(1); const [loading, setLoading] = useState(true); const [composeOpen, setComposeOpen] = useState(false); const [composeInitialReceiver, setComposeInitialReceiver] = useState(null); const [composeInitialTitle, setComposeInitialTitle] = useState(undefined); const [detailNoteId, setDetailNoteId] = useState(null); const loadNotes = useCallback(async () => { setLoading(true); try { const res = await fetchApi(`/api/note/inbox?page=${page}&perPage=20`); if (res.data) { setNotes(res.data.list || []); setTotal(res.data.total || 0); setUnreadCount(res.data.unreadCount || 0); } } catch {} setLoading(false); }, [page]); useEffect(() => { loadNotes(); }, [loadNotes]); const openNote = (note: NotePreview) => { setDetailNoteId(note.id); if (!note.isRead) { setNotes(prev => prev.map(n => n.id === note.id ? { ...n, isRead: true } : n)); setUnreadCount(prev => Math.max(0, prev - 1)); } }; const handleOpenCompose = () => { setComposeInitialReceiver(null); setComposeInitialTitle(undefined); setComposeOpen(true); }; const handleReply = (sender: RecipientItem, originalTitle: string) => { setDetailNoteId(null); setComposeInitialReceiver(sender); setComposeInitialTitle(`RE: ${originalTitle}`); setComposeOpen(true); }; const handleSent = () => { loadNotes(); }; const handleDeleted = () => { loadNotes(); }; const formatDate = (dateStr: string) => { const d = new Date(dateStr); return `${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')} ${d.getHours().toString().padStart(2, '0')}:${d.getMinutes().toString().padStart(2, '0')}`; }; return ( <>

받은 쪽지함

안읽은 쪽지: {unreadCount}건
{loading && } {!loading && notes.length === 0 && (
쪽지가 없습니다
)} {!loading && notes.length > 0 && (
{notes.map(note => { const counterparty = formatNoteCounterparty({ isSystem: note.isSystem, name: note.senderName, sid: note.senderSID, channelName: note.senderChannelName, channelHandle: note.senderChannelHandle }); return (
openNote(note)} className={`note-page__item${note.isRead ? '' : ' note-page__item--unread'}`} >
{note.isSystem && 시스템} {note.title}
{counterparty.primary} {counterparty.secondary && {counterparty.secondary}} · {formatDate(note.createdAt)}
{!note.isRead &&
}
); })}
)} {total > 20 && (
{page} / {Math.ceil(total / 20)}
)} setComposeOpen(false)} initialReceiver={composeInitialReceiver} initialTitle={composeInitialTitle} onSent={handleSent} /> setDetailNoteId(null)} onReply={handleReply} onDeleted={handleDeleted} /> ); }