This commit is contained in:
Ian Philips 2022-07-07 09:43:59 -06:00
parent df8a5fff8b
commit 8b18cd1a26
3 changed files with 13 additions and 16 deletions

View File

@ -15,14 +15,12 @@ export default function NotificationsIcon(props: { className?: string }) {
useEffect(() => {
if (user) {
const bonusChecker = setTimeout(() => {
requestBonuses({}).catch((error) => {
console.log("couldn't get bonuses:", error.message)
})
const bonusChecker = setInterval(() => {
requestBonuses({})
return () => {
clearInterval(bonusChecker)
}
}, 1000 * 120)
}, 1000 * 60)
}
}, [user])

View File

@ -16,7 +16,8 @@ export type NotificationGroup = {
type: 'income' | 'normal'
}
// This doesn't listen for new notifications, use firebase listener for that
// For some reason react-query subscriptions don't actually listen for notifications
// Use useUnseenPreferredNotificationGroups to listen for new notifications
export function usePreferredGroupedNotifications(privateUser: PrivateUser) {
const [notificationGroups, setNotificationGroups] = useState<
NotificationGroup[] | undefined
@ -26,11 +27,7 @@ export function usePreferredGroupedNotifications(privateUser: PrivateUser) {
const result = useFirestoreQuery(
[key],
getNotificationsQuery(privateUser.id, false),
{
// subscribe: false,
// includeMetadataChanges: true,
}
getNotificationsQuery(privateUser.id, false)
)
useEffect(() => {
if (result.isLoading) return
@ -131,7 +128,10 @@ export function useUnseenPreferredNotifications(
const [notifications, setNotifications] = useState<Notification[]>([])
const [userAppropriateNotifications, setUserAppropriateNotifications] =
useState<Notification[]>([])
listenForNotifications(privateUser.id, setNotifications, true)
useEffect(() => {
return listenForNotifications(privateUser.id, setNotifications, true)
}, [privateUser.id])
useEffect(() => {
const notificationsToShow = getAppropriateNotifications(
@ -154,7 +154,7 @@ const lessPriorityReasons = [
// 'on_contract_with_users_shares_in',
]
export function getAppropriateNotifications(
function getAppropriateNotifications(
notifications: Notification[],
notificationPreferences?: notification_subscribe_types
) {

View File

@ -39,13 +39,12 @@ export function listenForValue<T>(
export function listenForValues<T>(
query: Query,
setValues: (values: T[]) => void,
enableCache?: boolean
setValues: (values: T[]) => void
) {
// Exclude cached snapshots so we only trigger on fresh data.
// includeMetadataChanges ensures listener is called even when server data is the same as cached data.
return onSnapshot(query, { includeMetadataChanges: true }, (snapshot) => {
if (snapshot.metadata.fromCache && !enableCache) return
if (snapshot.metadata.fromCache) return
const values = snapshot.docs.map((doc) => doc.data() as T)
setValues(values)