Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions apps/meteor/client/hooks/useUnread.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { manageFavicon } from '@rocket.chat/favicon';
import { useSession, useSessionDispatch, useUserPreference, useUserSubscriptions } from '@rocket.chat/ui-contexts';
import { useEffect } from 'react';

import { useFireGlobalEvent } from './useFireGlobalEvent';
import { Rooms } from '../../app/models/client';

const query = { open: { $ne: false }, hideUnreadStatus: { $ne: true }, archived: { $ne: true } };
const options = { fields: { unread: 1, alert: 1, rid: 1, t: 1, name: 1, ls: 1, unreadAlert: 1, fname: 1, prid: 1 } };

export const useUnread = () => {
const unreadAlertEnabled = useUserPreference('unreadAlert');
const setUnread = useSessionDispatch('unread');
const unread = useSession('unread') as string | number | null | undefined;

const { mutate: fireEventUnreadChangedBySubscription } = useFireGlobalEvent('unread-changed-by-subscription');
const { mutate: fireEventUnreadChanged } = useFireGlobalEvent('unread-changed');

const subscriptions = useUserSubscriptions(query, options);

useEffect(() => {
let unreadAlert: false | '•' = false;

const unreadCount = subscriptions.reduce((ret, subscription) => {
const room = Rooms.findOne({ _id: subscription.rid }, { fields: { usersCount: 1 } });
fireEventUnreadChangedBySubscription({
...subscription,
usersCount: room?.usersCount,
});

if (subscription.alert || subscription.unread > 0) {
// Increment the total unread count.
if (subscription.alert === true && subscription.unreadAlert !== 'nothing') {
if (subscription.unreadAlert === 'all' || unreadAlertEnabled !== false) {
unreadAlert = '•';
}
}
return ret + subscription.unread;
Comment thread
juliajforesti marked this conversation as resolved.
}
return ret;
}, 0);

if (unreadCount > 0) {
if (unreadCount > 999) {
setUnread('999+');
} else {
setUnread(unreadCount);
}
} else if (unreadAlert !== false) {
setUnread(unreadAlert);
} else {
setUnread('');
}
fireEventUnreadChanged(unreadCount);
}, [setUnread, unread, subscriptions, unreadAlertEnabled, fireEventUnreadChangedBySubscription, fireEventUnreadChanged]);

useEffect(() => {
const updateFavicon = manageFavicon();
updateFavicon(unread);
}, [unread]);
};
1 change: 0 additions & 1 deletion apps/meteor/client/startup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ import './routes';
import './slashCommands';
import './startup';
import './streamMessage';
import './unread';
85 changes: 0 additions & 85 deletions apps/meteor/client/startup/unread.ts

This file was deleted.

2 changes: 2 additions & 0 deletions apps/meteor/client/views/root/MainLayout/LoggedInArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useCustomEmoji } from '../../../hooks/customEmoji/useCustomEmoji';
import { useNotificationUserCalendar } from '../../../hooks/notification/useNotificationUserCalendar';
import { useNotifyUser } from '../../../hooks/notification/useNotifyUser';
import { useRestrictedRoles } from '../../../hooks/useRestrictedRoles';
import { useUnread } from '../../../hooks/useUnread';
import { useForceLogout } from '../hooks/useForceLogout';
import { useOTRMessaging } from '../hooks/useOTRMessaging';
import { useStoreCookiesOnLogin } from '../hooks/useStoreCookiesOnLogin';
Expand All @@ -18,6 +19,7 @@ const LoggedInArea = ({ children }: { children: ReactNode }) => {
throw new Error('User not logged');
}

useUnread();
useNotifyUser(user);
useUpdateVideoConfUser(user._id);
useWebRTC(user._id);
Expand Down