Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace useCallback with useEffectEvent #35630

Merged
merged 1 commit into from
Mar 26, 2025
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
45 changes: 21 additions & 24 deletions apps/meteor/client/hooks/notification/useDesktopNotification.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { INotificationDesktop } from '@rocket.chat/core-typings';
import { useEffectEvent } from '@rocket.chat/fuselage-hooks';
import { useUser } from '@rocket.chat/ui-contexts';
import { useCallback } from 'react';

import { useNotification } from './useNotification';
import { e2e } from '../../../app/e2e/client';
Expand All @@ -11,32 +11,29 @@ export const useDesktopNotification = () => {
const user = useUser();
const notify = useNotification();

const notifyDesktop = useCallback(
async (notification: INotificationDesktop) => {
if (
notification.payload.rid === RoomManager.opened &&
(typeof window.document.hasFocus === 'function' ? window.document.hasFocus() : undefined)
) {
return;
}
if (user?.status === 'busy') {
return;
}
const notifyDesktop = useEffectEvent(async (notification: INotificationDesktop) => {
if (
notification.payload.rid === RoomManager.opened &&
(typeof window.document.hasFocus === 'function' ? window.document.hasFocus() : undefined)
) {
return;
}
if (user?.status === 'busy') {
return;
}

if (notification.payload.message?.t === 'e2e') {
const e2eRoom = await e2e.getInstanceByRoomId(notification.payload.rid);
if (e2eRoom) {
notification.text = (await e2eRoom.decrypt(notification.payload.message.msg)).text;
}
if (notification.payload.message?.t === 'e2e') {
const e2eRoom = await e2e.getInstanceByRoomId(notification.payload.rid);
if (e2eRoom) {
notification.text = (await e2eRoom.decrypt(notification.payload.message.msg)).text;
}
}

return getAvatarAsPng(notification.payload.sender?.username, (avatarAsPng) => {
notification.icon = avatarAsPng;
return notify(notification);
});
},
[notify, user?.status],
);
return getAvatarAsPng(notification.payload.sender?.username, (avatarAsPng) => {
notification.icon = avatarAsPng;
return notify(notification);
});
});

return notifyDesktop;
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AtLeast, ISubscription } from '@rocket.chat/core-typings';
import { useEffectEvent } from '@rocket.chat/fuselage-hooks';
import { useUserPreference } from '@rocket.chat/ui-contexts';
import { useCallback } from 'react';

import { CustomSounds } from '../../../app/custom-sounds/client/lib/CustomSounds';
import { useUserSoundPreferences } from '../useUserSoundPreferences';
Expand All @@ -9,24 +9,21 @@ export const useNewMessageNotification = () => {
const newMessageNotification = useUserPreference<string>('newMessageNotification');
const { notificationsSoundVolume } = useUserSoundPreferences();

const notifyNewMessage = useCallback(
(sub: AtLeast<ISubscription, 'rid'>) => {
if (!sub || sub.audioNotificationValue === 'none') {
return;
}
if (sub.audioNotificationValue && sub.audioNotificationValue !== '0') {
void CustomSounds.play(sub.audioNotificationValue, {
volume: Number((notificationsSoundVolume / 100).toPrecision(2)),
});
}
const notifyNewMessage = useEffectEvent((sub: AtLeast<ISubscription, 'rid'>) => {
if (!sub || sub.audioNotificationValue === 'none') {
return;
}
if (sub.audioNotificationValue && sub.audioNotificationValue !== '0') {
void CustomSounds.play(sub.audioNotificationValue, {
volume: Number((notificationsSoundVolume / 100).toPrecision(2)),
});
}

if (newMessageNotification && newMessageNotification !== 'none') {
void CustomSounds.play(newMessageNotification, {
volume: Number((notificationsSoundVolume / 100).toPrecision(2)),
});
}
},
[newMessageNotification, notificationsSoundVolume],
);
if (newMessageNotification && newMessageNotification !== 'none') {
void CustomSounds.play(newMessageNotification, {
volume: Number((notificationsSoundVolume / 100).toPrecision(2)),
});
}
});
return notifyNewMessage;
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffectEvent } from '@rocket.chat/fuselage-hooks';
import { useUserPreference } from '@rocket.chat/ui-contexts';
import { useCallback } from 'react';

import { CustomSounds } from '../../../app/custom-sounds/client/lib/CustomSounds';
import { useUserSoundPreferences } from '../useUserSoundPreferences';
Expand All @@ -8,15 +8,15 @@ export const useNewRoomNotification = () => {
const newRoomNotification = useUserPreference<string>('newRoomNotification');
const { notificationsSoundVolume } = useUserSoundPreferences();

const notifyNewRoom = useCallback(() => {
const notifyNewRoom = useEffectEvent(() => {
if (!newRoomNotification) {
return;
}

void CustomSounds.play(newRoomNotification, {
volume: Number((notificationsSoundVolume / 100).toPrecision(2)),
});
}, [newRoomNotification, notificationsSoundVolume]);
});

return notifyNewRoom;
};
199 changes: 98 additions & 101 deletions apps/meteor/client/hooks/notification/useNotification.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { INotificationDesktop } from '@rocket.chat/core-typings';
import { useEffectEvent } from '@rocket.chat/fuselage-hooks';
import { Random } from '@rocket.chat/random';
import { useRouter, useUserPreference } from '@rocket.chat/ui-contexts';
import { useCallback } from 'react';

import { useNotificationAllowed } from './useNotificationAllowed';
import { getUserAvatarURL } from '../../../app/utils/client';
Expand All @@ -14,111 +14,108 @@ export const useNotification = () => {
const router = useRouter();
const notificationAllowed = useNotificationAllowed();

const notify = useCallback(
async (notification: INotificationDesktop) => {
if (!notificationAllowed) {
return;
}
if (!notification.payload) {
return;
}
const notify = useEffectEvent(async (notification: INotificationDesktop) => {
if (!notificationAllowed) {
return;
}
if (!notification.payload) {
return;
}

const { rid } = notification.payload;
if (!rid) {
return;
}
const message = await onClientMessageReceived({
rid,
msg: notification.text,
notification: true,
} as any);
const { rid } = notification.payload;
if (!rid) {
return;
}
const message = await onClientMessageReceived({
rid,
msg: notification.text,
notification: true,
} as any);

const n = new Notification(notification.title, {
icon: notification.icon || getUserAvatarURL(notification.payload.sender?.username as string),
body: stripTags(message?.msg),
tag: notification.payload._id,
canReply: true,
silent: true,
requireInteraction,
} as NotificationOptions & {
canReply?: boolean;
});
const notificationDuration = !requireInteraction ? (notification.duration ?? 0) - 0 || 10 : -1;
if (notificationDuration > 0) {
setTimeout(() => n.close(), notificationDuration * 1000);
}
const n = new Notification(notification.title, {
icon: notification.icon || getUserAvatarURL(notification.payload.sender?.username as string),
body: stripTags(message?.msg),
tag: notification.payload._id,
canReply: true,
silent: true,
requireInteraction,
} as NotificationOptions & {
canReply?: boolean;
});
const notificationDuration = !requireInteraction ? (notification.duration ?? 0) - 0 || 10 : -1;
if (notificationDuration > 0) {
setTimeout(() => n.close(), notificationDuration * 1000);
}

if (n.addEventListener) {
n.addEventListener(
'reply',
({ response }) =>
void sdk.call('sendMessage', {
_id: Random.id(),
rid,
msg: response,
}),
);
}
if (n.addEventListener) {
n.addEventListener(
'reply',
({ response }) =>
void sdk.call('sendMessage', {
_id: Random.id(),
rid,
msg: response,
}),
);
}

n.onclick = () => {
n.close();
window.focus();
n.onclick = () => {
n.close();
window.focus();

if (!notification.payload._id || !notification.payload.rid || !notification.payload.name) {
return;
}
if (!notification.payload._id || !notification.payload.rid || !notification.payload.name) {
return;
}

switch (notification.payload?.type) {
case 'd':
router.navigate({
pattern: '/direct/:rid/:tab?/:context?',
params: {
rid: notification.payload.rid,
...(notification.payload.tmid && {
tab: 'thread',
context: notification.payload.tmid,
}),
},
search: { ...router.getSearchParameters(), jump: notification.payload._id },
});
break;
case 'c':
return router.navigate({
pattern: '/channel/:name/:tab?/:context?',
params: {
name: notification.payload.name,
...(notification.payload.tmid && {
tab: 'thread',
context: notification.payload.tmid,
}),
},
search: { ...router.getSearchParameters(), jump: notification.payload._id },
});
case 'p':
return router.navigate({
pattern: '/group/:name/:tab?/:context?',
params: {
name: notification.payload.name,
...(notification.payload.tmid && {
tab: 'thread',
context: notification.payload.tmid,
}),
},
search: { ...router.getSearchParameters(), jump: notification.payload._id },
});
case 'l':
return router.navigate({
pattern: '/live/:id/:tab?/:context?',
params: {
id: notification.payload.rid,
tab: 'room-info',
},
search: { ...router.getSearchParameters(), jump: notification.payload._id },
});
}
};
},
[notificationAllowed, requireInteraction, router],
);
switch (notification.payload?.type) {
case 'd':
router.navigate({
pattern: '/direct/:rid/:tab?/:context?',
params: {
rid: notification.payload.rid,
...(notification.payload.tmid && {
tab: 'thread',
context: notification.payload.tmid,
}),
},
search: { ...router.getSearchParameters(), jump: notification.payload._id },
});
break;
case 'c':
return router.navigate({
pattern: '/channel/:name/:tab?/:context?',
params: {
name: notification.payload.name,
...(notification.payload.tmid && {
tab: 'thread',
context: notification.payload.tmid,
}),
},
search: { ...router.getSearchParameters(), jump: notification.payload._id },
});
case 'p':
return router.navigate({
pattern: '/group/:name/:tab?/:context?',
params: {
name: notification.payload.name,
...(notification.payload.tmid && {
tab: 'thread',
context: notification.payload.tmid,
}),
},
search: { ...router.getSearchParameters(), jump: notification.payload._id },
});
case 'l':
return router.navigate({
pattern: '/live/:id/:tab?/:context?',
params: {
id: notification.payload.rid,
tab: 'room-info',
},
search: { ...router.getSearchParameters(), jump: notification.payload._id },
});
}
};
});
return notify;
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useCallback } from 'react';
import { useEffectEvent } from '@rocket.chat/fuselage-hooks';

import { notificationManager } from '../../lib/notificationManager';

export const useNotificationPermission = () => {
const requestPermission = useCallback(async () => {
const requestPermission = useEffectEvent(async () => {
const response = await Notification.requestPermission();
notificationManager.allowed = response === 'granted';
notificationManager.emit('change');
Expand All @@ -13,7 +13,7 @@ export const useNotificationPermission = () => {
notificationManager.allowed = notifications.state === 'granted';
notificationManager.emit('change');
};
}, []);
});

if ('Notification' in window) {
requestPermission();
Expand Down
Loading