import { useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; import { ConfigManager } from "@/lib/config/manager"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Loader2 } from "lucide-react"; export function HuggingFaceCallback() { const navigate = useNavigate(); const [status, setStatus] = useState<"loading" | "success" | "error">("loading"); const [error, setError] = useState(null); useEffect(() => { async function handleCallback() { try { // Get the authorization code from the URL const urlParams = new URLSearchParams(window.location.search); const code = urlParams.get("code"); if (!code) { setStatus("error"); setError("Authorization code not found in URL"); return; } // Exchange the code for an access token const response = await fetch("https://huggingface.co/oauth/token", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ grant_type: "authorization_code", client_id: import.meta.env.VITE_HF_CLIENT_ID, client_secret: import.meta.env.VITE_HF_CLIENT_SECRET, redirect_uri: import.meta.env.VITE_HF_REDIRECT_URI, code, }), }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.error_description || "Failed to exchange code for token"); } const data = await response.json(); const accessToken = data.access_token; // Store the access token in the config const configManager = ConfigManager.getInstance(); await configManager.updateConfig({ hf_token: accessToken }); setStatus("success"); } catch (err) { console.error("Error during OAuth callback:", err); setStatus("error"); setError(err instanceof Error ? err.message : "Unknown error occurred"); } } handleCallback(); }, [navigate]); const handleNavigateHome = () => { navigate("/"); }; return (
Hugging Face Integration {status === "loading" && "Setting up your Hugging Face integration..."} {status === "success" && "Successfully connected to Hugging Face!"} {status === "error" && "Failed to connect to Hugging Face"} {status === "loading" && } {status === "success" && ( <>
Your Hugging Face API token has been saved. You can now use Hugging Face models.
)} {status === "error" && ( <>
{error || "An error occurred while connecting to Hugging Face."}
)}
); }