page.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use client';
  2. import { useEffect, useState } from 'react';
  3. import { fetchApi } from '@/lib/utils/client';
  4. import { NotificationItem } from '@/types/notification';
  5. import { NotificationListResponse } from '@/types/response/notification/list';
  6. import { NotificationReadRequest } from '@/types/request/notification/read';
  7. const TYPE_LABELS: Record<number, string> = {
  8. 30: '새 쪽지',
  9. 99: '시스템'
  10. };
  11. export default function NotificationPage() {
  12. const [notifications, setNotifications] = useState<NotificationItem[]>([]);
  13. const [total, setTotal] = useState(0);
  14. const [page, setPage] = useState(1);
  15. const [loading, setLoading] = useState(true);
  16. useEffect(() => {
  17. const loadNotifications = async () => {
  18. setLoading(true);
  19. try {
  20. const res = await fetchApi<NotificationListResponse>(`/api/notification/list?page=${page}&perPage=20`, { silent: true });
  21. if (res.data) {
  22. setNotifications(res.data.list || []);
  23. setTotal(res.data.total || 0);
  24. }
  25. } catch {}
  26. setLoading(false);
  27. };
  28. loadNotifications();
  29. }, [page]);
  30. const handleRead = async (item: NotificationItem) => {
  31. if (!item.isRead) {
  32. await fetchApi('/api/notification/read', {
  33. method: 'POST',
  34. body: { notificationID: item.id } as NotificationReadRequest,
  35. silent: true
  36. });
  37. setNotifications(prev => prev.map(n => n.id === item.id ? { ...n, isRead: true } : n));
  38. }
  39. if (item.actionUrl) {
  40. window.location.href = item.actionUrl;
  41. }
  42. };
  43. const handleReadAll = async () => {
  44. await fetchApi('/api/notification/read', {
  45. method: 'POST',
  46. body: {} as NotificationReadRequest,
  47. silent: true
  48. });
  49. setNotifications(prev => prev.map(n => ({ ...n, isRead: true })));
  50. };
  51. const formatDate = (dateStr: string) => {
  52. const d = new Date(dateStr);
  53. const now = new Date();
  54. const diff = Math.floor((now.getTime() - d.getTime()) / 60000);
  55. if (diff < 1) { return '방금'; }
  56. if (diff < 60) { return `${diff}분 전`; }
  57. if (diff < 1440) { return `${Math.floor(diff / 60)}시간 전`; }
  58. return `${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')}`;
  59. };
  60. return (
  61. <div className="container mx-auto max-w-2xl p-4">
  62. <div className="flex items-center justify-between mb-4">
  63. <h1 className="text-xl font-bold">알림</h1>
  64. <button type="button" onClick={handleReadAll} className="text-sm text-blue-500 hover:underline">모두 읽음</button>
  65. </div>
  66. {loading && <div className="text-center text-gray-500 py-10">준비 중...</div>}
  67. {!loading && notifications.length === 0 && (
  68. <div className="text-center text-gray-500 py-10">알림이 없습니다</div>
  69. )}
  70. <div className="flex flex-col gap-2">
  71. {notifications.map(n => (
  72. <div key={n.id} onClick={() => handleRead(n)} className={`flex items-start gap-3 p-3 rounded-lg cursor-pointer border transition-colors ${n.isRead ? 'bg-white dark:bg-gray-900 border-gray-200 dark:border-gray-700' : 'bg-blue-50 dark:bg-blue-950 border-blue-200 dark:border-blue-800'}`}>
  73. {n.imageUrl ? (
  74. <img src={n.imageUrl} alt="" className="w-10 h-10 rounded-full object-cover flex-shrink-0" />
  75. ) : (
  76. <div className="w-10 h-10 rounded-full bg-gray-200 dark:bg-gray-700 flex items-center justify-center flex-shrink-0 text-lg">🔔</div>
  77. )}
  78. <div className="flex-1 overflow-hidden">
  79. <div className="flex items-center gap-2 mb-1">
  80. <span className="text-xs bg-gray-100 dark:bg-gray-800 px-1.5 py-0.5 rounded text-gray-600 dark:text-gray-400">
  81. {TYPE_LABELS[n.type] || '알림'}
  82. </span>
  83. <span className="text-xs text-gray-400">{formatDate(n.createdAt)}</span>
  84. </div>
  85. <div className={`text-sm ${n.isRead ? 'font-normal' : 'font-semibold'}`}>{n.title}</div>
  86. <div className="text-xs text-gray-500 mt-0.5 overflow-hidden text-ellipsis whitespace-nowrap">{n.message}</div>
  87. </div>
  88. {!n.isRead && <div className="w-2 h-2 rounded-full bg-blue-500 flex-shrink-0 mt-2" />}
  89. </div>
  90. ))}
  91. </div>
  92. {total > 20 && (
  93. <div className="flex justify-center gap-2 mt-4">
  94. <button type="button" onClick={() => setPage(p => Math.max(1, p - 1))} disabled={page <= 1} className="px-3 py-1 text-sm border rounded disabled:opacity-50">이전</button>
  95. <span className="px-3 py-1 text-sm">{page} / {Math.ceil(total / 20)}</span>
  96. <button type="button" onClick={() => setPage(p => p + 1)} disabled={page >= Math.ceil(total / 20)} className="px-3 py-1 text-sm border rounded disabled:opacity-50">다음</button>
  97. </div>
  98. )}
  99. </div>
  100. );
  101. }