diff --git a/frontend/src/components/MailList.tsx b/frontend/src/components/MailList.tsx new file mode 100644 index 00000000..8d13efe0 --- /dev/null +++ b/frontend/src/components/MailList.tsx @@ -0,0 +1,107 @@ +import { useMailStore } from '../store/mailStore' +import { mailApi } from '../api/mailApi' +import { formatDistanceToNow, parseISO } from 'date-fns' +import { ko } from 'date-fns/locale' + +function fmtDate(d: string) { + try { + return formatDistanceToNow(parseISO(d), { addSuffix: true, locale: ko }) + } catch { + return d?.slice(0, 16) || '' + } +} + +function fmtSize(b: number) { + if (b < 1024) return `${b}B` + if (b < 1024 * 1024) return `${(b / 1024).toFixed(0)}KB` + return `${(b / 1024 / 1024).toFixed(1)}MB` +} + +interface Props { onRefresh: () => void } + +export default function MailList({ onRefresh }: Props) { + const { messages, loading, currentFolder, totalMessages, currentPage, + selectedMail, setSelectedMail, setLoading, setMessages, + markRead, removeMessage } = useMailStore() + + // Sent 폴더에서는 받는사람 표시 + const isSent = currentFolder === 'Sent' + + const select = async (uid: string) => { + setLoading(true) + try { + const detail = await mailApi.detail(uid, currentFolder) + setSelectedMail(detail) + markRead(uid) + } catch { /* 오류 무시 */ } + finally { setLoading(false) } + } + + const del = async (e: React.MouseEvent, uid: string) => { + e.stopPropagation() + try { + await mailApi.delete(uid, currentFolder) + removeMessage(uid) + } catch { /* 오류 무시 */ } + } + + const perPage = 50 + const totalPages = Math.ceil(totalMessages / perPage) + + const goPage = async (p: number) => { + setLoading(true) + try { + const data = await mailApi.messages(currentFolder, p) + setMessages(data.messages || [], data.total || 0, p) + } finally { setLoading(false) } + } + + if (loading) return
⏳ 불러오는 중...
+ if (!messages.length) return ( +
+
📭
+
메일이 없습니다
+
+ ) + + return ( +
+
+ {totalMessages}개 + +
+ + + + {totalPages > 1 && ( +
+ + {currentPage} / {totalPages} + +
+ )} +
+ ) +}