feat: implement ThemeProvider and SoundCard components; add utility functions for formatting duration and size

This commit is contained in:
JSC
2025-08-02 18:21:26 +02:00
parent e66ab7b7f8
commit b42b802c37
9 changed files with 424 additions and 29 deletions

View File

@@ -0,0 +1,18 @@
export function formatDuration(ms: number): string {
if (isNaN(ms) || ms < 0) return '0:00'
// Convert milliseconds to seconds
const totalSeconds = Math.floor(ms / 1000)
// Calculate hours, minutes, and remaining seconds
const hours = Math.floor(totalSeconds / 3600)
const minutes = Math.floor((totalSeconds % 3600) / 60)
const seconds = totalSeconds % 60
// Format based on duration
if (hours > 0) {
return `${hours}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`
} else {
return `${minutes}:${seconds.toString().padStart(2, '0')}`
}
}

65
src/utils/format-size.ts Normal file
View File

@@ -0,0 +1,65 @@
/**
* Units for file sizes
*/
const FILE_SIZE_UNITS = ['B', 'KB', 'MB', 'GB', 'TB']
/**
* Interface for file size
*/
export interface FileSize {
value: number
unit: string
}
/**
* Base function to parse file size in bytes to value and unit
* @param bytes File size in bytes
* @param binary Whether to use binary (1024) or decimal (1000) units
* @returns Object with numeric value and unit string
*/
function parseSize(bytes: number, binary: boolean = false): FileSize {
// Handle invalid input
if (bytes === null || bytes === undefined || isNaN(bytes) || bytes < 0) {
return { value: 0, unit: 'B' }
}
// If the size is 0, return early
if (bytes === 0) {
return { value: 0, unit: 'B' }
}
// Otherwise, determine the appropriate unit based on the size
const unit = binary ? 1024 : 1000
const unitIndex = Math.floor(Math.log(bytes) / Math.log(unit))
const unitSize = Math.pow(unit, unitIndex)
const value = bytes / unitSize
// Make sure we don't exceed our units array
const safeUnitIndex = Math.min(unitIndex, FILE_SIZE_UNITS.length - 1)
return {
value: Math.round(value * 100) / 100, // Round to 2 decimal places
unit: FILE_SIZE_UNITS[safeUnitIndex]
}
}
/**
* Converts a file size in bytes to a human-readable string
* @param bytes File size in bytes
* @param binary Whether to use binary (1024) or decimal (1000) units
* @returns Formatted file size string (e.g., "1.5 MB")
*/
export function formatSize(bytes: number, binary: boolean = false): string {
const { value, unit } = parseSize(bytes, binary)
return `${value.toFixed(2)} ${unit}`
}
/**
* Converts a file size in bytes to an object with value and unit
* @param bytes File size in bytes
* @param binary Whether to use binary (1024) or decimal (1000) units
* @returns Object with numeric value and unit string
*/
export function formatSizeObject(bytes: number, binary: boolean = false): FileSize {
return parseSize(bytes, binary)
}