Something went wrong. Try again.
A modern frontend for Github repositories (ironically hosted on Tangled)
Something went wrong. Try again.
4.6 kB · 168 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169import { Octokit } from "octokit";
const useOctokit = () => { const { user } = useUserSession();
if (!(user.value as any)?.githubToken) { throw new Error("GitHub access token not found. Please login first."); }
return new Octokit({ auth: (user.value as any).githubToken });};
export const useAuthenticatedUser = async (token: string) => { const octokit = new Octokit({ auth: token }); const { data: { login }, } = await octokit.rest.users.getAuthenticated();
return login;};
export const useGithubOrg = (org: string) => { return useAsyncData( `github-org-${org}`, async () => { const octokit = useOctokit(); return octokit.rest.orgs.get({ org }); }, {}, );};export const useGithubRepo = (owner: string, repo: string) => { return useAsyncData( `github-repo-${owner}-${repo}`, async () => { const octokit = useOctokit(); return octokit.rest.repos.get({ owner, repo }); }, {}, );};
export const useGithubRepoReadme = (owner: string, repo: string) => { return useAsyncData( `github-readme-${owner}-${repo}`, async () => { const octokit = useOctokit(); try { // Fetch the README with HTML rendering const response = await octokit.rest.repos.getReadme({ owner, repo, mediaType: { format: "html" }, });
return { html: response.data as unknown as string, html_url: response.data.html_url || `https://github.com/${owner}/${repo}#readme`, }; } catch { return null; } }, {}, );};
export const useGithubRepoLanguages = (owner: string, repo: string) => { return useAsyncData( `github-languages-${owner}-${repo}`, async () => { const octokit = useOctokit(); return octokit.rest.repos.listLanguages({ owner, repo }); }, {}, );};
export const useGithubRepoContributors = (owner: string, repo: string) => { return useAsyncData( `github-contributors-${owner}-${repo}`, async () => { const octokit = useOctokit(); return octokit.rest.repos.listContributors({ owner, repo, per_page: 10 }); }, {}, );};
export const useGithubRepoReleases = (owner: string, repo: string) => { return useAsyncData( `github-releases-${owner}-${repo}`, async () => { const octokit = useOctokit(); return octokit.rest.repos.listReleases({ owner, repo, per_page: 5 }); }, {}, );};
export const useGithubRepoIssues = ( owner: string, repo: string, options: MaybeRef<{ state?: "open" | "closed" | "all"; sort?: "created" | "updated" | "comments"; direction?: "asc" | "desc"; per_page?: number; page?: number; }> = {},) => { return useAsyncData( () => `github-issues-${owner}-${repo}-${JSON.stringify(unref(options))}`, async () => { const octokit = useOctokit(); const opts = unref(options); return octokit.rest.issues.listForRepo({ owner, repo, state: opts.state || "open", sort: opts.sort || "created", direction: opts.direction || "desc", per_page: opts.per_page || 25, page: opts.page || 1, }); }, { watch: [() => unref(options)], }, );};
export const useGithubRepoIssuesCount = (owner: string, repo: string, state: "closed") => { return useAsyncData( `github-issues-count-${owner}-${repo}-${state}`, async () => { const octokit = useOctokit(); try { // Use the search API to get an accurate count of closed issues // This is more efficient than fetching all issues just for the count const searchQuery = `repo:${owner}/${repo} type:issue state:${state}`; const response = await octokit.rest.search.issuesAndPullRequests({ q: searchQuery, per_page: 1, // We only need the total_count, not the actual results }); return response.data.total_count; } catch { // Fallback: try to fetch first page of closed issues and estimate try { const response = await octokit.rest.issues.listForRepo({ owner, repo, state, per_page: 1, }); // This won't give us the exact count, but it's better than nothing // GitHub doesn't provide total count in the response headers for issues API return response.data.length > 0 ? 1 : 0; } catch { return 0; } } }, {}, );};