2022-08-28 20:20:21 +00:00
|
|
|
import { useMemo } from 'react'
|
2022-09-12 16:34:56 +00:00
|
|
|
import { PrivateUser } from 'common/user'
|
2022-06-06 17:36:59 +00:00
|
|
|
import { Notification } from 'common/notification'
|
2022-08-28 20:20:21 +00:00
|
|
|
import { getNotificationsQuery } from 'web/lib/firebase/notifications'
|
2022-08-22 05:22:49 +00:00
|
|
|
import { groupBy, map, partition } from 'lodash'
|
2022-07-19 15:04:47 +00:00
|
|
|
import { useFirestoreQueryData } from '@react-query-firebase/firestore'
|
2022-06-06 17:36:59 +00:00
|
|
|
|
2022-06-06 22:15:36 +00:00
|
|
|
export type NotificationGroup = {
|
|
|
|
notifications: Notification[]
|
2022-07-05 17:29:26 +00:00
|
|
|
groupedById: string
|
2022-06-06 22:15:36 +00:00
|
|
|
isSeen: boolean
|
|
|
|
timePeriod: string
|
2022-07-05 17:29:26 +00:00
|
|
|
type: 'income' | 'normal'
|
2022-06-06 22:15:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 20:20:21 +00:00
|
|
|
function useNotifications(privateUser: PrivateUser) {
|
2022-07-19 15:04:47 +00:00
|
|
|
const result = useFirestoreQueryData(
|
|
|
|
['notifications-all', privateUser.id],
|
2022-09-03 03:51:33 +00:00
|
|
|
getNotificationsQuery(privateUser.id)
|
2022-07-19 15:04:47 +00:00
|
|
|
)
|
2022-08-28 23:03:00 +00:00
|
|
|
|
2022-07-19 15:04:47 +00:00
|
|
|
const notifications = useMemo(() => {
|
2022-08-28 20:20:21 +00:00
|
|
|
if (!result.data) return undefined
|
2022-07-19 15:04:47 +00:00
|
|
|
const notifications = result.data as Notification[]
|
2022-07-07 20:41:50 +00:00
|
|
|
|
2022-09-12 16:34:56 +00:00
|
|
|
return notifications.filter((n) => !n.isSeenOnHref)
|
|
|
|
}, [result.data])
|
2022-08-28 20:20:21 +00:00
|
|
|
|
|
|
|
return notifications
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useUnseenNotifications(privateUser: PrivateUser) {
|
|
|
|
const notifications = useNotifications(privateUser)
|
|
|
|
return useMemo(
|
|
|
|
() => notifications && notifications.filter((n) => !n.isSeen),
|
|
|
|
[notifications]
|
|
|
|
)
|
|
|
|
}
|
2022-07-19 15:04:47 +00:00
|
|
|
|
2022-08-28 20:20:21 +00:00
|
|
|
export function useGroupedNotifications(privateUser: PrivateUser) {
|
|
|
|
const notifications = useNotifications(privateUser)
|
2022-07-19 15:04:47 +00:00
|
|
|
return useMemo(() => {
|
|
|
|
if (notifications) return groupNotifications(notifications)
|
2022-06-06 22:15:36 +00:00
|
|
|
}, [notifications])
|
|
|
|
}
|
|
|
|
|
2022-08-28 20:20:21 +00:00
|
|
|
export function useUnseenGroupedNotification(privateUser: PrivateUser) {
|
|
|
|
const notifications = useUnseenNotifications(privateUser)
|
|
|
|
return useMemo(() => {
|
|
|
|
if (notifications) return groupNotifications(notifications)
|
2022-07-07 20:41:50 +00:00
|
|
|
}, [notifications])
|
|
|
|
}
|
|
|
|
|
2022-06-06 22:15:36 +00:00
|
|
|
export function groupNotifications(notifications: Notification[]) {
|
|
|
|
let notificationGroups: NotificationGroup[] = []
|
|
|
|
const notificationGroupsByDay = groupBy(notifications, (notification) =>
|
|
|
|
new Date(notification.createdTime).toDateString()
|
|
|
|
)
|
2022-08-30 15:38:59 +00:00
|
|
|
const incomeSourceTypes = [
|
|
|
|
'bonus',
|
|
|
|
'tip',
|
|
|
|
'loan',
|
|
|
|
'betting_streak_bonus',
|
|
|
|
'tip_and_like',
|
|
|
|
]
|
2022-08-22 05:22:49 +00:00
|
|
|
|
2022-06-06 22:15:36 +00:00
|
|
|
Object.keys(notificationGroupsByDay).forEach((day) => {
|
2022-07-05 17:29:26 +00:00
|
|
|
const notificationsGroupedByDay = notificationGroupsByDay[day]
|
2022-08-22 05:22:49 +00:00
|
|
|
const [incomeNotifications, normalNotificationsGroupedByDay] = partition(
|
|
|
|
notificationsGroupedByDay,
|
2022-07-06 19:30:51 +00:00
|
|
|
(notification) =>
|
2022-08-22 05:22:49 +00:00
|
|
|
incomeSourceTypes.includes(notification.sourceType ?? '')
|
2022-07-05 17:29:26 +00:00
|
|
|
)
|
2022-07-06 19:30:51 +00:00
|
|
|
if (incomeNotifications.length > 0) {
|
2022-07-05 17:29:26 +00:00
|
|
|
notificationGroups = notificationGroups.concat({
|
2022-07-06 19:30:51 +00:00
|
|
|
notifications: incomeNotifications,
|
2022-07-05 17:29:26 +00:00
|
|
|
groupedById: 'income' + day,
|
2022-07-06 19:30:51 +00:00
|
|
|
isSeen: incomeNotifications[0].isSeen,
|
2022-07-05 17:29:26 +00:00
|
|
|
timePeriod: day,
|
|
|
|
type: 'income',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// Group notifications by contract, filtering out bonuses:
|
2022-06-06 22:15:36 +00:00
|
|
|
const groupedNotificationsByContractId = groupBy(
|
2022-07-05 17:29:26 +00:00
|
|
|
normalNotificationsGroupedByDay,
|
2022-06-06 22:15:36 +00:00
|
|
|
(notification) => {
|
|
|
|
return notification.sourceContractId
|
|
|
|
}
|
|
|
|
)
|
|
|
|
notificationGroups = notificationGroups.concat(
|
|
|
|
map(groupedNotificationsByContractId, (notifications, contractId) => {
|
2022-07-05 17:29:26 +00:00
|
|
|
const notificationsForContractId = groupedNotificationsByContractId[
|
|
|
|
contractId
|
|
|
|
].sort((a, b) => {
|
|
|
|
return b.createdTime - a.createdTime
|
|
|
|
})
|
2022-06-06 22:15:36 +00:00
|
|
|
// Create a notification group for each contract within each day
|
|
|
|
const notificationGroup: NotificationGroup = {
|
2022-07-05 17:29:26 +00:00
|
|
|
notifications: notificationsForContractId,
|
|
|
|
groupedById: contractId,
|
2022-09-16 15:02:58 +00:00
|
|
|
isSeen: notificationsForContractId.some((n) => !n.isSeen),
|
2022-06-06 22:15:36 +00:00
|
|
|
timePeriod: day,
|
2022-07-05 17:29:26 +00:00
|
|
|
type: 'normal',
|
2022-06-06 22:15:36 +00:00
|
|
|
}
|
|
|
|
return notificationGroup
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
|
|
|
return notificationGroups
|
|
|
|
}
|