mirror of
https://github.com/cinnyapp/cinny.git
synced 2026-12-02 14:52:45 -08:00
update favicon do display unread/notification and play invite sound
This commit is contained in:
1 parent
5521490f1c
commit
21f29bbd6c
1 file changed
+87
-1
@@ -7,8 +7,21 @@ https://cinny.in
|
||||
Please see LICENSE file in root of repository for full details.
|
||||
*/
|
||||
|
||||
import React from 'react';
|
||||
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useSetting } from '../../state/hooks/settings';
|
||||
import { unreadRoomsAtom } from '../../state/room/roomToUnread';
|
||||
import { roomToNotificationsAtom } from '../../state/room/roomToNotifications';
|
||||
import { notificationPermission, setFavicon } from '../../utils/dom';
|
||||
import LogoSVG from '../../../../public/res/svg/cinny.svg';
|
||||
import LogoUnreadSVG from '../../../../public/res/svg/cinny-unread.svg';
|
||||
import LogoHighlightSVG from '../../../../public/res/svg/cinny-highlight.svg';
|
||||
import { getRoomsTotalNotifications } from '../../state/hooks/roomNotifications';
|
||||
import { useClient } from '../../hooks/useClient';
|
||||
import { getInboxInvitesPath } from '../pathUtils';
|
||||
import InviteSound from '../../../../public/sound/invite.ogg';
|
||||
import { useSound } from '../../hooks/useSound';
|
||||
|
||||
function SystemEmojiFeature() {
|
||||
const [twitterEmoji] = useSetting('twitterEmoji');
|
||||
@@ -34,11 +47,84 @@ function PageZoomFeature() {
|
||||
return null;
|
||||
}
|
||||
|
||||
function FaviconUpdater() {
|
||||
const unreadRooms = useAtomValue(unreadRoomsAtom);
|
||||
const roomToNotifications = useAtomValue(roomToNotificationsAtom);
|
||||
|
||||
const notificaitonCount = useMemo(() => {
|
||||
const total = getRoomsTotalNotifications(roomToNotifications);
|
||||
return total;
|
||||
}, [roomToNotifications]);
|
||||
|
||||
useEffect(() => {
|
||||
if (unreadRooms || notificaitonCount.count > 0) {
|
||||
setFavicon(notificaitonCount.highlight ? LogoHighlightSVG : LogoUnreadSVG);
|
||||
} else {
|
||||
setFavicon(LogoSVG);
|
||||
}
|
||||
}, [unreadRooms, notificaitonCount]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function InviteNotifications() {
|
||||
const ct = useClient();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const { play } = useSound(InviteSound);
|
||||
|
||||
const prevInviteCount = useRef(0);
|
||||
|
||||
const [showNotifications] = useSetting('showNotifications');
|
||||
const [notificationSound] = useSetting('isNotificationSounds');
|
||||
|
||||
const notify = useCallback(
|
||||
(count: number) => {
|
||||
const noti = new window.Notification('Invitation', {
|
||||
icon: LogoSVG,
|
||||
badge: LogoSVG,
|
||||
body: `You have ${count} new invitation request.`,
|
||||
silent: true,
|
||||
});
|
||||
|
||||
noti.onclick = () => {
|
||||
if (!window.closed) navigate(getInboxInvitesPath());
|
||||
noti.close();
|
||||
};
|
||||
},
|
||||
[navigate]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const unsub = ct.rooms.invite.subscribe((invites) => {
|
||||
const prevCount = prevInviteCount.current;
|
||||
|
||||
if (invites.length > prevCount) {
|
||||
if (showNotifications && notificationPermission('granted')) {
|
||||
notify(invites.length - prevCount);
|
||||
}
|
||||
|
||||
if (notificationSound) {
|
||||
play();
|
||||
}
|
||||
}
|
||||
|
||||
prevInviteCount.current = invites.length;
|
||||
});
|
||||
|
||||
return unsub;
|
||||
}, [ct, showNotifications, notificationSound, notify, play]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function ClientNonUIFeatures() {
|
||||
return (
|
||||
<>
|
||||
<SystemEmojiFeature />
|
||||
<PageZoomFeature />
|
||||
<FaviconUpdater />
|
||||
<InviteNotifications />
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user