manifold/web/hooks/use-notifications.ts
Ian Philips 37c7f909a3
Notification detail, grouping, and settings control [wip] (#403)
* Revert "Revert "Notifications ux fixes - wip (#383)""

This reverts commit 699b03eb42.

* Group & provide more control over notification display

* UI/UX improvements

* Remove unused text key

* Refactor

* Refactor

* Show answer resolution in notification

* Disable eslint on single linefor exhaustive deps

* Handle arbritrary notifications

* Refactor

* Remove unused vars

* Add follow user

* Various UX improvements, add follow notif

* Various small ui changes

* Show notification settings breakdown

* Improve notification status lines
2022-06-06 10:52:11 -06:00

67 lines
2.0 KiB
TypeScript

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'
export function useNotifications(
userId: string | undefined,
options: { unseenOnly: boolean }
) {
const { unseenOnly } = options
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
)
}, [privateUser])
useEffect(() => {
if (!privateUser) return
const notificationsToShow = getAppropriateNotifications(
notifications,
privateUser.notificationPreferences
)
setUserAppropriateNotifications(notificationsToShow)
}, [privateUser, notifications])
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 &&
// Show all contract notifications
(n.sourceType === 'contract' || !lessPriorityReasons.includes(n.reason))
)
if (notificationPreferences === 'none') return []
return notifications
}