bigbuffet-rw/app/soapbox/features/admin/index.js

165 lines
6 KiB
JavaScript
Raw Normal View History

import PropTypes from 'prop-types';
2020-12-29 10:34:23 -08:00
import React from 'react';
import ImmutablePropTypes from 'react-immutable-proptypes';
import ImmutablePureComponent from 'react-immutable-pure-component';
2020-12-29 10:34:23 -08:00
import { defineMessages, injectIntl, FormattedMessage, FormattedNumber } from 'react-intl';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
import { getSubscribersCsv, getUnsubscribersCsv, getCombinedCsv } from 'soapbox/actions/email_list';
import { isAdmin } from 'soapbox/utils/accounts';
import sourceCode from 'soapbox/utils/code';
import { parseVersion } from 'soapbox/utils/features';
import { getFeatures } from 'soapbox/utils/features';
2021-11-16 05:12:55 -08:00
import { isNumber } from 'soapbox/utils/numbers';
2022-01-10 14:01:24 -08:00
import Column from '../ui/components/column';
2022-01-10 14:01:24 -08:00
import RegistrationModePicker from './components/registration_mode_picker';
// https://stackoverflow.com/a/53230807
const download = (response, filename) => {
const url = URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', filename);
document.body.appendChild(link);
link.click();
link.remove();
};
2020-12-29 10:34:23 -08:00
const messages = defineMessages({
heading: { id: 'column.admin.dashboard', defaultMessage: 'Dashboard' },
});
const mapStateToProps = (state, props) => {
const me = state.get('me');
return {
instance: state.get('instance'),
supportsEmailList: getFeatures(state.get('instance')).emailList,
account: state.getIn(['accounts', me]),
};
};
2020-12-29 10:34:23 -08:00
export default @connect(mapStateToProps)
@injectIntl
class Dashboard extends ImmutablePureComponent {
static propTypes = {
intl: PropTypes.object.isRequired,
instance: ImmutablePropTypes.map.isRequired,
supportsEmailList: PropTypes.bool,
account: ImmutablePropTypes.map,
2020-12-29 10:34:23 -08:00
};
handleSubscribersClick = e => {
this.props.dispatch(getSubscribersCsv()).then((response) => {
download(response, 'subscribers.csv');
}).catch(() => {});
e.preventDefault();
}
handleUnsubscribersClick = e => {
this.props.dispatch(getUnsubscribersCsv()).then((response) => {
download(response, 'unsubscribers.csv');
}).catch(() => {});
e.preventDefault();
}
handleCombinedClick = e => {
this.props.dispatch(getCombinedCsv()).then((response) => {
download(response, 'combined.csv');
}).catch(() => {});
e.preventDefault();
}
2020-12-29 10:34:23 -08:00
render() {
const { intl, instance, supportsEmailList, account } = this.props;
2020-12-29 10:34:23 -08:00
const v = parseVersion(instance.get('version'));
2021-04-22 19:00:28 -07:00
const userCount = instance.getIn(['stats', 'user_count']);
2021-04-22 18:23:54 -07:00
const mau = instance.getIn(['pleroma', 'stats', 'mau']);
2021-04-22 19:00:28 -07:00
const retention = (userCount && mau) ? Math.round(mau / userCount * 100) : null;
2020-12-29 10:34:23 -08:00
if (!account) return null;
2020-12-29 10:34:23 -08:00
return (
<Column icon='tachometer-alt' heading={intl.formatMessage(messages.heading)}>
2020-12-29 10:34:23 -08:00
<div className='dashcounters'>
2021-04-22 19:00:28 -07:00
{mau && <div className='dashcounter'>
<div>
<div className='dashcounter__num'>
<FormattedNumber value={mau} />
</div>
<div className='dashcounter__label'>
<FormattedMessage id='admin.dashcounters.mau_label' defaultMessage='monthly active users' />
</div>
</div>
</div>}
2020-12-29 10:34:23 -08:00
<div className='dashcounter'>
<Link to='/admin/users'>
2020-12-29 10:34:23 -08:00
<div className='dashcounter__num'>
2021-04-22 19:00:28 -07:00
<FormattedNumber value={userCount} />
2020-12-29 10:34:23 -08:00
</div>
<div className='dashcounter__label'>
2021-04-22 19:00:28 -07:00
<FormattedMessage id='admin.dashcounters.user_count_label' defaultMessage='total users' />
2020-12-29 10:34:23 -08:00
</div>
</Link>
2020-12-29 10:34:23 -08:00
</div>
2021-11-16 05:12:55 -08:00
{isNumber(retention) && (
<div className='dashcounter'>
<div>
<div className='dashcounter__num'>
{retention}%
</div>
<div className='dashcounter__label'>
<FormattedMessage id='admin.dashcounters.retention_label' defaultMessage='user retention' />
</div>
2021-04-22 19:00:28 -07:00
</div>
</div>
2021-11-16 05:12:55 -08:00
)}
2020-12-29 10:34:23 -08:00
<div className='dashcounter'>
<Link to='/timeline/local'>
2020-12-29 10:34:23 -08:00
<div className='dashcounter__num'>
<FormattedNumber value={instance.getIn(['stats', 'status_count'])} />
</div>
<div className='dashcounter__label'>
<FormattedMessage id='admin.dashcounters.status_count_label' defaultMessage='posts' />
</div>
</Link>
2020-12-29 10:34:23 -08:00
</div>
<div className='dashcounter'>
<div>
2020-12-29 10:34:23 -08:00
<div className='dashcounter__num'>
<FormattedNumber value={instance.getIn(['stats', 'domain_count'])} />
2020-12-29 10:34:23 -08:00
</div>
<div className='dashcounter__label'>
<FormattedMessage id='admin.dashcounters.domain_count_label' defaultMessage='peers' />
2020-12-29 10:34:23 -08:00
</div>
</div>
2020-12-29 10:34:23 -08:00
</div>
</div>
{isAdmin(account) && <RegistrationModePicker />}
2020-12-29 10:34:23 -08:00
<div className='dashwidgets'>
2020-12-29 21:25:07 -08:00
<div className='dashwidget'>
2020-12-29 10:34:23 -08:00
<h4><FormattedMessage id='admin.dashwidgets.software_header' defaultMessage='Software' /></h4>
<ul>
2021-08-21 20:46:33 -07:00
<li>{sourceCode.displayName} <span className='pull-right'>{sourceCode.version}</span></li>
2020-12-29 21:25:07 -08:00
<li>{v.software} <span className='pull-right'>{v.version}</span></li>
2020-12-29 10:34:23 -08:00
</ul>
</div>
{supportsEmailList && isAdmin(account) && <div className='dashwidget'>
<h4><FormattedMessage id='admin.dashwidgets.email_list_header' defaultMessage='Email list' /></h4>
<ul>
<li><a href='#' onClick={this.handleSubscribersClick} target='_blank'>subscribers.csv</a></li>
<li><a href='#' onClick={this.handleUnsubscribersClick} target='_blank'>unsubscribers.csv</a></li>
<li><a href='#' onClick={this.handleCombinedClick} target='_blank'>combined.csv</a></li>
</ul>
</div>}
2020-12-29 10:34:23 -08:00
</div>
</Column>
);
}
}