/* * Vencord, a Discord client mod * Copyright (c) 2025 Vendicated and contributors * SPDX-License-Identifier: GPL-3.0-or-later */ import { Flex } from "@components/Flex"; import { openModal } from "@utils/modal"; import { React, useEffect, UserProfileStore, useState } from "@webpack/common"; import { fetchReposByUserId, fetchReposByUsername, fetchUserInfo, GitHubUserInfo } from "../services/githubApi"; import { GitHubRepo } from "../types"; import { settings } from "../utils/settings"; import { RepoCard } from "./RepoCard"; import { ReposModal } from "./ReposModal"; export function GitHubReposComponent({ id, theme }: { id: string, theme: string; }) { const [repos, setRepos] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [userInfo, setUserInfo] = useState(null); const openReposModal = () => { if (!userInfo) return; const sortedRepos = [...repos].sort((a, b) => b.stargazers_count - a.stargazers_count); openModal(props => ( )); }; useEffect(() => { const fetchData = async () => { try { const profile = UserProfileStore.getUserProfile(id); if (!profile) { setLoading(false); return; } const connections = profile.connectedAccounts; if (!connections?.length) { setLoading(false); return; } const githubConnection = connections.find(conn => conn.type === "github"); if (!githubConnection) { setLoading(false); return; } const username = githubConnection.name; const userInfoData = await fetchUserInfo(username); if (userInfoData) { setUserInfo(userInfoData); } const githubId = githubConnection.id; // Try to fetch by ID first, fall back to username const reposById = await fetchReposByUserId(githubId); if (reposById) { setRepos(reposById); setLoading(false); return; } const reposByUsername = await fetchReposByUsername(username); setRepos(reposByUsername); setLoading(false); } catch (error) { const errorMessage = error instanceof Error ? error.message : "Failed to fetch repositories"; setError(errorMessage); setLoading(false); } }; fetchData(); }, [id]); if (loading) return
Loading repositories...
; if (error) return
Error: {error}
; if (!repos.length) return null; const topRepos = repos.slice(0, 3); return (
GitHub Repositories {userInfo && ( {` (${topRepos.length}/${userInfo.totalRepos})`} )}
{topRepos.map(repo => ( ))}
); }