diff --git a/web/components/notifications-icon.tsx b/web/components/notifications-icon.tsx index 95310de4..cc892ed9 100644 --- a/web/components/notifications-icon.tsx +++ b/web/components/notifications-icon.tsx @@ -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]) diff --git a/web/hooks/use-notifications.ts b/web/hooks/use-notifications.ts index 8e68fdcb..129efa18 100644 --- a/web/hooks/use-notifications.ts +++ b/web/hooks/use-notifications.ts @@ -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([]) const [userAppropriateNotifications, setUserAppropriateNotifications] = useState([]) - 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 ) { diff --git a/web/lib/firebase/utils.ts b/web/lib/firebase/utils.ts index 38bcb493..e63c2d96 100644 --- a/web/lib/firebase/utils.ts +++ b/web/lib/firebase/utils.ts @@ -39,13 +39,12 @@ export function listenForValue( export function listenForValues( 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)