'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 { NoteOutboxResponse, SentNotePreview } from '@/types/response/note/outbox'; import type { RecipientItem } from '@/types/response/note/searchRecipient'; import NoteComposeModal from '../_components/NoteComposeModal'; import NoteDetailModal from '../_components/NoteDetailModal'; export default function NoteOutboxPage() { const [notes, setNotes] = useState([]); const [total, setTotal] = 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/outbox?page=${page}&perPage=20`); if (res.data) { setNotes(res.data.list || []); setTotal(res.data.total || 0); } } catch {} setLoading(false); }, [page]); useEffect(() => { loadNotes(); }, [loadNotes]); const openNote = (note: SentNotePreview) => { setDetailNoteId(note.id); }; const handleOpenCompose = () => { setComposeInitialReceiver(null); setComposeInitialTitle(undefined); setComposeOpen(true); }; const handleSent = () => { loadNotes(); }; const handleDeleted = () => { loadNotes(); }; const readCount = notes.filter(n => n.isRead).length; 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 ( <>

보낸 쪽지함

읽음 {readCount} / {notes.length}건
{loading && } {!loading && notes.length === 0 && (
보낸 쪽지가 없습니다
)} {!loading && notes.length > 0 && (
{notes.map(note => { const counterparty = formatNoteCounterparty({ name: note.receiverName, sid: note.receiverSID, channelName: note.receiverChannelName, channelHandle: note.receiverChannelHandle }); return (
openNote(note)} className="note-page__item" >
{note.isRead ? '읽음' : '안읽음'} {note.title}
{counterparty.primary} {counterparty.secondary && {counterparty.secondary}} · {formatDate(note.createdAt)}
); })}
)} {total > 20 && (
{page} / {Math.ceil(total / 20)}
)} setComposeOpen(false)} initialReceiver={composeInitialReceiver} initialTitle={composeInitialTitle} onSent={handleSent} /> setDetailNoteId(null)} onDeleted={handleDeleted} /> ); }