2026-01-04 21:58:59 -05:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { useState, useEffect } from "react";
|
2026-01-19 05:15:11 -05:00
|
|
|
import Image from "next/image";
|
2026-01-19 18:31:08 -05:00
|
|
|
import PropTypes from "prop-types";
|
2026-01-18 22:39:30 -05:00
|
|
|
import { Card, CardSkeleton, Badge } from "@/shared/components";
|
2026-01-04 21:58:59 -05:00
|
|
|
import { OAUTH_PROVIDERS, APIKEY_PROVIDERS } from "@/shared/constants/config";
|
|
|
|
|
import Link from "next/link";
|
|
|
|
|
import { getErrorCode, getRelativeTime } from "@/shared/utils";
|
|
|
|
|
|
2026-01-19 18:31:08 -05:00
|
|
|
// Shared helper function to avoid code duplication between ProviderCard and ApiKeyProviderCard
|
|
|
|
|
function getStatusDisplay(connected, error, errorCode) {
|
|
|
|
|
const parts = [];
|
|
|
|
|
if (connected > 0) {
|
|
|
|
|
parts.push(
|
|
|
|
|
<Badge key="connected" variant="success" size="sm" dot>
|
|
|
|
|
{connected} Connected
|
|
|
|
|
</Badge>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (error > 0) {
|
|
|
|
|
const errText = errorCode ? `${error} Error (${errorCode})` : `${error} Error`;
|
|
|
|
|
parts.push(
|
|
|
|
|
<Badge key="error" variant="error" size="sm" dot>
|
|
|
|
|
{errText}
|
|
|
|
|
</Badge>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (parts.length === 0) {
|
|
|
|
|
return <span className="text-text-muted">No connections</span>;
|
|
|
|
|
}
|
|
|
|
|
return parts;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
export default function ProvidersPage() {
|
|
|
|
|
const [connections, setConnections] = useState([]);
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-01-19 05:15:11 -05:00
|
|
|
const fetchData = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const res = await fetch("/api/providers");
|
|
|
|
|
const data = await res.json();
|
|
|
|
|
if (res.ok) setConnections(data.connections || []);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log("Error fetching data:", error);
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
2026-01-04 21:58:59 -05:00
|
|
|
fetchData();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const getProviderStats = (providerId, authType) => {
|
|
|
|
|
const providerConnections = connections.filter(
|
|
|
|
|
c => c.provider === providerId && c.authType === authType
|
|
|
|
|
);
|
2026-01-06 09:44:14 -05:00
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
// Helper: check if connection is effectively active (cooldown expired)
|
|
|
|
|
const getEffectiveStatus = (conn) => {
|
|
|
|
|
const isCooldown = conn.rateLimitedUntil && new Date(conn.rateLimitedUntil).getTime() > Date.now();
|
|
|
|
|
return (conn.testStatus === "unavailable" && !isCooldown) ? "active" : conn.testStatus;
|
|
|
|
|
};
|
2026-01-06 09:44:14 -05:00
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
const connected = providerConnections.filter(c => {
|
|
|
|
|
const status = getEffectiveStatus(c);
|
|
|
|
|
return status === "active" || status === "success";
|
|
|
|
|
}).length;
|
2026-01-06 09:44:14 -05:00
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
const errorConns = providerConnections.filter(c => {
|
|
|
|
|
const status = getEffectiveStatus(c);
|
|
|
|
|
return status === "error" || status === "expired" || status === "unavailable";
|
|
|
|
|
});
|
2026-01-06 09:44:14 -05:00
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
const error = errorConns.length;
|
|
|
|
|
const total = providerConnections.length;
|
2026-01-06 09:44:14 -05:00
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
// Get latest error info
|
2026-01-06 09:44:14 -05:00
|
|
|
const latestError = errorConns.sort((a, b) =>
|
2026-01-04 21:58:59 -05:00
|
|
|
new Date(b.lastErrorAt || 0) - new Date(a.lastErrorAt || 0)
|
|
|
|
|
)[0];
|
|
|
|
|
const errorCode = latestError ? getErrorCode(latestError.lastError) : null;
|
|
|
|
|
const errorTime = latestError?.lastErrorAt ? getRelativeTime(latestError.lastErrorAt) : null;
|
2026-01-06 09:44:14 -05:00
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
return { connected, error, total, errorCode, errorTime };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex flex-col gap-8">
|
|
|
|
|
<CardSkeleton />
|
|
|
|
|
<CardSkeleton />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2026-01-06 09:44:14 -05:00
|
|
|
<div className="flex flex-col gap-6">
|
2026-01-18 22:39:30 -05:00
|
|
|
{/* OAuth Providers */}
|
|
|
|
|
<div className="flex flex-col gap-4">
|
|
|
|
|
<h2 className="text-xl font-semibold">OAuth Providers</h2>
|
|
|
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4">
|
|
|
|
|
{Object.entries(OAUTH_PROVIDERS).map(([key, info]) => (
|
|
|
|
|
<ProviderCard
|
|
|
|
|
key={key}
|
|
|
|
|
providerId={key}
|
|
|
|
|
provider={info}
|
|
|
|
|
stats={getProviderStats(key, "oauth")}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
2026-01-04 21:58:59 -05:00
|
|
|
</div>
|
|
|
|
|
|
2026-01-18 22:39:30 -05:00
|
|
|
{/* API Key Providers */}
|
|
|
|
|
<div className="flex flex-col gap-4">
|
|
|
|
|
<h2 className="text-xl font-semibold">API Key Providers</h2>
|
|
|
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4">
|
|
|
|
|
{Object.entries(APIKEY_PROVIDERS).map(([key, info]) => (
|
|
|
|
|
<ApiKeyProviderCard
|
|
|
|
|
key={key}
|
|
|
|
|
providerId={key}
|
|
|
|
|
provider={info}
|
|
|
|
|
stats={getProviderStats(key, "apikey")}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
2026-01-09 05:40:59 -05:00
|
|
|
</div>
|
2026-01-18 22:39:30 -05:00
|
|
|
</div>
|
2026-01-04 21:58:59 -05:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ProviderCard({ providerId, provider, stats }) {
|
|
|
|
|
const { connected, error, errorCode, errorTime } = stats;
|
|
|
|
|
const [imgError, setImgError] = useState(false);
|
|
|
|
|
|
|
|
|
|
return (
|
2026-01-31 00:58:04 -05:00
|
|
|
<Link href={`/dashboard/providers/${providerId}`} className="group">
|
|
|
|
|
<Card padding="sm" className="h-full hover:bg-black/[0.01] dark:hover:bg-white/[0.01] transition-colors cursor-pointer">
|
2026-01-04 21:58:59 -05:00
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
|
<div
|
2026-01-30 21:58:20 -05:00
|
|
|
className="size-8 rounded-lg flex items-center justify-center"
|
2026-01-04 21:58:59 -05:00
|
|
|
style={{ backgroundColor: `${provider.color}15` }}
|
|
|
|
|
>
|
2026-01-19 18:31:08 -05:00
|
|
|
{imgError ? (
|
|
|
|
|
<span
|
2026-01-30 21:58:20 -05:00
|
|
|
className="text-xs font-bold"
|
2026-01-19 18:31:08 -05:00
|
|
|
style={{ color: provider.color }}
|
|
|
|
|
>
|
|
|
|
|
{provider.textIcon || provider.id.slice(0, 2).toUpperCase()}
|
|
|
|
|
</span>
|
|
|
|
|
) : (
|
2026-01-19 05:15:11 -05:00
|
|
|
<Image
|
2026-01-04 21:58:59 -05:00
|
|
|
src={`/providers/${provider.id}.png`}
|
|
|
|
|
alt={provider.name}
|
2026-01-30 21:58:20 -05:00
|
|
|
width={32}
|
|
|
|
|
height={32}
|
|
|
|
|
className="object-contain rounded-lg max-w-[32px] max-h-[32px]"
|
|
|
|
|
sizes="32px"
|
2026-01-04 21:58:59 -05:00
|
|
|
onError={() => setImgError(true)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
<h3 className="font-semibold">{provider.name}</h3>
|
|
|
|
|
<div className="flex items-center gap-2 text-xs flex-wrap">
|
2026-01-19 18:31:08 -05:00
|
|
|
{getStatusDisplay(connected, error, errorCode)}
|
2026-01-04 21:58:59 -05:00
|
|
|
{errorTime && <span className="text-text-muted">• {errorTime}</span>}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-01-31 00:58:04 -05:00
|
|
|
<span className="material-symbols-outlined text-text-muted opacity-0 group-hover:opacity-100 transition-opacity">
|
2026-01-04 21:58:59 -05:00
|
|
|
chevron_right
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</Card>
|
|
|
|
|
</Link>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-19 18:31:08 -05:00
|
|
|
ProviderCard.propTypes = {
|
|
|
|
|
providerId: PropTypes.string.isRequired,
|
|
|
|
|
provider: PropTypes.shape({
|
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
|
color: PropTypes.string,
|
|
|
|
|
textIcon: PropTypes.string,
|
|
|
|
|
}).isRequired,
|
|
|
|
|
stats: PropTypes.shape({
|
|
|
|
|
connected: PropTypes.number,
|
|
|
|
|
error: PropTypes.number,
|
|
|
|
|
errorCode: PropTypes.string,
|
|
|
|
|
errorTime: PropTypes.string,
|
|
|
|
|
}).isRequired,
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-04 21:58:59 -05:00
|
|
|
// API Key providers - only use textIcon, no image
|
|
|
|
|
function ApiKeyProviderCard({ providerId, provider, stats }) {
|
|
|
|
|
const { connected, error, errorCode, errorTime } = stats;
|
|
|
|
|
|
|
|
|
|
return (
|
2026-01-31 00:58:04 -05:00
|
|
|
<Link href={`/dashboard/providers/${providerId}`} className="group">
|
|
|
|
|
<Card padding="sm" className="h-full hover:bg-black/[0.01] dark:hover:bg-white/[0.01] transition-colors cursor-pointer">
|
2026-01-04 21:58:59 -05:00
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
|
<div
|
2026-01-30 21:58:20 -05:00
|
|
|
className="size-8 rounded-lg flex items-center justify-center"
|
2026-01-04 21:58:59 -05:00
|
|
|
style={{ backgroundColor: `${provider.color}15` }}
|
|
|
|
|
>
|
|
|
|
|
<span
|
2026-01-30 21:58:20 -05:00
|
|
|
className="text-xs font-bold"
|
2026-01-04 21:58:59 -05:00
|
|
|
style={{ color: provider.color }}
|
|
|
|
|
>
|
|
|
|
|
{provider.textIcon || provider.id.slice(0, 2).toUpperCase()}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
<h3 className="font-semibold">{provider.name}</h3>
|
|
|
|
|
<div className="flex items-center gap-2 text-xs flex-wrap">
|
2026-01-19 18:31:08 -05:00
|
|
|
{getStatusDisplay(connected, error, errorCode)}
|
2026-01-04 21:58:59 -05:00
|
|
|
{errorTime && <span className="text-text-muted">• {errorTime}</span>}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-01-31 00:58:04 -05:00
|
|
|
<span className="material-symbols-outlined text-text-muted opacity-0 group-hover:opacity-100 transition-opacity">
|
2026-01-04 21:58:59 -05:00
|
|
|
chevron_right
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</Card>
|
|
|
|
|
</Link>
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-01-19 18:31:08 -05:00
|
|
|
|
|
|
|
|
ApiKeyProviderCard.propTypes = {
|
|
|
|
|
providerId: PropTypes.string.isRequired,
|
|
|
|
|
provider: PropTypes.shape({
|
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
|
color: PropTypes.string,
|
|
|
|
|
textIcon: PropTypes.string,
|
|
|
|
|
}).isRequired,
|
|
|
|
|
stats: PropTypes.shape({
|
|
|
|
|
connected: PropTypes.number,
|
|
|
|
|
error: PropTypes.number,
|
|
|
|
|
errorCode: PropTypes.string,
|
|
|
|
|
errorTime: PropTypes.string,
|
|
|
|
|
}).isRequired,
|
|
|
|
|
};
|