Browse Source

Add album dl size utility

master
jeffvli 3 years ago
committed by Jeff
parent
commit
10f2cc7c64
  1. 4
      src/components/shared/ToolbarButtons.tsx
  2. 21
      src/shared/utils.ts

4
src/components/shared/ToolbarButtons.tsx

@ -87,9 +87,9 @@ export const FavoriteButton = ({ isFavorite, ...rest }: any) => {
); );
}; };
export const DownloadButton = ({ ...rest }) => { export const DownloadButton = ({ downloadSize, ...rest }: any) => {
return ( return (
<CustomTooltip text="Toggle favorite"> <CustomTooltip text={downloadSize ? `Download (${downloadSize})` : 'Download'}>
<StyledIconButton tabIndex={0} icon={<Icon icon="download" />} {...rest} /> <StyledIconButton tabIndex={0} icon={<Icon icon="download" />} {...rest} />
</CustomTooltip> </CustomTooltip>
); );

21
src/shared/utils.ts

@ -96,6 +96,19 @@ export const shuffle = (array: any[]) => {
return array; return array;
}; };
// https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
const formatBytes = (bytes: number, decimals = 2) => {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}`;
};
export const formatSongDuration = (duration: number) => { export const formatSongDuration = (duration: number) => {
const hours = Math.floor(duration / 60 / 60); const hours = Math.floor(duration / 60 / 60);
const minutes = Math.floor((duration / 60) % 60); const minutes = Math.floor((duration / 60) % 60);
@ -491,3 +504,11 @@ export const getUniqueRandomNumberArr = (count: number, maxRange: number) => {
return arr; return arr;
}; };
export const getAlbumSize = (songs: any[]) => {
return formatBytes(
_.sumBy(songs, (o) => {
return o.size;
})
);
};

Loading…
Cancel
Save