p5.js-web-editor/client/modules/IDE/components/AssetSize.jsx

53 lines
1.3 KiB
React
Raw Permalink Normal View History

2019-11-04 20:30:24 +01:00
import PropTypes from 'prop-types';
import React from 'react';
import { connect } from 'react-redux';
import prettyBytes from 'pretty-bytes';
import getConfig from '../../../utils/getConfig';
const limit = getConfig('UPLOAD_LIMIT') || 250000000;
const MAX_SIZE_B = limit;
2019-11-04 20:30:24 +01:00
const formatPercent = (percent) => {
const percentUsed = percent * 100;
if (percentUsed < 1) {
return '0%';
}
return `${Math.round(percentUsed)}%`;
};
/* Eventually, this copy should be Total / 250 MB Used */
const AssetSize = ({ totalSize }) => {
if (totalSize === undefined) {
2019-11-04 20:30:24 +01:00
return null;
}
const currentSize = prettyBytes(totalSize);
const sizeLimit = prettyBytes(MAX_SIZE_B);
2019-11-25 11:15:10 +01:00
const percentValue = totalSize / MAX_SIZE_B;
const percent = formatPercent(percentValue);
const percentSize = percentValue < 1 ? percentValue : 1;
2019-11-04 20:30:24 +01:00
return (
<div className="asset-size" style={{ '--percent': percentSize }}>
2019-11-25 11:15:10 +01:00
<div className="asset-size-bar" />
<p className="asset-current">{currentSize} ({percent})</p>
<p className="asset-max">Max: {sizeLimit}</p>
2019-11-04 20:30:24 +01:00
</div>
);
};
AssetSize.propTypes = {
totalSize: PropTypes.number.isRequired,
};
function mapStateToProps(state) {
return {
user: state.user,
totalSize: state.user.totalSize || state.assets.totalSize,
2019-11-04 20:30:24 +01:00
};
}
export default connect(mapStateToProps)(AssetSize);