2022-06-06 17:36:59 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import { listenForPrivateUser } from 'web/lib/firebase/users'
|
|
|
|
import { notification_subscribe_types, PrivateUser } from 'common/user'
|
|
|
|
import { Notification } from 'common/notification'
|
|
|
|
import { listenForNotifications } from 'web/lib/firebase/notifications'
|
2022-06-06 22:15:36 +00:00
|
|
|
import { groupBy, map } from 'lodash'
|
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
|
|
|
}
|
|
|
|
|
|
|
|
export function usePreferredGroupedNotifications(
|
|
|
|
userId: string | undefined,
|
|
|
|
options: { unseenOnly: boolean }
|
|
|
|
) {
|
|
|
|
const [notificationGroups, setNotificationGroups] = useState<
|
2022-06-08 14:43:24 +00:00
|
|
|
NotificationGroup[] | undefined
|
|
|
|
>(undefined)
|
2022-06-06 22:15:36 +00:00
|
|
|
|
|
|
|
const notifications = usePreferredNotifications(userId, options)
|
|
|
|
useEffect(() => {
|
|
|
|
if (!notifications) return
|
|
|
|
|
|
|
|
const groupedNotifications = groupNotifications(notifications)
|
|
|
|
setNotificationGroups(groupedNotifications)
|
|
|
|
}, [notifications])
|
|
|
|
|
|
|
|
return notificationGroups
|
|
|
|
}
|
|
|
|
|
|
|
|
export function groupNotifications(notifications: Notification[]) {
|
|
|
|
let notificationGroups: NotificationGroup[] = []
|
|
|
|
const notificationGroupsByDay = groupBy(notifications, (notification) =>
|
|
|
|
new Date(notification.createdTime).toDateString()
|
|
|
|
)
|
|
|
|
Object.keys(notificationGroupsByDay).forEach((day) => {
|
2022-07-05 17:29:26 +00:00
|
|
|
const notificationsGroupedByDay = notificationGroupsByDay[day]
|
2022-07-06 19:30:51 +00:00
|
|
|
const incomeNotifications = notificationsGroupedByDay.filter(
|
|
|
|
(notification) =>
|
|
|
|
notification.sourceType === 'bonus' || notification.sourceType === 'tip'
|
2022-07-05 17:29:26 +00:00
|
|
|
)
|
|
|
|
const normalNotificationsGroupedByDay = notificationsGroupedByDay.filter(
|
2022-07-06 19:30:51 +00:00
|
|
|
(notification) =>
|
|
|
|
notification.sourceType !== 'bonus' && notification.sourceType !== 'tip'
|
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-05 23:18:37 +00:00
|
|
|
export function usePreferredNotifications(
|
2022-06-06 17:36:59 +00:00
|
|
|
userId: string | undefined,
|
2022-07-05 23:18:37 +00:00
|
|
|
options: { unseenOnly: boolean; customHref?: string }
|
2022-06-06 17:36:59 +00:00
|
|
|
) {
|
2022-07-05 23:18:37 +00:00
|
|
|
const { unseenOnly, customHref } = options
|
2022-06-06 17:36:59 +00:00
|
|
|
const [privateUser, setPrivateUser] = useState<PrivateUser | null>(null)
|
|
|
|
const [notifications, setNotifications] = useState<Notification[]>([])
|
|
|
|
const [userAppropriateNotifications, setUserAppropriateNotifications] =
|
|
|
|
useState<Notification[]>([])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (userId) listenForPrivateUser(userId, setPrivateUser)
|
|
|
|
}, [userId])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (privateUser)
|
|
|
|
return listenForNotifications(
|
|
|
|
privateUser.id,
|
|
|
|
setNotifications,
|
|
|
|
unseenOnly
|
|
|
|
)
|
2022-06-06 22:15:36 +00:00
|
|
|
}, [privateUser, unseenOnly])
|
2022-06-06 17:36:59 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!privateUser) return
|
|
|
|
|
|
|
|
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-05 23:18:37 +00:00
|
|
|
}, [privateUser, notifications, customHref])
|
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
|
|
|
|
}
|