Consolidate and optimize data fetching for ProjectCard

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.
This commit is contained in:
Thomas Scholtes
2024-03-05 10:49:25 +01:00
committed by Rūdolfs Ošiņš
parent d315380e01
commit 67e16ea7dd
7 changed files with 94 additions and 147 deletions
+16 -23
View File
@@ -1,32 +1,25 @@
<script lang="ts">
import type { BaseUrl } from "@httpd-client";
import type { WeeklyActivity } from "@app/lib/commit";
import { formatTimestamp, twemoji } from "@app/lib/utils";
import ActivityDiagram from "@app/components/ActivityDiagram.svelte";
import IconSmall from "@app/components/IconSmall.svelte";
import Link from "@app/components/Link.svelte";
import type { ProjectInfo } from "./ProjectCard";
export let compact = false;
export let activity: WeeklyActivity[];
export let description: string;
export let baseUrl: BaseUrl;
export let numberOfIssues: number;
export let numberOfPatches: number;
export let projectInfo: ProjectInfo;
export let isDelegate: boolean;
export let isSeeding: boolean;
export let isPrivate: boolean;
export let lastUpdatedTimestamp: number;
$: lastUpdated = formatTimestamp(lastUpdatedTimestamp);
export let id: string;
export let name: string;
$: project = projectInfo.project;
$: baseUrl = projectInfo.baseUrl;
$: isPrivate = project.visibility?.type === "private";
$: lastUpdated = formatTimestamp(
projectInfo.lastCommit.commit.committer.time,
);
</script>
<style>
@@ -138,21 +131,21 @@
<Link
route={{
resource: "project.source",
project: id,
project: project.id,
node: baseUrl,
}}>
<div class="project-card" class:compact>
<div class="activity">
<div class="fadeout-overlay" />
<ActivityDiagram
{id}
id={project.id}
viewBoxHeight={200}
styleColor="var(--color-foreground-primary"
{activity} />
activity={projectInfo.activity} />
</div>
<div class="title">
<div class="headline-and-badges">
<h4 use:twemoji>{name}</h4>
<h4 use:twemoji>{project.name}</h4>
<div class="badges">
{#if isPrivate}
<div
@@ -180,14 +173,14 @@
{/if}
</div>
</div>
<p class="txt-small" use:twemoji>{description}</p>
<p class="txt-small" use:twemoji>{project.description}</p>
</div>
<div class="stats-row txt-tiny" style:color="var(--color-foreground-dim)">
<IconSmall name="issue" />
{numberOfIssues} ·
{project.issues.open} ·
<IconSmall name="patch" />
<span style:overflow="hidden" style:text-overflow="ellipsis">
{numberOfPatches} · Updated {lastUpdated}
{project.patches.open} · Updated {lastUpdated}
</span>
</div>
</div>
+38
View File
@@ -0,0 +1,38 @@
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;
});
}