gitscope
← marzneshin / v0.6.0 / dashboard/src/features/github-repo/services/github-repo.query.ts
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/features/github-repo/services/github-repo.query.ts · TypeScript
Blame Raw
 1import { useQuery } from "@tanstack/react-query";
 2import { projectInfo } from "@marzneshin/utils";
 3import { ofetch } from "ofetch";
 4
 5/**
 6 * GithubRepoStatsResponse
 7 *
 8 * Github API repo statistics response
 9 *
10 * @param full_name Full name of the repo (author/repo-name)
11 * @param description Github repo description
12 * @param stargazers_count Github repo star count
13 */
14export interface GithubRepoStatsResponse {
15    full_name: string;
16    description: string;
17    stargazers_count: number;
18}
19
20export const GithubRepoStatsResponseDefault: GithubRepoStatsResponse = {
21    full_name: projectInfo.repo,
22    description: "A fork of Marzban aiming for scalability",
23    stargazers_count: 0,
24}
25
26export async function fetchGithubRepoStats(): Promise<GithubRepoStatsResponse> {
27    return await ofetch(`https://api.github.com/repos/${projectInfo.repo}`, { parseResponse: JSON.parse }).then((result) => {
28        return result;
29    });
30}
31
32
33export const useGithubRepoStatsQuery = () => {
34    return useQuery({
35        queryKey: ["github", projectInfo.repo, "stats"],
36        queryFn: fetchGithubRepoStats,
37        refetchInterval: 1000 * 60 * 5, // 5min refresh
38        initialData: GithubRepoStatsResponseDefault
39    })
40}