58 lines
1.1 KiB
TypeScript
58 lines
1.1 KiB
TypeScript
import { CountdownInfo } from "./types";
|
|
|
|
/**
|
|
* Determine the countdown label, color and urgency for a target time.
|
|
*
|
|
* Thresholds (minutes until target):
|
|
* - 0 → red, "Now"
|
|
* - ≤10 → orange (urgent)
|
|
* - ≤30 → yellow
|
|
* - ≤60 → green
|
|
* - >60 → blue ("Xh Ymin")
|
|
*/
|
|
export function calculateCountdown(targetDate: Date): CountdownInfo {
|
|
const now = new Date();
|
|
const diffMs = targetDate.getTime() - now.getTime();
|
|
const diffMin = Math.floor(diffMs / 60000);
|
|
|
|
if (diffMin <= 0) {
|
|
return {
|
|
label: "Now",
|
|
color: "red",
|
|
urgent: true,
|
|
};
|
|
}
|
|
|
|
if (diffMin <= 10) {
|
|
return {
|
|
label: `${diffMin}min`,
|
|
color: "orange",
|
|
urgent: true,
|
|
};
|
|
}
|
|
|
|
if (diffMin <= 30) {
|
|
return {
|
|
label: `${diffMin}min`,
|
|
color: "yellow",
|
|
urgent: false,
|
|
};
|
|
}
|
|
|
|
if (diffMin <= 60) {
|
|
return {
|
|
label: `${diffMin}min`,
|
|
color: "green",
|
|
urgent: false,
|
|
};
|
|
}
|
|
|
|
const hours = Math.floor(diffMin / 60);
|
|
const remainingMin = diffMin % 60;
|
|
return {
|
|
label: `${hours}h ${remainingMin}min`,
|
|
color: "blue",
|
|
urgent: false,
|
|
};
|
|
}
|