refactor(quota): unify reset time formatting across providers
This commit is contained in:
@@ -152,9 +152,25 @@ const normalizeAuthEntry = (entry: AuthEntry | null) => {
|
||||
return null;
|
||||
};
|
||||
|
||||
const formatResetAt = (timestamp: number) => {
|
||||
const formatResetTime = (timestamp: number) => {
|
||||
try {
|
||||
return new Date(timestamp).toLocaleTimeString(undefined, {
|
||||
const resetDate = new Date(timestamp);
|
||||
const now = new Date();
|
||||
const isToday = resetDate.toDateString() === now.toDateString();
|
||||
|
||||
if (isToday) {
|
||||
// Same day: show time only (e.g., "9:56 PM")
|
||||
return resetDate.toLocaleTimeString(undefined, {
|
||||
hour: 'numeric',
|
||||
minute: '2-digit',
|
||||
});
|
||||
}
|
||||
|
||||
// Different day: show date + weekday + time (e.g., "Feb 2, Sun 9:56 PM")
|
||||
return resetDate.toLocaleString(undefined, {
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
weekday: 'short',
|
||||
hour: 'numeric',
|
||||
minute: '2-digit',
|
||||
});
|
||||
@@ -163,25 +179,6 @@ const formatResetAt = (timestamp: number) => {
|
||||
}
|
||||
};
|
||||
|
||||
const formatDuration = (seconds: number | null) => {
|
||||
if (typeof seconds !== 'number' || Number.isNaN(seconds)) {
|
||||
return null;
|
||||
}
|
||||
const clamped = Math.max(0, Math.round(seconds));
|
||||
const hours = Math.floor(clamped / 3600);
|
||||
const minutes = Math.floor((clamped % 3600) / 60);
|
||||
if (hours === 0 && minutes === 0) {
|
||||
return '0m';
|
||||
}
|
||||
if (hours === 0) {
|
||||
return `${minutes}m`;
|
||||
}
|
||||
if (minutes === 0) {
|
||||
return `${hours}h`;
|
||||
}
|
||||
return `${hours}h ${minutes}m`;
|
||||
};
|
||||
|
||||
const calculateResetAfterSeconds = (resetAt: number | null) => {
|
||||
if (!resetAt) return null;
|
||||
const delta = Math.floor((resetAt - Date.now()) / 1000);
|
||||
@@ -190,14 +187,15 @@ const calculateResetAfterSeconds = (resetAt: number | null) => {
|
||||
|
||||
const toUsageWindow = (data: { usedPercent: number | null; windowSeconds: number | null; resetAt: number | null }) => {
|
||||
const resetAfterSeconds = calculateResetAfterSeconds(data.resetAt);
|
||||
const resetFormatted = data.resetAt ? formatResetTime(data.resetAt) : null;
|
||||
return {
|
||||
usedPercent: data.usedPercent,
|
||||
remainingPercent: data.usedPercent !== null ? Math.max(0, 100 - data.usedPercent) : null,
|
||||
windowSeconds: data.windowSeconds ?? null,
|
||||
resetAfterSeconds,
|
||||
resetAt: data.resetAt,
|
||||
resetAtFormatted: data.resetAt ? formatResetAt(data.resetAt) : null,
|
||||
resetAfterFormatted: resetAfterSeconds !== null ? formatDuration(resetAfterSeconds) : null,
|
||||
resetAtFormatted: resetFormatted,
|
||||
resetAfterFormatted: resetFormatted,
|
||||
} satisfies UsageWindow;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user