"use client"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { AlertCircle, CheckCircle2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { useAuth } from "@/auth/clerk"; import { DashboardPageLayout } from "@/components/templates/DashboardPageLayout"; import { ForgejoConnectionsTable } from "@/components/git/ForgejoConnectionsTable"; import { ConfirmActionDialog } from "@/components/ui/confirm-action-dialog"; import { getForgejoConnections, deleteForgejoConnection, validateConnection, type ForgejoConnection, } from "@/lib/api-forgejo"; export default function ForgejoConnectionsPage() { const router = useRouter(); const auth = useAuth(); const [connections, setConnections] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const [notice, setNotice] = useState<{ tone: "success" | "error"; message: string; } | null>(null); const [deleteTarget, setDeleteTarget] = useState( null, ); const [deleteError, setDeleteError] = useState(null); const [isDeleting, setIsDeleting] = useState(false); useEffect(() => { const fetchConnections = async () => { try { setIsLoading(true); const data = await getForgejoConnections(); setConnections(data); setError(null); } catch (err) { setError( err instanceof Error ? err.message : "Failed to load connections", ); } finally { setIsLoading(false); } }; if (auth.isSignedIn) { fetchConnections(); } }, [auth.isSignedIn]); const handleDelete = (connection: ForgejoConnection) => { setDeleteError(null); setDeleteTarget(connection); }; const confirmDelete = async () => { if (!deleteTarget) return; setIsDeleting(true); setDeleteError(null); try { await deleteForgejoConnection(deleteTarget.id); setConnections((prev) => prev.filter((c) => c.id !== deleteTarget.id)); setNotice({ tone: "success", message: `Deleted "${deleteTarget.name}".`, }); setDeleteTarget(null); } catch (err) { setDeleteError( err instanceof Error ? err.message : "Failed to delete connection", ); } finally { setIsDeleting(false); } }; const handleValidateConnection = async (connection: ForgejoConnection) => { try { const result = await validateConnection(connection.id); if (result.status.ok) { setNotice({ tone: "success", message: `"${connection.name}" validated in ${Math.round(result.response_time_ms)}ms.`, }); } else { setNotice({ tone: "error", message: `Connection validation failed: ${result.status.error_message || "Unknown error"}`, }); } return result; } catch (err) { setNotice({ tone: "error", message: err instanceof Error ? err.message : "Failed to validate connection", }); throw err; } }; return ( <>
{notice ? (
{notice.tone === "success" ? ( ) : ( )} {notice.message}
) : null}

Connections

{error ? (

{error}

) : ( )}
{ if (!open) setDeleteTarget(null); }} title="Delete Git Project connection" description={ deleteTarget ? `Delete "${deleteTarget.name}" from Pipeline? Repositories that use this connection will stop syncing.` : "" } onConfirm={confirmDelete} isConfirming={isDeleting} errorMessage={deleteError} confirmLabel="Delete Connection" confirmingLabel="Deleting…" confirmClassName="bg-[color:var(--danger)] text-white hover:bg-[color:var(--danger)]/90" cancelLabel="Keep Connection" /> ); }