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-07-05 17:29:26 +00:00
|
|
|
import { usePrivateUser, useUser } from 'web/hooks/use-user'
|
2022-06-01 13:11:25 +00:00
|
|
|
import { useRouter } from 'next/router'
|
2022-06-06 22:15:36 +00:00
|
|
|
import { usePreferredGroupedNotifications } from 'web/hooks/use-notifications'
|
2022-07-05 17:29:26 +00:00
|
|
|
import { NOTIFICATIONS_PER_PAGE } from 'web/pages/notifications'
|
|
|
|
import { requestBonuses } from 'web/lib/firebase/api-call'
|
2022-06-01 13:11:25 +00:00
|
|
|
|
|
|
|
export default function NotificationsIcon(props: { className?: string }) {
|
|
|
|
const user = useUser()
|
2022-07-05 17:29:26 +00:00
|
|
|
const privateUser = usePrivateUser(user?.id)
|
|
|
|
const notifications = usePreferredGroupedNotifications(privateUser?.id, {
|
2022-06-06 22:15:36 +00:00
|
|
|
unseenOnly: true,
|
|
|
|
})
|
2022-06-06 17:36:59 +00:00
|
|
|
const [seen, setSeen] = useState(false)
|
|
|
|
|
2022-07-05 17:29:26 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!privateUser) return
|
|
|
|
|
2022-07-06 13:51:32 +00:00
|
|
|
if (Date.now() - (privateUser.lastTimeCheckedBonuses ?? 0) > 65 * 1000)
|
2022-07-05 17:29:26 +00:00
|
|
|
requestBonuses({}).catch((error) => {
|
|
|
|
console.log("couldn't get bonuses:", error.message)
|
|
|
|
})
|
|
|
|
}, [privateUser])
|
|
|
|
|
2022-06-01 13:11:25 +00:00
|
|
|
const router = useRouter()
|
|
|
|
useEffect(() => {
|
2022-06-06 17:36:59 +00:00
|
|
|
if (router.pathname.endsWith('notifications')) return setSeen(true)
|
|
|
|
else setSeen(false)
|
2022-06-01 13:11:25 +00:00
|
|
|
}, [router.pathname])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Row className={clsx('justify-center')}>
|
|
|
|
<div className={'relative'}>
|
2022-06-06 17:36:59 +00:00
|
|
|
{!seen && notifications && notifications.length > 0 && (
|
2022-06-06 22:15:36 +00:00
|
|
|
<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">
|
2022-07-05 17:29:26 +00:00
|
|
|
{notifications.length > NOTIFICATIONS_PER_PAGE
|
|
|
|
? `${NOTIFICATIONS_PER_PAGE}+`
|
|
|
|
: notifications.length}
|
2022-06-06 22:15:36 +00:00
|
|
|
</div>
|
2022-06-01 13:11:25 +00:00
|
|
|
)}
|
|
|
|
<BellIcon className={clsx(props.className)} />
|
|
|
|
</div>
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|