2022-07-19 15:04:47 +00:00
|
|
|
import { useEffect, useMemo, useState } from 'react'
|
2022-06-06 17:36:59 +00:00
|
|
|
import { notification_subscribe_types, PrivateUser } from 'common/user'
|
|
|
|
import { Notification } from 'common/notification'
|
2022-07-07 20:41:50 +00:00
|
|
|
import {
|
|
|
|
getNotificationsQuery,
|
|
|
|
listenForNotifications,
|
|
|
|
} 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-07-07 20:41:50 +00:00
|
|
|
import { NOTIFICATIONS_PER_PAGE } from 'web/pages/notifications'
|
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-07-07 20:41:50 +00:00
|
|
|
// For some reason react-query subscriptions don't actually listen for notifications
|
|
|
|
// Use useUnseenPreferredNotificationGroups to listen for new notifications
|
2022-07-19 15:04:47 +00:00
|
|
|
export function usePreferredGroupedNotifications(
|
|
|
|
privateUser: PrivateUser,
|
|
|
|
cachedNotifications?: Notification[]
|
|
|
|
) {
|
|
|
|
const result = useFirestoreQueryData(
|
|
|
|
['notifications-all', privateUser.id],
|
|
|
|
getNotificationsQuery(privateUser.id)
|
|
|
|
)
|
|
|
|
const notifications = useMemo(() => {
|
|
|
|
if (result.isLoading) return cachedNotifications ?? []
|
|
|
|
if (!result.data) return cachedNotifications ?? []
|
|
|
|
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-07-19 15:04:47 +00:00
|
|
|
}, [
|
|
|
|
cachedNotifications,
|
|
|
|
privateUser.notificationPreferences,
|
|
|
|
result.data,
|
|
|
|
result.isLoading,
|
|
|
|
])
|
|
|
|
|
|
|
|
return useMemo(() => {
|
|
|
|
if (notifications) return groupNotifications(notifications)
|
2022-06-06 22:15:36 +00:00
|
|
|
}, [notifications])
|
|
|
|
}
|
|
|
|
|
2022-07-07 20:41:50 +00:00
|
|
|
export function useUnseenPreferredNotificationGroups(privateUser: PrivateUser) {
|
|
|
|
const notifications = useUnseenPreferredNotifications(privateUser, {})
|
|
|
|
const [notificationGroups, setNotificationGroups] = useState<
|
|
|
|
NotificationGroup[] | undefined
|
|
|
|
>(undefined)
|
|
|
|
useEffect(() => {
|
|
|
|
if (!notifications) return
|
|
|
|
|
|
|
|
const groupedNotifications = groupNotifications(notifications)
|
|
|
|
setNotificationGroups(groupedNotifications)
|
|
|
|
}, [notifications])
|
|
|
|
return notificationGroups
|
|
|
|
}
|
|
|
|
|
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-07-07 20:41:50 +00:00
|
|
|
export function useUnseenPreferredNotifications(
|
|
|
|
privateUser: PrivateUser,
|
|
|
|
options: { customHref?: string },
|
|
|
|
limit: number = NOTIFICATIONS_PER_PAGE
|
2022-06-06 17:36:59 +00:00
|
|
|
) {
|
2022-07-07 20:41:50 +00:00
|
|
|
const { customHref } = options
|
2022-06-06 17:36:59 +00:00
|
|
|
const [notifications, setNotifications] = useState<Notification[]>([])
|
|
|
|
const [userAppropriateNotifications, setUserAppropriateNotifications] =
|
|
|
|
useState<Notification[]>([])
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-07-07 20:41:50 +00:00
|
|
|
return listenForNotifications(privateUser.id, setNotifications, {
|
|
|
|
unseenOnly: true,
|
|
|
|
limit,
|
|
|
|
})
|
|
|
|
}, [limit, privateUser.id])
|
2022-06-06 17:36:59 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const notificationsToShow = getAppropriateNotifications(
|
|
|
|
notifications,
|
|
|
|
privateUser.notificationPreferences
|
2022-07-05 23:18:37 +00:00
|
|
|
).filter((n) =>
|
|
|
|
customHref ? n.isSeenOnHref?.includes(customHref) : !n.isSeenOnHref
|
2022-06-06 17:36:59 +00:00
|
|
|
)
|
|
|
|
setUserAppropriateNotifications(notificationsToShow)
|
2022-07-07 20:41:50 +00:00
|
|
|
}, [notifications, customHref, privateUser.notificationPreferences])
|
2022-06-06 17:36:59 +00:00
|
|
|
|
|
|
|
return userAppropriateNotifications
|
|
|
|
}
|
|
|
|
|
|
|
|
const lessPriorityReasons = [
|
|
|
|
'on_contract_with_users_comment',
|
|
|
|
'on_contract_with_users_answer',
|
|
|
|
'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
|
|
|
|
}
|