2021-10-15 05:44:59 +11:00
|
|
|
import PropTypes from 'prop-types';
|
2023-05-24 01:15:17 +10:00
|
|
|
import { PureComponent } from 'react';
|
|
|
|
|
2021-10-15 05:44:59 +11:00
|
|
|
import { FormattedNumber } from 'react-intl';
|
2023-05-24 01:15:17 +10:00
|
|
|
|
|
|
|
import api from 'mastodon/api';
|
2023-05-23 16:58:08 +10:00
|
|
|
import { Skeleton } from 'mastodon/components/skeleton';
|
2023-05-24 01:15:17 +10:00
|
|
|
import { roundTo10 } from 'mastodon/utils/numbers';
|
2021-10-15 05:44:59 +11:00
|
|
|
|
2023-05-23 18:52:27 +10:00
|
|
|
export default class Dimension extends PureComponent {
|
2021-10-15 05:44:59 +11:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
dimension: PropTypes.string.isRequired,
|
|
|
|
start_at: PropTypes.string.isRequired,
|
|
|
|
end_at: PropTypes.string.isRequired,
|
|
|
|
limit: PropTypes.number.isRequired,
|
|
|
|
label: PropTypes.string.isRequired,
|
2021-11-25 23:07:38 +11:00
|
|
|
params: PropTypes.object,
|
2021-10-15 05:44:59 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
loading: true,
|
|
|
|
data: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount () {
|
2021-11-25 23:07:38 +11:00
|
|
|
const { start_at, end_at, dimension, limit, params } = this.props;
|
2021-10-15 05:44:59 +11:00
|
|
|
|
2024-05-23 17:30:48 +10:00
|
|
|
api(false).post('/api/v1/admin/dimensions', { keys: [dimension], start_at, end_at, limit, [dimension]: params }).then(res => {
|
2021-10-15 05:44:59 +11:00
|
|
|
this.setState({
|
|
|
|
loading: false,
|
|
|
|
data: res.data,
|
|
|
|
});
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { label, limit } = this.props;
|
|
|
|
const { loading, data } = this.state;
|
|
|
|
|
|
|
|
let content;
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
content = (
|
|
|
|
<table>
|
|
|
|
<tbody>
|
|
|
|
{Array.from(Array(limit)).map((_, i) => (
|
|
|
|
<tr className='dimension__item' key={i}>
|
|
|
|
<td className='dimension__item__key'>
|
|
|
|
<Skeleton width={100} />
|
|
|
|
</td>
|
|
|
|
|
|
|
|
<td className='dimension__item__value'>
|
|
|
|
<Skeleton width={60} />
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const sum = data[0].data.reduce((sum, cur) => sum + (cur.value * 1), 0);
|
|
|
|
|
|
|
|
content = (
|
|
|
|
<table>
|
|
|
|
<tbody>
|
|
|
|
{data[0].data.map(item => (
|
|
|
|
<tr className='dimension__item' key={item.key}>
|
|
|
|
<td className='dimension__item__key'>
|
|
|
|
<span className={`dimension__item__indicator dimension__item__indicator--${roundTo10(((item.value * 1) / sum) * 100)}`} />
|
|
|
|
<span title={item.key}>{item.human_key}</span>
|
|
|
|
</td>
|
|
|
|
|
|
|
|
<td className='dimension__item__value'>
|
|
|
|
{typeof item.human_value !== 'undefined' ? item.human_value : <FormattedNumber value={item.value} />}
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='dimension'>
|
|
|
|
<h4>{label}</h4>
|
|
|
|
|
|
|
|
{content}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|