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

58 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-04-14 21:21:36 -07:00
import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { Helmet } from'react-helmet';
2021-01-15 11:10:50 -08:00
import { getSettings } from 'soapbox/actions/settings';
import sourceCode from 'soapbox/utils/code';
2020-04-14 21:21:36 -07:00
const getNotifTotals = state => {
const notifications = state.getIn(['notifications', 'unread'], 0);
const chats = state.get('chats').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 {
siteTitle: state.getIn(['instance', 'title'], sourceCode.displayName),
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
};
addCounter = title => {
2021-01-15 11:10:50 -08:00
const { unreadCount, demetricator } = this.props;
if (unreadCount < 1 || demetricator) return title;
return `(${unreadCount}) ${title}`;
}
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 connect(mapStateToProps)(SoapboxHelmet);