2022-06-01 13:11:25 +00:00
|
|
|
import { BellIcon } from '@heroicons/react/outline'
|
|
|
|
import clsx from 'clsx'
|
|
|
|
import { Row } from 'web/components/layout/row'
|
|
|
|
import { useEffect, useState } from 'react'
|
2022-06-06 16:54:25 +00:00
|
|
|
import { Notification } from 'common/notification'
|
|
|
|
import { listenForNotifications } from 'web/lib/firebase/notifications'
|
2022-06-01 13:11:25 +00:00
|
|
|
import { useUser } from 'web/hooks/use-user'
|
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
|
|
|
|
export default function NotificationsIcon(props: { className?: string }) {
|
|
|
|
const user = useUser()
|
2022-06-06 16:54:25 +00:00
|
|
|
const [notifications, setNotifications] = useState<
|
|
|
|
Notification[] | undefined
|
|
|
|
>()
|
2022-06-01 13:11:25 +00:00
|
|
|
const router = useRouter()
|
|
|
|
useEffect(() => {
|
2022-06-06 16:54:25 +00:00
|
|
|
if (router.pathname.endsWith('notifications')) return setNotifications([])
|
2022-06-01 13:11:25 +00:00
|
|
|
}, [router.pathname])
|
|
|
|
|
2022-06-06 16:54:25 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (user) return listenForNotifications(user.id, setNotifications, true)
|
|
|
|
}, [user])
|
|
|
|
|
2022-06-01 13:11:25 +00:00
|
|
|
return (
|
|
|
|
<Row className={clsx('justify-center')}>
|
|
|
|
<div className={'relative'}>
|
2022-06-06 16:54:25 +00:00
|
|
|
{notifications && notifications.length > 0 && (
|
|
|
|
<div className="-mt-0.75 absolute ml-3.5 min-w-[15px] rounded-full bg-indigo-500 p-[2px] text-center text-[10px] leading-3 text-white lg:-mt-1 lg:ml-2">
|
|
|
|
{notifications.length}
|
|
|
|
</div>
|
2022-06-01 13:11:25 +00:00
|
|
|
)}
|
|
|
|
<BellIcon className={clsx(props.className)} />
|
|
|
|
</div>
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|