forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationCenter.tsx
More file actions
208 lines (193 loc) · 8.22 KB
/
Copy pathNotificationCenter.tsx
File metadata and controls
208 lines (193 loc) · 8.22 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use client';
import { useMemo } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { X, Bell, CheckCheck, Trash2 } from 'lucide-react';
import { useNotificationContext } from './NotificationContext';
import { NotificationItem } from './NotificationItem';
import { NotificationPermissionBanner } from './NotificationPermissionBanner';
import { cn } from '@/lib/utils';
import type { OmiNotification } from '@/types/notification';
import { PanelReveal } from '@/components/ui/PanelReveal';
/**
* Group notifications by date (Today, Yesterday, Earlier)
*/
function groupNotificationsByDate(notifications: OmiNotification[]) {
const now = new Date();
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const yesterday = new Date(today.getTime() - 24 * 60 * 60 * 1000);
const groups: { label: string; notifications: OmiNotification[] }[] = [
{ label: 'Today', notifications: [] },
{ label: 'Yesterday', notifications: [] },
{ label: 'Earlier', notifications: [] },
];
notifications.forEach((notification) => {
const notifDate = new Date(notification.timestamp);
const notifDay = new Date(
notifDate.getFullYear(),
notifDate.getMonth(),
notifDate.getDate(),
);
if (notifDay.getTime() >= today.getTime()) {
groups[0].notifications.push(notification);
} else if (notifDay.getTime() >= yesterday.getTime()) {
groups[1].notifications.push(notification);
} else {
groups[2].notifications.push(notification);
}
});
// Filter out empty groups
return groups.filter((g) => g.notifications.length > 0);
}
export function NotificationCenter() {
const {
isOpen,
closeNotificationCenter,
notifications,
unreadCount,
permission,
isSupported,
markAllAsRead,
clearAllNotifications,
navigateToNotification,
markAsRead,
clearNotification,
getAppImage,
} = useNotificationContext();
const groupedNotifications = useMemo(
() => groupNotificationsByDate(notifications),
[notifications],
);
const showPermissionBanner =
isSupported && (permission === 'default' || permission === 'denied');
return (
<AnimatePresence>
{isOpen && (
<>
{/* Mobile backdrop */}
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.2 }}
className="fixed inset-0 bg-black/30 z-40 sm:hidden"
onClick={closeNotificationCenter}
/>
{/* Panel - push/slide animation */}
<motion.div
initial={{ width: 0 }}
animate={{ width: 400 }}
exit={{ width: 0 }}
transition={{ type: 'spring', damping: 30, stiffness: 300 }}
className={cn(
'h-full flex-shrink-0 overflow-hidden',
'bg-bg-secondary border-l border-bg-tertiary',
'max-sm:fixed max-sm:inset-0 max-sm:z-50 max-sm:w-full',
)}
>
<PanelReveal className={cn('w-[400px] h-full flex flex-col', 'max-sm:w-full')}>
{/* Header */}
<div className="flex items-center justify-between p-4 border-b border-bg-tertiary">
<div className="flex items-center gap-3">
<div className="w-8 h-8 rounded-full bg-white/[0.14] flex items-center justify-center">
<Bell className="w-4 h-4 text-text-primary" />
</div>
<div>
<h2 className="font-semibold text-text-primary">Notifications</h2>
{unreadCount > 0 && (
<p className="text-xs text-text-tertiary">{unreadCount} unread</p>
)}
</div>
</div>
<div className="flex items-center gap-1">
{unreadCount > 0 && (
<button
onClick={markAllAsRead}
className="p-2 rounded-lg hover:bg-bg-tertiary transition-colors"
aria-label="Mark all as read"
title="Mark all as read"
>
<CheckCheck className="w-4 h-4 text-text-quaternary hover:text-text-secondary" />
</button>
)}
{notifications.length > 0 && (
<button
onClick={clearAllNotifications}
className="p-2 rounded-lg hover:bg-bg-tertiary transition-colors"
aria-label="Clear all notifications"
title="Clear all notifications"
>
<Trash2 className="w-4 h-4 text-text-quaternary hover:text-text-secondary" />
</button>
)}
<button
onClick={closeNotificationCenter}
className="p-2 rounded-lg hover:bg-bg-tertiary transition-colors"
aria-label="Close notifications"
>
<X className="w-5 h-5 text-text-secondary" />
</button>
</div>
</div>
{/* Permission banner */}
{showPermissionBanner && <NotificationPermissionBanner />}
{/* Notifications list */}
<div className="flex-1 overflow-y-auto">
{notifications.length === 0 ? (
<div className="flex flex-col items-center justify-center h-full px-4 py-12 text-center">
<div className="w-16 h-16 rounded-full bg-bg-tertiary flex items-center justify-center mb-4">
<Bell className="w-8 h-8 text-text-quaternary" />
</div>
<p className="text-text-secondary font-medium mb-1">
No notifications yet
</p>
<p className="text-sm text-text-quaternary">
When you receive notifications, they'll appear here
</p>
</div>
) : (
<div className="divide-y divide-bg-tertiary">
{groupedNotifications.map((group) => (
<div key={group.label}>
<div className="px-4 py-2 bg-bg-primary/50 sticky top-0">
<p className="text-xs font-medium text-text-quaternary uppercase tracking-wide">
{group.label}
</p>
</div>
<div>
{group.notifications.map((notification) => {
// Extract app_id from notification data for image lookup
// Try multiple sources: direct data fields, or extract from navigate_to
let appId =
(notification.data?.app_id as string | undefined) ||
(notification.data?.plugin_id as string | undefined);
// Fallback: extract from navigate_to (e.g., /chat/bitcoin-live)
if (
!appId &&
notification.navigate_to?.startsWith('/chat/')
) {
appId = notification.navigate_to.split('/').pop();
}
return (
<NotificationItem
key={notification.id}
notification={notification}
onClick={() => navigateToNotification(notification)}
onMarkAsRead={() => markAsRead(notification.id)}
onClear={() => clearNotification(notification.id)}
appImage={getAppImage(appId)}
/>
);
})}
</div>
</div>
))}
</div>
)}
</div>
</PanelReveal>
</motion.div>
</>
)}
</AnimatePresence>
);
}