mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-15 11:20:36 +08:00
We consolidate the code that fetches the data for `ProjectCard` in one module, adjacent to the `ProjectCard` component. We also consolidate all the data into one interface to simplify the component API. With the data loading in one place we can eliminate duplicate code and a handful of helper functions. The consolidation also allows us to discover some optimizations to fetching: We parallelize requests and we use the commit detail endpoint instead of the commit list endpoint. For the latter to pay off, we need to deploy caching to the API.
39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import { loadProjectActivity, type WeeklyActivity } from "@app/lib/commit";
|
|
import {
|
|
HttpdClient,
|
|
type BaseUrl,
|
|
type Commit,
|
|
type Project,
|
|
} from "@httpd-client";
|
|
|
|
export interface ProjectInfo {
|
|
project: Project;
|
|
baseUrl: BaseUrl;
|
|
activity: WeeklyActivity[];
|
|
lastCommit: Commit;
|
|
}
|
|
|
|
export async function fetchProjectInfos(
|
|
baseUrl: BaseUrl,
|
|
show: "all" | "pinned",
|
|
): Promise<ProjectInfo[]> {
|
|
const api = new HttpdClient(baseUrl);
|
|
const projects = await api.project.getAll({ show });
|
|
const info = await Promise.all(
|
|
projects.map(async project => {
|
|
const [activity, lastCommit] = await Promise.all([
|
|
loadProjectActivity(project.id, baseUrl),
|
|
api.project.getCommitBySha(project.id, project.head),
|
|
]);
|
|
return { project, activity, lastCommit, baseUrl };
|
|
}),
|
|
);
|
|
|
|
return info.sort((a, b) => {
|
|
const aLastCommit = a.lastCommit.commit.committer.time;
|
|
const bLastCommit = b.lastCommit.commit.committer.time;
|
|
|
|
return bLastCommit - aLastCommit;
|
|
});
|
|
}
|