2022-06-06 17:36:59 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
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-06-06 22:15:36 +00:00
|
|
|
import { groupBy, map } from 'lodash'
|
2022-07-07 20:41:50 +00:00
|
|
|
import { useFirestoreQuery } from '@react-query-firebase/firestore'
|
|
|
|
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
|
|
|
|
export function usePreferredGroupedNotifications(privateUser: PrivateUser) {
|
2022-06-06 22:15:36 +00:00
|
|
|
const [notificationGroups, setNotificationGroups] = useState<
|
2022-06-08 14:43:24 +00:00
|
|
|
NotificationGroup[] | undefined
|
|
|
|
>(undefined)
|
2022-07-07 20:41:50 +00:00
|
|
|
const [notifications, setNotifications] = useState<Notification[]>([])
|
|
|
|
const key = `notifications-${privateUser.id}-all`
|
|
|
|
|
|
|
|
const result = useFirestoreQuery([key], getNotificationsQuery(privateUser.id))
|
|
|
|
useEffect(() => {
|
|
|
|
if (result.isLoading) return
|
|
|
|
if (!result.data) return setNotifications([])
|
|
|
|
const notifications = result.data.docs.map(
|
|
|
|
(doc) => doc.data() as Notification
|
|
|
|
)
|
|
|
|
|
|
|
|
const notificationsToShow = getAppropriateNotifications(
|
|
|
|
notifications,
|
|
|
|
privateUser.notificationPreferences
|
|
|
|
).filter((n) => !n.isSeenOnHref)
|
|
|
|
setNotifications(notificationsToShow)
|
|
|
|
}, [privateUser.notificationPreferences, result.data, result.isLoading])
|
2022-06-06 22:15:36 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!notifications) return
|
|
|
|
|
|
|
|
const groupedNotifications = groupNotifications(notifications)
|
|
|
|
setNotificationGroups(groupedNotifications)
|
|
|
|
}, [notifications])
|
|
|
|
|
|
|
|
return notificationGroups
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
)
|
|
|
|
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-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
|
|
|
|
}
|