mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +00:00
fix: update the quota information
This commit is contained in:
@@ -4,8 +4,8 @@ import { useCallback, useEffect, useState } from "react";
|
||||
import { AI_PROVIDERS } from "@/shared/constants/providers";
|
||||
import { Button, Card, CardSkeleton } from "@/shared/components";
|
||||
import ProviderIcon from "@/shared/components/ProviderIcon";
|
||||
import { formatResetTime, REFRESH_INTERVAL_MS } from "./ProviderLimits/utils";
|
||||
import { formatVietnamDateTime, formatVietnamTime, isSameVietnamDay } from "@/shared/utils/dateTime";
|
||||
import { REFRESH_INTERVAL_MS } from "./ProviderLimits/utils";
|
||||
import { formatVietnamTime } from "@/shared/utils/dateTime";
|
||||
|
||||
function getProviderInfo(providerId) {
|
||||
return AI_PROVIDERS[providerId] || {
|
||||
@@ -24,21 +24,6 @@ function formatUpdatedAt(value) {
|
||||
return formattedTime ? `Updated ${formattedTime}` : "Not updated yet";
|
||||
}
|
||||
|
||||
function formatResetAt(value) {
|
||||
if (!value) return null;
|
||||
|
||||
const date = new Date(value);
|
||||
if (!Number.isFinite(date.getTime())) return null;
|
||||
|
||||
const isToday = isSameVietnamDay(date);
|
||||
const isTomorrow = isSameVietnamDay(date, new Date(Date.now() + 86400000));
|
||||
const time = formatVietnamTime(date, { hour: "2-digit", minute: "2-digit" });
|
||||
|
||||
if (isToday) return `today at ${time}`;
|
||||
if (isTomorrow) return `tomorrow at ${time}`;
|
||||
return formatVietnamDateTime(date, { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit" });
|
||||
}
|
||||
|
||||
function getQuotaTone(percentage) {
|
||||
if (percentage > 70) {
|
||||
return { bar: "bg-green-500", dot: "bg-green-500", text: "text-green-500" };
|
||||
@@ -51,9 +36,6 @@ function getQuotaTone(percentage) {
|
||||
|
||||
function QuotaListRow({ quota }) {
|
||||
const tone = getQuotaTone(quota.remainingPercentage);
|
||||
const resetIn = formatResetTime(quota.resetAt);
|
||||
const resetAt = formatResetAt(quota.resetAt);
|
||||
const resetLabel = quota.recurring === false ? "Expires" : "Resets";
|
||||
|
||||
return (
|
||||
<li className="py-3 first:pt-0 last:pb-0">
|
||||
@@ -77,13 +59,6 @@ function QuotaListRow({ quota }) {
|
||||
style={{ width: `${Math.min(Math.max(quota.remainingPercentage, 0), 100)}%` }}
|
||||
/>
|
||||
</div>
|
||||
{(resetIn !== "-" || resetAt) && (
|
||||
<p className="mt-2 text-xs text-text-muted">
|
||||
{resetIn !== "-" && <span>{resetLabel} in {resetIn}</span>}
|
||||
{resetIn !== "-" && resetAt && <span className="px-1.5 text-text-muted/60"> · </span>}
|
||||
{resetAt && <span>{resetAt}</span>}
|
||||
</p>
|
||||
)}
|
||||
</li>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -60,6 +60,18 @@ function sanitizeProviderError(error) {
|
||||
return "Quota data is temporarily unavailable for this provider.";
|
||||
}
|
||||
|
||||
function hideQuotaResetDetails(data, user) {
|
||||
if (user.role === "admin") return data;
|
||||
|
||||
return {
|
||||
...data,
|
||||
providers: data.providers.map((provider) => ({
|
||||
...provider,
|
||||
quotas: provider.quotas.map(({ resetAt: _resetAt, recurring: _recurring, ...quota }) => quota),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
function buildSystemQuotaResponse(connections, results) {
|
||||
const providerGroups = new Map();
|
||||
|
||||
@@ -170,14 +182,14 @@ async function fetchConnectionQuota(connection) {
|
||||
*/
|
||||
export async function GET(request) {
|
||||
try {
|
||||
await requireUsageDashboardUser();
|
||||
const user = await requireUsageDashboardUser();
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const forceRefresh = searchParams.get("refresh") === "true";
|
||||
const cacheIsFresh = cachedSystemQuota && Date.now() - cachedSystemQuota.cachedAt < CACHE_TTL_MS;
|
||||
|
||||
if (!forceRefresh && cacheIsFresh) {
|
||||
return Response.json({ ...cachedSystemQuota.data, cached: true });
|
||||
return Response.json({ ...hideQuotaResetDetails(cachedSystemQuota.data, user), cached: true });
|
||||
}
|
||||
|
||||
const connections = (await getProviderConnections({})).filter(isUsageEligible);
|
||||
@@ -209,7 +221,7 @@ export async function GET(request) {
|
||||
const data = buildSystemQuotaResponse(connections, normalizedResults);
|
||||
cachedSystemQuota = { data, cachedAt: Date.now() };
|
||||
|
||||
return Response.json({ ...data, cached: false });
|
||||
return Response.json({ ...hideQuotaResetDetails(data, user), cached: false });
|
||||
} catch (error) {
|
||||
if (error?.message === "Unauthorized") {
|
||||
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
Reference in New Issue
Block a user