2023-05-23 18:47:36 +09:00
|
|
|
import { PureComponent } from 'react';
|
2021-10-15 03:44:59 +09:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import api from 'mastodon/api';
|
|
|
|
import { FormattedNumber } from 'react-intl';
|
|
|
|
import { Sparklines, SparklinesCurve } from 'react-sparklines';
|
|
|
|
import classNames from 'classnames';
|
2023-05-23 15:58:08 +09:00
|
|
|
import { Skeleton } from 'mastodon/components/skeleton';
|
2021-10-15 03:44:59 +09:00
|
|
|
|
|
|
|
const percIncrease = (a, b) => {
|
|
|
|
let percent;
|
|
|
|
|
|
|
|
if (b !== 0) {
|
|
|
|
if (a !== 0) {
|
|
|
|
percent = (b - a) / a;
|
|
|
|
} else {
|
|
|
|
percent = 1;
|
|
|
|
}
|
|
|
|
} else if (b === 0 && a === 0) {
|
|
|
|
percent = 0;
|
|
|
|
} else {
|
|
|
|
percent = - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return percent;
|
|
|
|
};
|
|
|
|
|
2023-05-23 17:52:27 +09:00
|
|
|
export default class Counter extends PureComponent {
|
2021-10-15 03:44:59 +09:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
measure: PropTypes.string.isRequired,
|
|
|
|
start_at: PropTypes.string.isRequired,
|
|
|
|
end_at: PropTypes.string.isRequired,
|
|
|
|
label: PropTypes.string.isRequired,
|
|
|
|
href: PropTypes.string,
|
2021-11-25 21:07:38 +09:00
|
|
|
params: PropTypes.object,
|
2022-03-26 10:52:51 +09:00
|
|
|
target: PropTypes.string,
|
2021-10-15 03:44:59 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
loading: true,
|
|
|
|
data: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount () {
|
2021-11-25 21:07:38 +09:00
|
|
|
const { measure, start_at, end_at, params } = this.props;
|
2021-10-15 03:44:59 +09:00
|
|
|
|
2021-11-25 21:07:38 +09:00
|
|
|
api().post('/api/v1/admin/measures', { keys: [measure], start_at, end_at, [measure]: params }).then(res => {
|
2021-10-15 03:44:59 +09:00
|
|
|
this.setState({
|
|
|
|
loading: false,
|
|
|
|
data: res.data,
|
|
|
|
});
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2022-03-26 10:52:51 +09:00
|
|
|
const { label, href, target } = this.props;
|
2021-10-15 03:44:59 +09:00
|
|
|
const { loading, data } = this.state;
|
|
|
|
|
|
|
|
let content;
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
content = (
|
2023-05-23 18:47:36 +09:00
|
|
|
<>
|
2021-10-15 03:44:59 +09:00
|
|
|
<span className='sparkline__value__total'><Skeleton width={43} /></span>
|
|
|
|
<span className='sparkline__value__change'><Skeleton width={43} /></span>
|
2023-05-23 18:47:36 +09:00
|
|
|
</>
|
2021-10-15 03:44:59 +09:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const measure = data[0];
|
2022-03-09 16:52:32 +09:00
|
|
|
const percentChange = measure.previous_total && percIncrease(measure.previous_total * 1, measure.total * 1);
|
2021-10-15 03:44:59 +09:00
|
|
|
|
|
|
|
content = (
|
2023-05-23 18:47:36 +09:00
|
|
|
<>
|
2022-03-09 16:52:32 +09:00
|
|
|
<span className='sparkline__value__total'>{measure.human_value || <FormattedNumber value={measure.total} />}</span>
|
|
|
|
{measure.previous_total && (<span className={classNames('sparkline__value__change', { positive: percentChange > 0, negative: percentChange < 0 })}>{percentChange > 0 && '+'}<FormattedNumber value={percentChange} style='percent' /></span>)}
|
2023-05-23 18:47:36 +09:00
|
|
|
</>
|
2021-10-15 03:44:59 +09:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const inner = (
|
2023-05-23 18:47:36 +09:00
|
|
|
<>
|
2021-10-15 03:44:59 +09:00
|
|
|
<div className='sparkline__value'>
|
|
|
|
{content}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className='sparkline__label'>
|
|
|
|
{label}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className='sparkline__graph'>
|
|
|
|
{!loading && (
|
|
|
|
<Sparklines width={259} height={55} data={data[0].data.map(x => x.value * 1)}>
|
|
|
|
<SparklinesCurve />
|
|
|
|
</Sparklines>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-05-23 18:47:36 +09:00
|
|
|
</>
|
2021-10-15 03:44:59 +09:00
|
|
|
);
|
|
|
|
|
|
|
|
if (href) {
|
|
|
|
return (
|
2022-03-26 10:52:51 +09:00
|
|
|
<a href={href} className='sparkline' target={target}>
|
2021-10-15 03:44:59 +09:00
|
|
|
{inner}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<div className='sparkline'>
|
|
|
|
{inner}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|