-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotificationsDropdown.tsx
191 lines (184 loc) · 7.13 KB
/
notificationsDropdown.tsx
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
import { api } from '../../utils/api';
import Image from 'next/image';
import { AnimatePresence, motion } from 'framer-motion';
import { formatDistanceToNowStrict } from 'date-fns';
import locale from 'date-fns/locale/en-US';
import { customFormatDistance } from '../../utils/customFormat';
import { useSubscribeToUserEvent } from '../../utils/pusher';
import { useRouter } from 'next/router';
import Link from 'next/link';
import { useTranslations } from 'next-intl';
type Props = {
setShowDropdown: (show: boolean) => void;
};
export const NotificationsDropdown: React.FC<Props> = ({ setShowDropdown }) => {
const t = useTranslations('notifications');
const router = useRouter();
const utils = api.useContext();
const updateNotifications = api.notifications.updateNotification.useMutation();
const acceptConnectionMutation = api.connections.acceptConnection.useMutation();
const declineConnectionMutation = api.connections.removeConnection.useMutation();
const { data: notifications } = api.notifications.getNotifications.useQuery(
{ unreadOnly: true },
{
refetchOnWindowFocus: false,
},
);
const markAsSeen = api.notifications.updateNotification.useMutation({
onSuccess: () => {
void utils.notifications.getNotifications.refetch();
void utils.notifications.getNotificationCount.refetch();
},
});
useSubscribeToUserEvent('notification', () => {
void utils.notifications.getNotifications.refetch();
});
const update = async (notificationId: string) => {
await updateNotifications.mutateAsync({
id: notificationId,
type: 'ConnectionResponse',
seen: true,
});
await utils.notifications.getNotifications.refetch();
await utils.notifications.getNotificationCount.refetch();
};
return (
<motion.div
initial={{ height: 0, width: 0 }}
animate={{ height: 400, width: 380 }}
exit={{
height: 0,
width: 0,
transition: {
type: 'tween',
duration: 0.1,
},
}}
transition={{
type: 'spring',
stiffness: 300,
damping: 30,
}}
className='absolute top-12 -right-2 flex h-96 w-[380px] flex-col gap-2 overflow-y-auto rounded-lg bg-white px-4 py-2 shadow-xl'
>
<motion.div
initial={{ opacity: 0, y: -10 }}
animate={{ opacity: 1, y: 0, transition: { delay: 0.1 } }}
className='flex items-center justify-between'
>
<span className='text-lg font-bold text-primary-300'>Notifications</span>
<Link
href='/notifications'
onClick={() => setShowDropdown(false)}
className='rounded-md px-2 py-1 text-primary-100 transition-colors duration-200 hover:bg-primary-100/10 active:bg-primary-100/30'
>
{t('see-all')}
</Link>
</motion.div>
{notifications?.length ? (
notifications.map((notification, index) => (
<motion.a
href='#'
initial={{ opacity: 0, y: -10 }}
animate={{ opacity: 1, y: 0, transition: { delay: index * 0.1 + 0.2 } }}
transition={{
type: 'spring',
stiffness: 300,
damping: 30,
}}
key={notification.id}
onClick={() => {
void markAsSeen.mutate({ id: notification.id, seen: true });
if (!!notification.route) {
void router.push(notification.route);
}
}}
className={`flex items-center gap-2 rounded-lg p-2 pr-4 transition-colors duration-200 ${
notification.seen ? 'bg-gray-200/30' : 'bg-primary-100/20'
} ${notification.route ? 'hover:bg-primary-100/30 active:bg-primary-100/50' : ''}`}
>
<Image
alt='User Avatar'
loader={() => notification.Sender?.image || '/placeholder.jpeg'}
src={notification.Sender?.image || '/placeholder.jpeg'}
width={52}
height={52}
className='rounded-full'
referrerPolicy='no-referrer'
/>
<div className='flex flex-1 flex-col'>
<div className='flex items-center justify-between'>
<span className='font-bold text-primary-300'>
{notification.Sender.firstName} {notification.Sender.lastName}
</span>
<span className='text-xs text-primary-300'>
{formatDistanceToNowStrict(notification.createdAt, {
locale: { ...locale, formatDistance: customFormatDistance },
})}
</span>
</div>
<span className='text-left leading-[1.2] text-primary-100'>
{t(`content.${notification.type}`, {
name: `${notification.Sender.firstName || ''} ${notification.Sender.lastName || ''}`,
content: notification.content,
})}
</span>
<AnimatePresence>
{notification.type === 'ConnectionRequest' && (
<motion.div
className='mt-2 flex gap-2'
exit={{
opacity: 0,
y: 10,
}}
transition={{
type: 'spring',
stiffness: 400,
damping: 30,
}}
>
<button
onClick={(e) => {
e.stopPropagation();
acceptConnectionMutation.mutate(
{ userEmail: notification.Sender.email },
{
onSuccess: () => {
void update(notification.id);
},
},
);
}}
className='flex-1 rounded-md bg-primary-400 px-2 py-1 text-white hover:bg-primary-500 active:bg-primary-600'
>
{t('accept')}
</button>
<button
onClick={(e) => {
e.stopPropagation();
declineConnectionMutation.mutate(
{ userEmail: notification.Sender.email },
{
onSuccess: () => {
void utils.notifications.getNotifications.refetch();
void utils.notifications.getNotificationCount.refetch();
},
},
);
}}
className='flex-1 rounded-md bg-primary-100/20 px-2 py-1 text-primary-100 hover:bg-primary-100/30 active:bg-primary-100/50'
>
{t('decline')}
</button>
</motion.div>
)}
</AnimatePresence>
</div>
</motion.a>
))
) : (
<div className='pt-8 text-center text-primary-100/50'>{t('empty')}</div>
)}
</motion.div>
);
};