1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
import { useState } from "react";
import useLocalState from "@/state/state";
import Modal from "./modals/Modal";
import Icon from "./Icon";
import Avatar from "./Avatar";
import { useLocation } from "wouter";
import type { Notification, NotificationType } from "@/types/notifications";
import "@/styles/NotificationCenter.css";
const NotificationCenter = () => {
const [_, navigate] = useLocation();
const {
notifications,
unreadNotifications,
markNotificationRead,
markAllNotificationsRead,
clearNotifications,
setModal
} = useLocalState((s) => ({
notifications: s.notifications,
unreadNotifications: s.unreadNotifications,
markNotificationRead: s.markNotificationRead,
markAllNotificationsRead: s.markAllNotificationsRead,
clearNotifications: s.clearNotifications,
setModal: s.setModal
}));
const [filter, setFilter] = useState<"all" | "unread">("all");
const filteredNotifications = filter === "unread"
? notifications.filter(n => !n.read)
: notifications;
const handleNotificationClick = (notification: Notification) => {
// Mark as read
if (!notification.read) {
markNotificationRead(notification.id);
}
// Navigate based on notification type
if (notification.postId) {
// Navigate to post
navigate(`/post/${notification.postId}`);
setModal(null);
} else if (notification.type === "follow" || notification.type === "access_request") {
// Navigate to user profile
navigate(`/feed/${notification.from}`);
setModal(null);
}
};
const getNotificationIcon = (type: NotificationType) => {
switch (type) {
case "follow":
case "unfollow":
return "pals";
case "mention":
case "reply":
return "messages";
case "repost":
return "repost";
case "react":
return "emoji";
case "access_request":
case "access_granted":
return "key";
default:
return "bell";
}
};
const getNotificationText = (notification: Notification) => {
switch (notification.type) {
case "follow":
return `${notification.from} started following you`;
case "unfollow":
return `${notification.from} unfollowed you`;
case "mention":
return `${notification.from} mentioned you in a post`;
case "reply":
return `${notification.from} replied to your post`;
case "repost":
return `${notification.from} reposted your post`;
case "react":
return `${notification.from} reacted ${notification.reaction || ""} to your post`;
case "access_request":
return `${notification.from} requested access to your feed`;
case "access_granted":
return `${notification.from} granted you access to their feed`;
default:
return notification.message || "New notification";
}
};
const formatTimestamp = (date: Date) => {
const now = new Date();
const diff = now.getTime() - new Date(date).getTime();
const minutes = Math.floor(diff / 60000);
const hours = Math.floor(diff / 3600000);
const days = Math.floor(diff / 86400000);
if (minutes < 1) return "Just now";
if (minutes < 60) return `${minutes}m ago`;
if (hours < 24) return `${hours}h ago`;
if (days < 7) return `${days}d ago`;
return new Date(date).toLocaleDateString();
};
return (
<Modal close={() => setModal(null)}>
<div className="notification-center">
<div className="notification-header">
<h2>Notifications</h2>
<div className="notification-actions">
{unreadNotifications > 0 && (
<button
className="mark-all-read-btn"
onClick={markAllNotificationsRead}
>
Mark all as read
</button>
)}
{notifications.length > 0 && (
<button
className="clear-all-btn"
onClick={clearNotifications}
>
Clear all
</button>
)}
</div>
</div>
<div className="notification-filters">
<button
className={`filter-btn ${filter === "all" ? "active" : ""}`}
onClick={() => setFilter("all")}
>
All ({notifications.length})
</button>
<button
className={`filter-btn ${filter === "unread" ? "active" : ""}`}
onClick={() => setFilter("unread")}
>
Unread ({unreadNotifications})
</button>
</div>
<div className="notification-list">
{filteredNotifications.length === 0 ? (
<div className="no-notifications">
<Icon name="bell" size={48} color="textMuted" />
<p>No {filter === "unread" ? "unread " : ""}notifications</p>
</div>
) : (
filteredNotifications.map((notification) => (
<div
key={notification.id}
className={`notification-item ${!notification.read ? "unread" : ""}`}
onClick={() => handleNotificationClick(notification)}
>
<div className="notification-icon">
<Icon
name={getNotificationIcon(notification.type)}
size={20}
color={!notification.read ? "primary" : "textSecondary"}
/>
</div>
<div className="notification-content">
<div className="notification-user">
<Avatar p={notification.from} size={32} />
<div className="notification-text">
<p>{getNotificationText(notification)}</p>
<span className="notification-time">
{formatTimestamp(notification.timestamp)}
</span>
</div>
</div>
</div>
{!notification.read && <div className="unread-indicator" />}
</div>
))
)}
</div>
</div>
</Modal>
);
};
export default NotificationCenter;
|