File size: 1,869 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { planHasFeature, FEATURE_UNLIMITED_STORAGE } from '@automattic/calypso-products';
import { ProgressBar } from '@automattic/components';
import clsx from 'clsx';
import filesize from 'filesize';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';

const ALERT_PERCENT = 80;
const WARN_PERCENT = 60;

export class PlanStorageBar extends Component {
	static propTypes = {
		className: PropTypes.string,
		mediaStorage: PropTypes.object,
		displayUpgradeLink: PropTypes.bool,
		sitePlanSlug: PropTypes.string.isRequired,
	};

	render() {
		const { className, displayUpgradeLink, mediaStorage, sitePlanSlug, translate } = this.props;

		if ( planHasFeature( sitePlanSlug, FEATURE_UNLIMITED_STORAGE ) ) {
			return null;
		}

		if ( ! mediaStorage || mediaStorage.maxStorageBytes === -1 ) {
			return null;
		}

		let percent = ( mediaStorage.storageUsedBytes / mediaStorage.maxStorageBytes ) * 100;

		// Round percentage to 2dp for values under 1%, and whole numbers otherwise.
		percent = percent < 1 ? percent.toFixed( 2 ) : Math.round( percent );

		const classes = clsx( className, 'plan-storage__bar', {
			'is-alert': percent > ALERT_PERCENT,
			'is-warn': percent > WARN_PERCENT && percent <= ALERT_PERCENT,
		} );

		const max = filesize( mediaStorage.maxStorageBytes, { round: 0 } );

		return (
			<div className={ classes }>
				<ProgressBar value={ percent } total={ 100 } compact />

				<span className="plan-storage__storage-label">
					{ translate( '%(percent)f%% of %(max)s used', {
						args: {
							percent: percent,
							max: max,
						},
					} ) }
				</span>

				{ displayUpgradeLink && (
					<span className="plan-storage__storage-link">{ translate( 'Upgrade' ) }</span>
				) }

				{ this.props.children }
			</div>
		);
	}
}

export default localize( PlanStorageBar );