2022-08-28 20:20:21 +00:00
|
|
|
import { useMemo } from 'react'
|
2022-06-06 17:36:59 +00:00
|
|
|
import { notification_subscribe_types, PrivateUser } from 'common/user'
|
|
|
|
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-08-28 20:20:21 +00:00
|
|
|
getNotificationsQuery(privateUser.id),
|
|
|
|
{ subscribe: true, includeMetadataChanges: true },
|
|
|
|
// Temporary workaround for react-query bug:
|
|
|
|
// https://github.com/invertase/react-query-firebase/issues/25
|
|
|
|
{ cacheTime: 0 }
|
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-07-19 19:58:51 +00:00
|
|
|
return getAppropriateNotifications(
|
2022-07-07 20:41:50 +00:00
|
|
|
notifications,
|
|
|
|
privateUser.notificationPreferences
|
|
|
|
).filter((n) => !n.isSeenOnHref)
|
2022-08-28 20:20:21 +00:00
|
|
|
}, [privateUser.notificationPreferences, result.data])
|
|
|
|
|
|
|
|
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-22 05:22:49 +00:00
|
|
|
const incomeSourceTypes = ['bonus', 'tip', 'loan', 'betting_streak_bonus']
|
|
|
|
|
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,
|
|
|
|
isSeen: notificationsForContractId[0].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
|
|
|
|
}
|
|
|
|
|
2022-06-06 17:36:59 +00:00
|
|
|
const lessPriorityReasons = [
|
|
|
|
'on_contract_with_users_comment',
|
|
|
|
'on_contract_with_users_answer',
|
2022-08-24 16:49:53 +00:00
|
|
|
// Notifications not currently generated for users who've sold their shares
|
2022-06-06 17:36:59 +00:00
|
|
|
'on_contract_with_users_shares_out',
|
|
|
|
// Not sure if users will want to see these w/ less:
|
|
|
|
// 'on_contract_with_users_shares_in',
|
|
|
|
]
|
|
|
|
|
|
|
|
function getAppropriateNotifications(
|
|
|
|
notifications: Notification[],
|
|
|
|
notificationPreferences?: notification_subscribe_types
|
|
|
|
) {
|
|
|
|
if (notificationPreferences === 'all') return notifications
|
|
|
|
if (notificationPreferences === 'less')
|
|
|
|
return notifications.filter(
|
|
|
|
(n) =>
|
|
|
|
n.reason &&
|
2022-07-01 13:47:19 +00:00
|
|
|
// Show all contract notifications and any that aren't in the above list:
|
2022-06-06 17:36:59 +00:00
|
|
|
(n.sourceType === 'contract' || !lessPriorityReasons.includes(n.reason))
|
|
|
|
)
|
|
|
|
if (notificationPreferences === 'none') return []
|
|
|
|
|
|
|
|
return notifications
|
|
|
|
}
|