mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +00:00
fix: update logic for showing the infomation
This commit is contained in:
@@ -65,17 +65,23 @@ export default function APIPageClient({ isAdmin }) {
|
|||||||
const fetchQuickStats = useCallback(async () => {
|
const fetchQuickStats = useCallback(async () => {
|
||||||
setQuickStatsLoading(true);
|
setQuickStatsLoading(true);
|
||||||
try {
|
try {
|
||||||
const [statsResponse, providersResponse] = await Promise.all([
|
const statsRequest = fetch("/api/usage/stats?period=today");
|
||||||
fetch("/api/usage/stats?period=today"),
|
const providersRequest = isAdmin ? fetch("/api/providers/client?pageSize=500") : null;
|
||||||
fetch("/api/providers/client?pageSize=500"),
|
const statsResponse = await statsRequest;
|
||||||
]);
|
|
||||||
|
|
||||||
if (!statsResponse.ok) throw new Error("Failed to fetch usage statistics");
|
if (!statsResponse.ok) throw new Error("Failed to fetch usage statistics");
|
||||||
|
|
||||||
const statsData = await statsResponse.json();
|
const statsData = await statsResponse.json();
|
||||||
setQuickStats(statsData);
|
setQuickStats(statsData);
|
||||||
|
|
||||||
if (providersResponse.ok) {
|
if (!providersRequest) {
|
||||||
|
setProviderSummary(null);
|
||||||
|
} else {
|
||||||
|
const providersResponse = await providersRequest;
|
||||||
|
if (!providersResponse.ok) {
|
||||||
|
setProviderSummary(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const providersData = await providersResponse.json();
|
const providersData = await providersResponse.json();
|
||||||
const connections = providersData.connections || [];
|
const connections = providersData.connections || [];
|
||||||
const activeConnections = connections.filter(
|
const activeConnections = connections.filter(
|
||||||
@@ -85,8 +91,6 @@ export default function APIPageClient({ isAdmin }) {
|
|||||||
active: activeConnections.length,
|
active: activeConnections.length,
|
||||||
total: providersData.pagination?.total ?? connections.length,
|
total: providersData.pagination?.total ?? connections.length,
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
setProviderSummary(null);
|
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
setQuickStats(null);
|
setQuickStats(null);
|
||||||
@@ -94,7 +98,7 @@ export default function APIPageClient({ isAdmin }) {
|
|||||||
} finally {
|
} finally {
|
||||||
setQuickStatsLoading(false);
|
setQuickStatsLoading(false);
|
||||||
}
|
}
|
||||||
}, []);
|
}, [isAdmin]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const initialize = window.setTimeout(() => {
|
const initialize = window.setTimeout(() => {
|
||||||
@@ -207,7 +211,7 @@ export default function APIPageClient({ isAdmin }) {
|
|||||||
<div className={`${styles.skeletonLine} h-3 w-4/5`} />
|
<div className={`${styles.skeletonLine} h-3 w-4/5`} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<QuickStatsBarSkeleton />
|
<QuickStatsBarSkeleton isAdmin={isAdmin} />
|
||||||
<div className={`${styles.skeletonPanel} flex flex-col gap-4`}>
|
<div className={`${styles.skeletonPanel} flex flex-col gap-4`}>
|
||||||
<div className={`${styles.skeletonLine} h-5 w-40`} />
|
<div className={`${styles.skeletonLine} h-5 w-40`} />
|
||||||
<div className={`${styles.skeletonLine} h-14 w-full`} />
|
<div className={`${styles.skeletonLine} h-14 w-full`} />
|
||||||
@@ -243,9 +247,9 @@ export default function APIPageClient({ isAdmin }) {
|
|||||||
<p className="sr-only" role="status" aria-live="polite">{copied ? "Value copied to clipboard." : ""}</p>
|
<p className="sr-only" role="status" aria-live="polite">{copied ? "Value copied to clipboard." : ""}</p>
|
||||||
|
|
||||||
{quickStatsLoading ? (
|
{quickStatsLoading ? (
|
||||||
<QuickStatsBarSkeleton />
|
<QuickStatsBarSkeleton isAdmin={isAdmin} />
|
||||||
) : (
|
) : (
|
||||||
<QuickStatsBar stats={quickStats} providerSummary={providerSummary} />
|
<QuickStatsBar stats={quickStats} providerSummary={providerSummary} isAdmin={isAdmin} />
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{loadError && (
|
{loadError && (
|
||||||
|
|||||||
@@ -47,11 +47,17 @@ const statDefinitions = [
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
export function QuickStatsBarSkeleton() {
|
function getVisibleStatDefinitions(isAdmin) {
|
||||||
|
return isAdmin ? statDefinitions : statDefinitions.filter((stat) => stat.key !== "providers");
|
||||||
|
}
|
||||||
|
|
||||||
|
export function QuickStatsBarSkeleton({ isAdmin = false }) {
|
||||||
|
const visibleStatDefinitions = getVisibleStatDefinitions(isAdmin);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className={`${styles.quickStats} animate-pulse`} aria-label="Loading today's gateway statistics" aria-busy="true">
|
<section className={`${styles.quickStats} animate-pulse`} aria-label="Loading today's gateway statistics" aria-busy="true">
|
||||||
<span className="sr-only">Loading today's gateway statistics.</span>
|
<span className="sr-only">Loading today's gateway statistics.</span>
|
||||||
{statDefinitions.map((stat) => (
|
{visibleStatDefinitions.map((stat) => (
|
||||||
<div key={stat.key} className={styles.statSkeleton} aria-hidden="true">
|
<div key={stat.key} className={styles.statSkeleton} aria-hidden="true">
|
||||||
<div className={`${styles.skeletonLine} size-8`} />
|
<div className={`${styles.skeletonLine} size-8`} />
|
||||||
<div className="min-w-0 space-y-2">
|
<div className="min-w-0 space-y-2">
|
||||||
@@ -65,13 +71,14 @@ export function QuickStatsBarSkeleton() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function QuickStatsBar({ stats, providerSummary = null }) {
|
export default function QuickStatsBar({ stats, providerSummary = null, isAdmin = false }) {
|
||||||
if (!stats) return null;
|
if (!stats) return null;
|
||||||
|
const visibleStatDefinitions = getVisibleStatDefinitions(isAdmin);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className={styles.quickStats} aria-labelledby="today-overview-title">
|
<section className={styles.quickStats} aria-labelledby="today-overview-title">
|
||||||
<h3 id="today-overview-title" className="sr-only">Today's gateway overview</h3>
|
<h3 id="today-overview-title" className="sr-only">Today's gateway overview</h3>
|
||||||
{statDefinitions.map((stat) => (
|
{visibleStatDefinitions.map((stat) => (
|
||||||
<article key={stat.key} className={styles.statCard}>
|
<article key={stat.key} className={styles.statCard}>
|
||||||
<span className={`${styles.statIcon} material-symbols-outlined`} aria-hidden="true">{stat.icon}</span>
|
<span className={`${styles.statIcon} material-symbols-outlined`} aria-hidden="true">{stat.icon}</span>
|
||||||
<p className={styles.statLabel}>{stat.label}</p>
|
<p className={styles.statLabel}>{stat.label}</p>
|
||||||
@@ -84,6 +91,7 @@ export default function QuickStatsBar({ stats, providerSummary = null }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
QuickStatsBar.propTypes = {
|
QuickStatsBar.propTypes = {
|
||||||
|
isAdmin: PropTypes.bool,
|
||||||
stats: PropTypes.shape({
|
stats: PropTypes.shape({
|
||||||
totalRequests: PropTypes.number,
|
totalRequests: PropTypes.number,
|
||||||
totalPromptTokens: PropTypes.number,
|
totalPromptTokens: PropTypes.number,
|
||||||
|
|||||||
Reference in New Issue
Block a user