/ src / components / RepoCard.ts
RepoCard.ts
 1  import type { BaseUrl, Repo, RepoListQuery } from "@http-client";
 2  
 3  import { loadRepoActivity, type WeeklyActivity } from "@app/lib/commit";
 4  import { HttpdClient } from "@http-client";
 5  
 6  export interface RepoInfo {
 7    repo: Repo;
 8    baseUrl: BaseUrl;
 9    activity: WeeklyActivity[];
10  }
11  
12  export async function fetchRepoInfos(
13    baseUrl: BaseUrl,
14    query?: RepoListQuery,
15    delegate?: string,
16  ): Promise<RepoInfo[]> {
17    const api = new HttpdClient(baseUrl);
18    let repos: Repo[];
19  
20    if (delegate) {
21      repos = await api.repo.getByDelegate(delegate, query);
22    } else {
23      repos = await api.repo.getAll(query);
24    }
25    const info = await Promise.all(
26      repos
27        .filter(r => Boolean(r.payloads["xyz.radicle.project"]))
28        .map(async repo => {
29          const activity = await loadRepoActivity(repo.rid, baseUrl);
30          return { repo, activity, baseUrl };
31        }),
32    );
33  
34    return info.sort((a, b) => {
35      if (a.activity.length === 0 && b.activity.length === 0) {
36        return 0;
37      } else if (a.activity.length === 0 && b.activity.length > 0) {
38        return 1;
39      } else if (b.activity.length === 0 && a.activity.length > 0) {
40        return -1;
41      } else {
42        return b.activity[0].time - a.activity[0].time;
43      }
44    });
45  }