bigbuffet-rw/app/soapbox/components/helmet.js

88 lines
2.3 KiB
JavaScript
Raw Normal View History

import PropTypes from 'prop-types';
2020-04-14 21:21:36 -07:00
import React from 'react';
import { Helmet } from'react-helmet';
2020-04-14 21:21:36 -07:00
import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom';
2021-01-15 11:10:50 -08:00
import { getSettings } from 'soapbox/actions/settings';
2022-03-21 11:09:01 -07:00
// import sourceCode from 'soapbox/utils/code';
import FaviconService from 'soapbox/utils/favicon_service';
2020-04-14 21:21:36 -07:00
FaviconService.initFaviconService();
const getNotifTotals = state => {
const notifications = state.getIn(['notifications', 'unread'], 0);
const chats = state.getIn(['chats', 'items']).reduce((acc, curr) => acc + Math.min(curr.get('unread', 0), 1), 0);
2020-12-31 12:29:31 -08:00
const reports = state.getIn(['admin', 'openReports']).count();
const approvals = state.getIn(['admin', 'awaitingApproval']).count();
return notifications + chats + reports + approvals;
};
2021-01-15 11:10:50 -08:00
const mapStateToProps = state => {
const settings = getSettings(state);
return {
2022-03-21 11:33:10 -07:00
siteTitle: state.getIn(['instance', 'title']),
2021-01-15 11:10:50 -08:00
unreadCount: getNotifTotals(state),
demetricator: settings.get('demetricator'),
};
};
2020-04-14 21:21:36 -07:00
class SoapboxHelmet extends React.Component {
static propTypes = {
2020-04-21 10:04:00 -07:00
siteTitle: PropTypes.string,
2020-04-14 21:21:36 -07:00
children: PropTypes.node,
unreadCount: PropTypes.number,
2021-01-15 11:10:50 -08:00
demetricator: PropTypes.bool,
2020-04-14 21:21:36 -07:00
};
hasUnread = () => {
2021-01-15 11:10:50 -08:00
const { unreadCount, demetricator } = this.props;
return !(unreadCount < 1 || demetricator);
}
addCounter = title => {
const { unreadCount } = this.props;
const hasUnread = this.hasUnread();
return hasUnread ? `(${unreadCount}) ${title}` : title;
}
updateFaviconBadge = () => {
const hasUnread = this.hasUnread();
if (hasUnread) {
FaviconService.drawFaviconBadge();
} else {
FaviconService.clearFaviconBadge();
}
}
componentDidUpdate(prevProps) {
if (this.props.unreadCount !== prevProps.unreadCount || this.props.demetricator !== prevProps.demetricator) {
this.updateFaviconBadge();
}
}
2021-10-15 13:33:09 -07:00
componentDidMount() {
this.updateFaviconBadge();
}
2020-04-14 21:21:36 -07:00
render() {
const { siteTitle, children } = this.props;
return (
<Helmet
titleTemplate={this.addCounter(`%s | ${siteTitle}`)}
defaultTitle={this.addCounter(siteTitle)}
defer={false}
2020-04-14 21:21:36 -07:00
>
{children}
</Helmet>
);
}
}
export default withRouter(connect(mapStateToProps)(SoapboxHelmet));