-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathnotification-box.tsx
239 lines (216 loc) · 7.15 KB
/
notification-box.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
'use client'
import { HTMLAttributes, useMemo } from 'react'
import { TabsContent } from '@radix-ui/react-tabs'
import { AnimatePresence, motion } from 'framer-motion'
import moment from 'moment'
import useSWR from 'swr'
import { useQuery } from 'urql'
import { graphql } from '@/lib/gql/generates'
import { NotificationsQuery } from '@/lib/gql/generates/graphql'
import { useMutation } from '@/lib/tabby/gql'
import { notificationsQuery } from '@/lib/tabby/query'
import { ArrayElementType } from '@/lib/types'
import { cn } from '@/lib/utils'
import { Button } from '@/components/ui/button'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuTrigger
} from '@/components/ui/dropdown-menu'
import { IconBell, IconCheck } from '@/components/ui/icons'
import { Separator } from '@/components/ui/separator'
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'
import LoadingWrapper from '@/components/loading-wrapper'
import { MemoizedReactMarkdown } from '@/components/markdown'
import { ListSkeleton } from '@/components/skeleton'
interface Props extends HTMLAttributes<HTMLDivElement> {}
const markNotificationsReadMutation = graphql(/* GraphQL */ `
mutation markNotificationsRead($notificationId: ID) {
markNotificationsRead(notificationId: $notificationId)
}
`)
export function NotificationBox({ className, ...rest }: Props) {
const [{ data, fetching }, reexecuteQuery] = useQuery({
query: notificationsQuery
})
useSWR('refresh_notifications', () => reexecuteQuery(), {
revalidateOnFocus: true,
revalidateOnReconnect: true,
revalidateOnMount: false,
refreshInterval: 1000 * 60 * 10 // 10 mins
})
const notifications = useMemo(() => {
return data?.notifications.slice().reverse()
}, [data?.notifications])
const unreadNotifications = useMemo(() => {
return notifications?.filter(o => !o.read) ?? []
}, [notifications])
const hasUnreadNotification = unreadNotifications.length > 0
const markNotificationsRead = useMutation(markNotificationsReadMutation)
const onClickMarkAllRead = () => {
markNotificationsRead()
}
return (
<div className={cn(className)} {...rest}>
<DropdownMenu modal={false}>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="icon" className="relative">
<IconBell />
{hasUnreadNotification && (
<div className="absolute right-1 top-1 h-1.5 w-1.5 rounded-full bg-red-400"></div>
)}
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent
side="bottom"
align="end"
className="flex w-96 flex-col overflow-hidden p-0"
style={{ maxHeight: '60vh' }}
>
<div className="flex items-center justify-between px-4 py-2">
<div className="text-sm font-medium">Nofitications</div>
<Button
size="sm"
className="h-6 py-1 text-xs"
onClick={onClickMarkAllRead}
disabled={!hasUnreadNotification}
>
Mark all as read
</Button>
</div>
<Separator />
<Tabs
className="relative my-2 flex-1 overflow-y-auto px-5"
defaultValue="unread"
>
<TabsList className="sticky top-0 z-10 grid w-full grid-cols-2">
<TabsTrigger value="unread">Unread</TabsTrigger>
<TabsTrigger value="all">All</TabsTrigger>
</TabsList>
<TabsContent value="unread" className="mt-4">
<LoadingWrapper loading={fetching} fallback={<ListSkeleton />}>
<NotificationList
type="unread"
notifications={unreadNotifications}
/>
</LoadingWrapper>
</TabsContent>
<TabsContent value="all" className="mt-4">
<LoadingWrapper loading={fetching} fallback={<ListSkeleton />}>
<NotificationList type="all" notifications={notifications} />
</LoadingWrapper>
</TabsContent>
</Tabs>
</DropdownMenuContent>
</DropdownMenu>
</div>
)
}
function NotificationList({
notifications,
type
}: {
notifications: NotificationsQuery['notifications'] | undefined
type: 'unread' | 'all'
}) {
const len = notifications?.length ?? 0
if (!len) {
return (
<div className="my-4 text-center text-sm text-muted-foreground">
{type === 'unread' ? 'No unread notifications' : 'No notifications'}
</div>
)
}
return (
<div className="space-y-2">
<AnimatePresence>
{notifications?.map((item, index) => {
return (
<motion.div layout key={item.id}>
<NotificationItem data={item} />
<Separator
className={cn('my-3', {
hidden: index === len - 1
})}
/>
</motion.div>
)
})}
</AnimatePresence>
</div>
)
}
interface NotificationItemProps extends HTMLAttributes<HTMLDivElement> {
data: ArrayElementType<NotificationsQuery['notifications']>
}
function NotificationItem({ data }: NotificationItemProps) {
const markNotificationsRead = useMutation(markNotificationsReadMutation)
const onClickMarkRead = () => {
markNotificationsRead({
notificationId: data.id
})
}
return (
<div className="space-y-1.5">
<MemoizedReactMarkdown
className={cn(
'prose max-w-none break-words text-sm dark:prose-invert prose-p:my-1 prose-p:leading-relaxed',
{ 'unread-notification': !data.read }
)}
components={{
a: props => (
<a
{...props}
onClick={e => {
onClickMarkRead()
props.onClick?.(e)
}}
/>
)
}}
>
{data.content}
</MemoizedReactMarkdown>
<div className="flex items-center justify-between text-xs text-muted-foreground">
<span className="text-muted-foreground">
{formatNotificationTime(data.createdAt)}
</span>
<div className="flex items-center gap-1.5">
{!data.read && (
<Button
variant="link"
className="flex h-auto items-center gap-0.5 p-1 text-xs text-muted-foreground"
onClick={onClickMarkRead}
>
<IconCheck className="h-3 w-3" />
Mark as read
</Button>
)}
</div>
</div>
</div>
)
}
function resolveNotification(content: string) {
// use first line as title
const title = content.split('\n')[0]
const _content = content.split('\n').slice(1).join('\n')
return {
title,
content: _content
}
}
// Nov 21, 2022, 7:03 AM
// Nov 21, 7:03 AM
function formatNotificationTime(time: string) {
const targetTime = moment(time)
if (targetTime.isBefore(moment().subtract(1, 'year'))) {
const timeText = targetTime.format('MMM D, YYYY, h:mm A')
return timeText
}
if (targetTime.isBefore(moment().subtract(1, 'month'))) {
const timeText = targetTime.format('MMM D, hh:mm A')
return `${timeText}`
}
return `${targetTime.fromNow()}`
}