2025-05-18 14:03:18 -07:00
|
|
|
import type { Album, AlbumArtist, Song } from '/@/renderer/api/types';
|
|
|
|
|
|
2024-05-26 12:20:01 -07:00
|
|
|
import dayjs from 'dayjs';
|
|
|
|
|
import relativeTime from 'dayjs/plugin/relativeTime';
|
2024-09-12 20:35:57 -05:00
|
|
|
import utc from 'dayjs/plugin/utc';
|
2024-05-26 12:20:01 -07:00
|
|
|
import formatDuration from 'format-duration';
|
2025-05-18 14:03:18 -07:00
|
|
|
|
2024-05-26 12:20:01 -07:00
|
|
|
import { Rating } from '/@/renderer/components/rating';
|
|
|
|
|
|
|
|
|
|
dayjs.extend(relativeTime);
|
2024-09-12 20:35:57 -05:00
|
|
|
dayjs.extend(utc);
|
|
|
|
|
|
2024-09-16 17:33:06 -07:00
|
|
|
const FORMATS: Record<number, string> = Object.freeze({
|
|
|
|
|
0: 'YYYY',
|
|
|
|
|
1: 'MMM YYYY',
|
|
|
|
|
2: 'MMM D, YYYY',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const getDateFormat = (key: string): string => {
|
|
|
|
|
const dashes = Math.min(key.split('-').length - 1, 2);
|
|
|
|
|
return FORMATS[dashes];
|
|
|
|
|
};
|
2024-05-26 12:20:01 -07:00
|
|
|
|
2025-05-18 14:03:18 -07:00
|
|
|
export const formatDateAbsolute = (key: null | string) =>
|
2024-09-16 17:33:06 -07:00
|
|
|
key ? dayjs(key).format(getDateFormat(key)) : '';
|
2024-09-12 20:35:57 -05:00
|
|
|
|
2025-05-18 14:03:18 -07:00
|
|
|
export const formatDateAbsoluteUTC = (key: null | string) =>
|
2024-09-16 17:33:06 -07:00
|
|
|
key ? dayjs.utc(key).format(getDateFormat(key)) : '';
|
2024-05-26 12:20:01 -07:00
|
|
|
|
2025-05-18 14:03:18 -07:00
|
|
|
export const formatDateRelative = (key: null | string) => (key ? dayjs(key).fromNow() : '');
|
2024-05-26 12:20:01 -07:00
|
|
|
|
|
|
|
|
export const formatDurationString = (duration: number) => {
|
|
|
|
|
const rawDuration = formatDuration(duration).split(':');
|
|
|
|
|
|
|
|
|
|
let string;
|
|
|
|
|
|
|
|
|
|
switch (rawDuration.length) {
|
|
|
|
|
case 1:
|
|
|
|
|
string = `${rawDuration[0]} sec`;
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
string = `${rawDuration[0]} min ${rawDuration[1]} sec`;
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
string = `${rawDuration[0]} hr ${rawDuration[1]} min ${rawDuration[2]} sec`;
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
string = `${rawDuration[0]} day ${rawDuration[1]} hr ${rawDuration[2]} min ${rawDuration[3]} sec`;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const formatRating = (item: Album | AlbumArtist | Song) =>
|
|
|
|
|
item.userRating !== null ? (
|
|
|
|
|
<Rating
|
|
|
|
|
readOnly
|
|
|
|
|
value={item.userRating}
|
|
|
|
|
/>
|
|
|
|
|
) : null;
|
|
|
|
|
|
|
|
|
|
const SIZES = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
|
|
|
|
|
|
|
|
|
|
export const formatSizeString = (size?: number): string => {
|
|
|
|
|
let count = 0;
|
|
|
|
|
let finalSize = size ?? 0;
|
|
|
|
|
while (finalSize > 1024) {
|
|
|
|
|
finalSize /= 1024;
|
|
|
|
|
count += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return `${finalSize.toFixed(2)} ${SIZES[count]}`;
|
|
|
|
|
};
|