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

49 lines
1.2 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';
const getNotifTotals = state => {
const normNotif = state.getIn(['notifications', 'unread']);
const chatNotif = state.get('chats').reduce((acc, curr) => acc + curr.get('unread'), 0);
const notifTotals = normNotif + chatNotif;
return notifTotals;
};
2020-04-14 21:21:36 -07:00
const mapStateToProps = state => ({
siteTitle: state.getIn(['instance', 'title']),
unreadCount: getNotifTotals(state),
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,
2020-04-14 21:21:36 -07:00
};
addCounter = title => {
const { unreadCount } = this.props;
if (unreadCount < 1) 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);