2022-03-24 06:17:32 -07:00
|
|
|
import * as React from 'react';
|
2022-05-11 10:40:34 -07:00
|
|
|
import { Helmet as ReactHelmet } from 'react-helmet';
|
2022-03-24 06:17:32 -07:00
|
|
|
|
|
|
|
import { useAppSelector, useSettings } from 'soapbox/hooks';
|
2022-08-01 09:31:49 -07:00
|
|
|
import { RootState } from 'soapbox/store';
|
2022-03-24 06:17:32 -07:00
|
|
|
import FaviconService from 'soapbox/utils/favicon_service';
|
|
|
|
|
|
|
|
FaviconService.initFaviconService();
|
|
|
|
|
2022-08-01 09:31:49 -07:00
|
|
|
const getNotifTotals = (state: RootState): number => {
|
|
|
|
const notifications = state.notifications.unread || 0;
|
|
|
|
const chats = state.chats.items.reduce((acc: any, curr: any) => acc + Math.min(curr.get('unread', 0), 1), 0);
|
|
|
|
const reports = state.admin.openReports.count();
|
|
|
|
const approvals = state.admin.awaitingApproval.count();
|
2022-03-24 06:17:32 -07:00
|
|
|
return notifications + chats + reports + approvals;
|
|
|
|
};
|
|
|
|
|
|
|
|
const Helmet: React.FC = ({ children }) => {
|
2022-03-28 16:54:28 -07:00
|
|
|
const title = useAppSelector((state) => state.instance.title);
|
2022-03-24 06:17:32 -07:00
|
|
|
const unreadCount = useAppSelector((state) => getNotifTotals(state));
|
2022-03-28 16:54:28 -07:00
|
|
|
const demetricator = useSettings().get('demetricator');
|
2022-03-24 06:17:32 -07:00
|
|
|
|
|
|
|
const hasUnreadNotifications = React.useMemo(() => !(unreadCount < 1 || demetricator), [unreadCount, demetricator]);
|
|
|
|
|
|
|
|
const addCounter = (string: string) => {
|
2022-03-28 16:54:28 -07:00
|
|
|
return hasUnreadNotifications ? `(${unreadCount}) ${string}` : string;
|
2022-03-24 06:17:32 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const updateFaviconBadge = () => {
|
|
|
|
if (hasUnreadNotifications) {
|
|
|
|
FaviconService.drawFaviconBadge();
|
|
|
|
} else {
|
|
|
|
FaviconService.clearFaviconBadge();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
updateFaviconBadge();
|
|
|
|
}, [unreadCount, demetricator]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ReactHelmet
|
|
|
|
titleTemplate={addCounter(`%s | ${title}`)}
|
|
|
|
defaultTitle={addCounter(title)}
|
|
|
|
defer={false}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</ReactHelmet>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Helmet;
|