Vectrum
Sign In
29 lines · 1016 Bytes · TypeScript
import "server-only";
import { lastActivity, log, type Commit } from "./git";
import type { RepoRecord } from "./repos";

/** Adds last-activity info to repositories for list views. */
export async function withActivity(repos: RepoRecord[]) {
  return Promise.all(
    repos.map(async (r) => {
      const last = await lastActivity(r.dir);
      return { ...r, updatedAt: last ?? r.createdAt, empty: last === null };
    }),
  );
}

export type ActivityItem = { repo: RepoRecord; commit: Commit };

/** The most recent commits across the given repositories, newest first. */
export async function recentActivity(repos: RepoRecord[], limit = 20): Promise<ActivityItem[]> {
  const perRepo = await Promise.all(
    repos.slice(0, 20).map(async (repo) => {
      const commits = await log(repo.dir, "--branches", { limit: 8 }).catch(() => []);
      return commits.map((commit) => ({ repo, commit }));
    }),
  );
  return perRepo
    .flat()
    .sort((a, b) => b.commit.date - a.commit.date)
    .slice(0, limit);
}