import "server-only";
import { lastActivity, log, type Commit } from "./git";
import { visibleParent, type RepoRecord } from "./repos";
/** Adds last-activity info to repositories for list views, and for forks the repository they came from if the viewer may see it. */
export async function withActivity(repos: RepoRecord[], viewerId?: string | null) {
return Promise.all(
repos.map(async (r) => {
const [last, parent] = await Promise.all([lastActivity(r.dir), visibleParent(r, viewerId)]);
return { ...r, updatedAt: last ?? r.createdAt, empty: last === null, parent };
}),
);
}
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);
}