"use client"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Separator } from "@/components/ui/separator"; import { RefreshCw, GitBranch, Lock, Globe, ExternalLink, Building2, User, Github, LogIn, Plus, } from "lucide-react"; import { useGitHubAppProvider } from "@/providers/GitHubApp"; import { InstallationSelector } from "@/components/github/installation-selector"; import { cn } from "@/lib/utils"; export function GitHubManager() { const { isInstalled, isLoading, error, installations, currentInstallation, installationsLoading, installationsError, switchInstallation, refreshInstallations, repositories, repositoriesLoadingMore, refreshRepositories, loadMoreRepositories, repositoriesHasMore, } = useGitHubAppProvider(); const handleRefresh = async () => { await Promise.all([refreshRepositories(), refreshInstallations()]); }; const handleInstallApp = () => { window.location.href = "/api/github/installation"; }; const handleManageOnGitHub = () => { const appId = process.env.NEXT_PUBLIC_GITHUB_APP_CLIENT_ID; const fallbackInstallationsUrl = "https://github.com/settings/installations"; const applicationUrl = appId ? `https://github.com/settings/connections/applications/${appId}` : fallbackInstallationsUrl; window.open(applicationUrl, "_blank"); }; const handleLogin = () => { window.location.href = "/api/auth/github/login"; }; const isAuthError = (errorMessage: string | null) => { return ( errorMessage?.includes("GitHub access token not found") || errorMessage?.includes("Please authenticate first") || errorMessage?.includes("authentication") || errorMessage?.includes("access token") ); }; if (isAuthError(installationsError) || isAuthError(error)) { return (
GitHub Authentication Required Sign in with GitHub to access your repositories and manage installations

Connect Your GitHub Account

To manage your GitHub repositories and app installations, you need to authenticate with GitHub first. This will allow you to select repositories and work with your code.

); } if (isLoading || installationsLoading) { return (
GitHub Integration Loading your GitHub setup...
{[1, 2, 3].map((i) => (
))}
); } if ( (error && !isAuthError(error)) || (installationsError && !isAuthError(installationsError)) ) { return (
GitHub Integration Error {error || installationsError}
); } if (!isInstalled) { return (
GitHub App Installation Install the GitHub App to connect your repositories

Get Started with GitHub Integration

To access your GitHub repositories, you need to install our GitHub App and grant it access to the repositories you want to use.

); } return (
GitHub{" "} {currentInstallation?.accountType === "Organization" ? "Organization" : "User"} Manage your GitHub App installations and switch between organizations

Current{" "} {currentInstallation?.accountType === "Organization" ? "Organization" : "User"}

Select which GitHub organization or user account to work with

{currentInstallation && (
{`${currentInstallation.accountName}

{currentInstallation.accountName}

{currentInstallation.accountType === "Organization" ? ( <> Organization ) : ( <> Personal )}

{repositories.length} repositories accessible

)}
Your Repositories Repositories you have access to through your GitHub integration
{repositories.length} repositories
{repositories.length === 0 ? (

No repositories found. Make sure you've granted access to at least one repository for the selected organization.

) : ( <> {repositories.map((repo, index) => (

{repo.full_name}

{repo.private ? ( <> Private ) : ( <> Public )}
{repo.description && (

{repo.description}

)}
Default: {repo.default_branch}
{index < repositories.length - 1 && ( )}
))} {repositoriesHasMore && (
)} )}
GitHub App Management Manage your GitHub App installation and permissions

GitHub App Installation

You can manage your GitHub App installation, including adding or removing repositories, through GitHub.

); }