Skip to content

Commit

Permalink
Putting the format size stuff in a common function
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjyong committed Apr 25, 2024
1 parent 9102678 commit c680fbd
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions frontend/src/stores/upload.ts
Expand Up @@ -13,6 +13,15 @@ const beforeUnload = (event: Event) => {
// event.returnValue = "";
};

// Utility function to format bytes into a readable string
function formatSize(bytes: number): string {
if (bytes === 0) return '0 Bytes';

Check warning on line 18 in frontend/src/stores/upload.ts

View workflow job for this annotation

GitHub Actions / lint-frontend

Replace `'0路Bytes'` with `"0路Bytes"`

const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

Check warning on line 20 in frontend/src/stores/upload.ts

View workflow job for this annotation

GitHub Actions / lint-frontend

Replace `'Bytes',路'KB',路'MB',路'GB',路'TB',路'PB',路'EB',路'ZB',路'YB'` with `"Bytes",路"KB",路"MB",路"GB",路"TB",路"PB",路"EB",路"ZB",路"YB"`
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return parseFloat((bytes / Math.pow(1024, i)).toFixed(2)) + ' ' + sizes[i];

Check warning on line 22 in frontend/src/stores/upload.ts

View workflow job for this annotation

GitHub Actions / lint-frontend

Replace `'路'` with `"路"`
}

export const useUploadStore = defineStore("upload", {
// convert to a function
state: (): {
Expand Down Expand Up @@ -59,29 +68,19 @@ export const useUploadStore = defineStore("upload", {
return ((sum / totalSize) * 100).toFixed(2);
},
getTotalProgressBytes: (state) => {
if (state.progress.length === 0 || state.sizes.length === 0) {
if (state.progress.length === 0 || state.sizes.length === 0) {
return '0 Bytes';

Check warning on line 72 in frontend/src/stores/upload.ts

View workflow job for this annotation

GitHub Actions / lint-frontend

Replace `'0路Bytes'` with `"0路Bytes"`
}

const totalSize = state.sizes.reduce((a, b) => a + b, 0);

// TODO: this looks ugly but it works with ts now
const sum = state.progress.reduce((acc, val) => +acc + +val) as number;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(sum) / Math.log(1024));
return parseFloat((sum / Math.pow(1024, i)).toFixed(2)) + ' ' + sizes[i];

const sum = state.progress.reduce((acc, val) => +acc + +val, 0) as number;
return formatSize(sum);
},
getTotalSize: (state) => {
if (state.sizes.length === 0) {
return '0 Bytes';
}

const totalSize = state.sizes.reduce((a, b) => a + b, 0);
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(totalSize) / Math.log(1024));
return parseFloat((totalSize / Math.pow(1024, i)).toFixed(2)) + ' ' + sizes[i];
},
return formatSize(totalSize);
},
filesInUploadCount: (state) => {
return Object.keys(state.uploads).length + state.queue.length;
},
Expand Down

0 comments on commit c680fbd

Please sign in to comment.