返回 CodeWhale
project-pulse.tsx
根目录 / web / components / project-pulse.tsx
1 import { Icon } from "./icon";
2 import { LiveStarCount } from "./github-live";
3 import { GITHUB_STAR_COPY } from "@/lib/content/github-star";
4 import { PROJECT_COPY } from "@/lib/content/project";
5 import { relativeAge, type Face } from "@/lib/github";
6 import { fill, getChrome, pickText } from "@/lib/i18n/dictionaries";
7 import { REPO_URL } from "@/lib/i18n/links";
8 import type { FeedItem, RepoStats } from "@/lib/types";
9
10 const avatar = (url: string, size: number) => `${url}${url.includes("?") ? "&" : "?"}s=${size}`;
11
12 function age(iso: string, locale: string): string {
13 const { value, unit } = relativeAge(iso);
14 return new Intl.RelativeTimeFormat(getChrome(locale).dateLocale, { numeric: "auto" }).format(value, unit);
15 }
16
17 export function readAsOf(readAt: string | null | undefined, locale: string): string | undefined {
18 if (!readAt || !Number.isFinite(Date.parse(readAt))) return undefined;
19 const date = new Intl.DateTimeFormat(getChrome(locale).dateLocale, { dateStyle: "medium", timeZone: "UTC" }).format(new Date(readAt));
20 return fill(getChrome(locale).numbersAsOf, { date });
21 }
22
23 export function statsAsOf(stats: RepoStats | null, locale: string): string | undefined {
24 return readAsOf(stats?.fetchedAt, locale);
25 }
26
27 export function MergedList({ items, locale }: { items: FeedItem[]; locale: string; limit?: number }) {
28 if (items.length === 0) return null;
29 return (
30 <ol className="merged-list" role="list">
31 {items.map((item) => (
32 <li key={item.number}>
33 {/* eslint-disable-next-line @next/next/no-img-element */}
34 <img src={avatar(item.authorAvatar, 80)} alt={item.author} width={40} height={40} loading="lazy" decoding="async" className="face" />
35 <div className="merged-text">
36 <a href={item.url} className="merged-title">{item.title}</a>
37 <p className="merged-meta">
38 <a href={`https://github.com/${item.author}`}>@{item.author}</a>
39 <span>{fill(pickText(PROJECT_COPY.mergedAge, locale), { age: age(item.eventAt ?? item.updatedAt, locale) })}</span>
40 {item.firstTimeContributor ? <span className="merged-first">{pickText(PROJECT_COPY.firstContribution, locale)}</span> : null}
41 </p>
42 </div>
43 </li>
44 ))}
45 </ol>
46 );
47 }
48
49 export function FaceWall({ faces, total, locale }: { faces: Face[]; total: number; locale: string }) {
50 if (faces.length === 0) return null;
51 return (
52 <div className="face-wall">
53 <ul role="list">
54 {faces.map((face) => (
55 <li key={face.login}>
56 <a href={`https://github.com/${face.login}`} title={`@${face.login}`} aria-label={`@${face.login}`}>
57 {/* eslint-disable-next-line @next/next/no-img-element */}
58 <img src={avatar(face.avatar, 96)} alt={face.login} width={44} height={44} loading="lazy" decoding="async" className="face" />
59 </a>
60 </li>
61 ))}
62 </ul>
63 <a href={`${REPO_URL}/blob/main/docs/CONTRIBUTORS.md`} className="section-link">
64 {fill(pickText(PROJECT_COPY.facesAll, locale), { count: total })}
65 <Icon name="external" className="icon" />
66 </a>
67 </div>
68 );
69 }
70
71 export function GitHubStar({ stars, locale, asOf }: { stars?: number; locale: string; asOf?: string }) {
72 const label = pickText(GITHUB_STAR_COPY, locale);
73 return (
74 <a href={REPO_URL} className="btn btn-secondary github-star">
75 <span aria-hidden="true">☆</span>
76 <span>{label}</span>
77 <LiveStarCount initial={stars} numberLocale={getChrome(locale).dateLocale} asOf={asOf} />
78 </a>
79 );
80 }
81
81 lines Plain Text