|
|
|
|
@ -1,55 +1,57 @@
|
|
|
|
|
import Timeframe from "~/types/timeframe"
|
|
|
|
|
import Timeframe from "~/types/timeframe";
|
|
|
|
|
|
|
|
|
|
const timeframeToInterval = (timeframe: Timeframe): string => {
|
|
|
|
|
switch (timeframe) {
|
|
|
|
|
case Timeframe.Day:
|
|
|
|
|
return "1 day"
|
|
|
|
|
return "1 day";
|
|
|
|
|
case Timeframe.Week:
|
|
|
|
|
return "1 week"
|
|
|
|
|
return "1 week";
|
|
|
|
|
case Timeframe.Month:
|
|
|
|
|
return "1 month"
|
|
|
|
|
return "1 month";
|
|
|
|
|
case Timeframe.Year:
|
|
|
|
|
return "1 year"
|
|
|
|
|
return "1 year";
|
|
|
|
|
case Timeframe.AllTime:
|
|
|
|
|
return "99 years"
|
|
|
|
|
}
|
|
|
|
|
return "99 years";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function timeSince(date: Date) {
|
|
|
|
|
const now = new Date();
|
|
|
|
|
const seconds = Math.floor((now.getTime() - date.getTime()) / 1000);
|
|
|
|
|
|
|
|
|
|
const intervals = [
|
|
|
|
|
{ label: 'year', seconds: 31536000 },
|
|
|
|
|
{ label: 'month', seconds: 2592000 },
|
|
|
|
|
{ label: 'week', seconds: 604800 },
|
|
|
|
|
{ label: 'day', seconds: 86400 },
|
|
|
|
|
{ label: 'hour', seconds: 3600 },
|
|
|
|
|
{ label: 'minute', seconds: 60 },
|
|
|
|
|
{ label: 'second', seconds: 1 },
|
|
|
|
|
{ label: "year", seconds: 31536000 },
|
|
|
|
|
{ label: "month", seconds: 2592000 },
|
|
|
|
|
{ label: "week", seconds: 604800 },
|
|
|
|
|
{ label: "day", seconds: 86400 },
|
|
|
|
|
{ label: "hour", seconds: 3600 },
|
|
|
|
|
{ label: "minute", seconds: 60 },
|
|
|
|
|
{ label: "second", seconds: 1 },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (const interval of intervals) {
|
|
|
|
|
const count = Math.floor(seconds / interval.seconds);
|
|
|
|
|
if (count >= 1) {
|
|
|
|
|
return `${count} ${interval.label}${count !== 1 ? 's' : ''} ago`;
|
|
|
|
|
return `${count} ${interval.label}${count !== 1 ? "s" : ""} ago`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 'just now';
|
|
|
|
|
return "just now";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { timeSince }
|
|
|
|
|
export { timeSince };
|
|
|
|
|
|
|
|
|
|
type hsl = {
|
|
|
|
|
h: number,
|
|
|
|
|
s: number,
|
|
|
|
|
l: number,
|
|
|
|
|
}
|
|
|
|
|
h: number;
|
|
|
|
|
s: number;
|
|
|
|
|
l: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const hexToHSL = (hex: string): hsl => {
|
|
|
|
|
let r = 0, g = 0, b = 0;
|
|
|
|
|
hex = hex.replace('#', '');
|
|
|
|
|
let r = 0,
|
|
|
|
|
g = 0,
|
|
|
|
|
b = 0;
|
|
|
|
|
hex = hex.replace("#", "");
|
|
|
|
|
|
|
|
|
|
if (hex.length === 3) {
|
|
|
|
|
r = parseInt(hex[0] + hex[0], 16);
|
|
|
|
|
@ -65,16 +67,25 @@ const hexToHSL = (hex: string): hsl => {
|
|
|
|
|
g /= 255;
|
|
|
|
|
b /= 255;
|
|
|
|
|
|
|
|
|
|
const max = Math.max(r, g, b), min = Math.min(r, g, b);
|
|
|
|
|
let h = 0, s = 0, l = (max + min) / 2;
|
|
|
|
|
const max = Math.max(r, g, b),
|
|
|
|
|
min = Math.min(r, g, b);
|
|
|
|
|
let h = 0,
|
|
|
|
|
s = 0,
|
|
|
|
|
l = (max + min) / 2;
|
|
|
|
|
|
|
|
|
|
if (max !== min) {
|
|
|
|
|
const d = max - min;
|
|
|
|
|
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
|
|
|
switch (max) {
|
|
|
|
|
case r: h = ((g - b) / d + (g < b ? 6 : 0)); break;
|
|
|
|
|
case g: h = ((b - r) / d + 2); break;
|
|
|
|
|
case b: h = ((r - g) / d + 4); break;
|
|
|
|
|
case r:
|
|
|
|
|
h = (g - b) / d + (g < b ? 6 : 0);
|
|
|
|
|
break;
|
|
|
|
|
case g:
|
|
|
|
|
h = (b - r) / d + 2;
|
|
|
|
|
break;
|
|
|
|
|
case b:
|
|
|
|
|
h = (r - g) / d + 4;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
h /= 6;
|
|
|
|
|
}
|
|
|
|
|
@ -82,21 +93,16 @@ const hexToHSL = (hex: string): hsl => {
|
|
|
|
|
return {
|
|
|
|
|
h: Math.round(h * 360),
|
|
|
|
|
s: Math.round(s * 100),
|
|
|
|
|
l: Math.round(l * 100)
|
|
|
|
|
l: Math.round(l * 100),
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const timeListenedString = (seconds: number) => {
|
|
|
|
|
if (!seconds) return ""
|
|
|
|
|
|
|
|
|
|
if (seconds > (120 * 60) - 1) {
|
|
|
|
|
let hours = Math.floor(seconds / 60 / 60)
|
|
|
|
|
return `${hours} hours listened`
|
|
|
|
|
} else {
|
|
|
|
|
let minutes = Math.floor(seconds / 60)
|
|
|
|
|
return `${minutes} minutes listened`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!seconds) return "";
|
|
|
|
|
|
|
|
|
|
let minutes = Math.floor(seconds / 60);
|
|
|
|
|
return `${minutes} minutes listened`;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export {hexToHSL, timeListenedString}
|
|
|
|
|
export type {hsl}
|
|
|
|
|
export { hexToHSL, timeListenedString };
|
|
|
|
|
export type { hsl };
|
|
|
|
|
|