fix(website): deduplicate contributor avatars
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
import { useState } from "react";
|
||||
import Link from "next/link";
|
||||
import type { Contributor } from "@/lib/contributors";
|
||||
import { dedupeContributors, type Contributor } from "@/lib/contributors";
|
||||
|
||||
function ContributorAvatar({ c }: { c: Contributor }) {
|
||||
const [hovered, setHovered] = useState(false);
|
||||
@@ -34,7 +34,8 @@ function ContributorAvatar({ c }: { c: Contributor }) {
|
||||
}
|
||||
|
||||
export function ContributorsWallContent({ contributors, title, desc }: { contributors: Contributor[]; title: string; desc: string }) {
|
||||
if (contributors.length === 0) return null;
|
||||
const uniqueContributors = dedupeContributors(contributors);
|
||||
if (uniqueContributors.length === 0) return null;
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -43,13 +44,13 @@ export function ContributorsWallContent({ contributors, title, desc }: { contrib
|
||||
<p className="mt-2 max-w-[650px] text-landing-muted text-sm leading-[1.65] justify-self-end text-right max-[760px]:max-w-none max-[760px]:text-left">
|
||||
{desc}{" "}
|
||||
<Link href="https://github.com/t8y2/dbx/graphs/contributors" target="_blank" className="landing-inline-link inline-flex items-center gap-[5px]">
|
||||
{contributors.length}+ contributors on GitHub
|
||||
{uniqueContributors.length}+ contributors on GitHub
|
||||
</Link>
|
||||
</p>
|
||||
</div>
|
||||
<div className="landing-contributor-grid">
|
||||
{contributors.map((c) => (
|
||||
<ContributorAvatar key={c.login} c={c} />
|
||||
{uniqueContributors.map((c) => (
|
||||
<ContributorAvatar key={c.login.toLowerCase()} c={c} />
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "vitest";
|
||||
import { dedupeContributors, type Contributor } from "./contributors";
|
||||
|
||||
const contributor = (login: string): Contributor => ({
|
||||
login,
|
||||
avatar_url: `https://avatars.githubusercontent.com/${login}`,
|
||||
html_url: `https://github.com/${login}`,
|
||||
contributions: 1,
|
||||
});
|
||||
|
||||
test("dedupeContributors removes duplicate GitHub logins case-insensitively", () => {
|
||||
const first = contributor("BlueSkyXN");
|
||||
|
||||
assert.deepEqual(dedupeContributors([first, contributor("blueskyxn"), contributor("other")]), [first, contributor("other")]);
|
||||
});
|
||||
|
||||
test("dedupeContributors preserves the original contributor order", () => {
|
||||
const contributors = [contributor("first"), contributor("second"), contributor("third")];
|
||||
|
||||
assert.deepEqual(dedupeContributors(contributors), contributors);
|
||||
});
|
||||
@@ -65,6 +65,17 @@ function fallbackContributors(): Contributor[] {
|
||||
];
|
||||
}
|
||||
|
||||
export function dedupeContributors(contributors: Contributor[]): Contributor[] {
|
||||
const seenLogins = new Set<string>();
|
||||
|
||||
return contributors.filter((contributor) => {
|
||||
const normalizedLogin = contributor.login.trim().toLowerCase();
|
||||
if (!normalizedLogin || seenLogins.has(normalizedLogin)) return false;
|
||||
seenLogins.add(normalizedLogin);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
export async function fetchContributors(): Promise<Contributor[]> {
|
||||
const token = process.env.GITHUB_TOKEN;
|
||||
const firstPage = `${GITHUB_CONTRIBUTORS_API}?per_page=100`;
|
||||
@@ -81,7 +92,7 @@ export async function fetchContributors(): Promise<Contributor[]> {
|
||||
|
||||
if (allContributors.length === 0) return fallbackContributors();
|
||||
|
||||
return allContributors.filter((c) => c.login && c.avatar_url && c.type !== "Bot");
|
||||
return dedupeContributors(allContributors.filter((c) => c.login && c.avatar_url && c.type !== "Bot"));
|
||||
} catch {
|
||||
return fallbackContributors();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user