forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationItem.tsx
More file actions
127 lines (118 loc) · 3.79 KB
/
Copy pathNotificationItem.tsx
File metadata and controls
127 lines (118 loc) · 3.79 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
import type { ComponentType, MouseEvent } from "react";
import { Link } from "react-router-dom";
import {
Bell,
UserPlus,
Receipt,
CheckCircle2,
XCircle,
Users,
Megaphone,
CreditCard,
} from "lucide-react";
import type { Notification, NotificationType } from "../../types/notifications";
import { useNotificationsStore } from "../../store/notifications";
const TYPE_ICONS: Record<NotificationType, ComponentType<{ className?: string }>> = {
split_invitation: Users,
payment_reminder: CreditCard,
payment_received: Receipt,
split_completed: CheckCircle2,
split_cancelled: XCircle,
friend_request: UserPlus,
system_announcement: Megaphone,
};
function formatTime(iso: string): string {
const date = new Date(iso);
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffMins = Math.floor(diffMs / 60000);
const diffHours = Math.floor(diffMs / 3600000);
const diffDays = Math.floor(diffMs / 86400000);
if (diffMins < 1) return "Just now";
if (diffMins < 60) return `${diffMins}m ago`;
if (diffHours < 24) return `${diffHours}h ago`;
if (diffDays < 7) return `${diffDays}d ago`;
return date.toLocaleDateString();
}
interface NotificationItemProps {
notification: Notification;
compact?: boolean;
}
export function NotificationItem({ notification, compact = false }: NotificationItemProps) {
const { markAsRead, markAsUnread } = useNotificationsStore();
const Icon = TYPE_ICONS[notification.type] ?? Bell;
const isUnread = !notification.read;
const content = (
<>
<div
className={`flex shrink-0 w-10 h-10 rounded-full items-center justify-center bg-theme border-theme border ${isUnread ? "bg-accent/10 text-accent" : "bg-surface text-theme"}`}
>
<Icon className="w-5 h-5" />
</div>
<div className="min-w-0 flex-1">
<p className={`text-sm font-medium text-theme ${isUnread ? "font-semibold" : ""}`}>
{notification.title}
</p>
<p className="text-sm text-muted-theme line-clamp-2 mt-0.5">
{notification.message}
</p>
<p className="text-xs text-muted-theme mt-1">{formatTime(notification.createdAt)}</p>
</div>
</>
);
const toggleRead = (e: MouseEvent) => {
e.preventDefault();
e.stopPropagation();
if (notification.read) {
markAsUnread(notification.id);
} else {
markAsRead(notification.id);
}
};
const wrapperClass = `
flex gap-3 p-3 rounded-lg border border-theme bg-card-theme
transition-colors hover:bg-surface
${isUnread ? "bg-accent/5 border-accent/30" : ""}
${compact ? "py-2" : ""}
`.trim();
if (notification.actionUrl) {
return (
<Link
to={notification.actionUrl}
className={`block ${wrapperClass}`}
onClick={() => !notification.read && markAsRead(notification.id)}
data-testid={`notification-${notification.id}`}
>
{content}
{!compact && (
<button
type="button"
onClick={toggleRead}
className="shrink-0 self-start text-xs text-muted-theme hover:text-theme underline"
aria-label={notification.read ? "Mark as unread" : "Mark as read"}
>
{notification.read ? "Mark as unread" : "Mark as read"}
</button>
)}
</Link>
);
}
return (
<div
className={wrapperClass}
data-testid={`notification-${notification.id}`}
>
{content}
{!compact && (
<button
type="button"
onClick={toggleRead}
className="shrink-0 self-start text-xs text-muted-theme hover:text-theme underline"
aria-label={notification.read ? "Mark as unread" : "Mark as read"}
>
{notification.read ? "Mark as unread" : "Mark as read"}
</button>
)}
</div>
);
}