Skip to content
This repository was archived by the owner on Aug 13, 2024. It is now read-only.

Commit 977df75

Browse files
committed
feat: add correct languages calculation and display
1 parent 4c7d29a commit 977df75

File tree

4 files changed

+45
-26
lines changed

4 files changed

+45
-26
lines changed

src/github/gql/get-user.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ query ($username: String!, $dateSince: DateTime) {
8080
primaryLanguage {
8181
name
8282
}
83-
languages(first: 10, orderBy: { field: SIZE, direction: DESC }) {
83+
languages(first: 100, orderBy: { field: SIZE, direction: DESC }) {
8484
edges {
8585
node {
8686
id

src/social-card/social-card.service.ts

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import userLangs from "./templates/user-langs";
77
import userProfileRepos from "./templates/user-profile-repos";
88
import userProfileCard from "./templates/user-profile-card";
99
import { GithubService } from "../github/github.service";
10-
import { Repository } from "@octokit/graphql-schema";
10+
import { Repository, Language } from "@octokit/graphql-schema";
1111

1212
@Injectable()
1313
export class SocialCardService {
@@ -17,19 +17,42 @@ export class SocialCardService {
1717
) {}
1818

1919
async getUserData (username: string): Promise<{
20-
langs: string[],
20+
langs: (Language & {
21+
size: number,
22+
})[],
23+
langTotal: number,
2124
repos: Repository[],
2225
avatarUrl: string,
2326
}> {
27+
const langs: Record<string, Language & {
28+
size: number,
29+
}> = {};
30+
const today = (new Date);
31+
const today30daysAgo = new Date((new Date).setDate(today.getDate() - 30));
2432
const user = await this.githubService.getUser(username);
25-
26-
/*
27-
* console.log(user);
28-
* console.log(user.repositories.nodes);
29-
*/
33+
const langRepos = user.repositories.nodes?.filter(repo => new Date(String(repo?.pushedAt)) > today30daysAgo) as Repository[];
34+
let langTotal = 0;
35+
36+
langRepos.map(repo => {
37+
repo.languages?.edges?.map(edge => {
38+
if (edge?.node.id) {
39+
langTotal += edge.size;
40+
41+
if (!Object.keys(langs).includes(edge.node.id)) {
42+
langs[edge.node.id] = {
43+
...edge.node,
44+
size: edge.size,
45+
};
46+
} else {
47+
langs[edge.node.id].size += edge.size;
48+
}
49+
}
50+
});
51+
});
3052

3153
return {
32-
langs: [],
54+
langs: Array.from(Object.values(langs)),
55+
langTotal,
3356
repos: user.topRepositories.nodes?.filter(repo => !repo?.isPrivate && repo?.owner.login !== username) as Repository[],
3457
avatarUrl: `${String(user.avatarUrl)}&size=150`,
3558
};
@@ -45,9 +68,9 @@ export class SocialCardService {
4568
const { html } = await import("satori-html");
4669
const satori = (await import("satori")).default;
4770

48-
const { avatarUrl, repos, langs } = await this.getUserData(username);
71+
const { avatarUrl, repos, langs, langTotal } = await this.getUserData(username);
4972

50-
const template = html(userProfileCard(avatarUrl, username, userLangs(langs), userProfileRepos(repos)));
73+
const template = html(userProfileCard(avatarUrl, username, userLangs(langs, langTotal), userProfileRepos(repos)));
5174

5275
const robotoArrayBuffer = await readFile("node_modules/@fontsource/roboto/files/roboto-latin-ext-400-normal.woff");
5376

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
import colors from "github-colors/colors.json";
1+
import { Language } from "@octokit/graphql-schema";
22

3-
const userLangs = (langs: string[], joinLiteral = "") => langs.map(lang => {
4-
const colorKey = Object.keys(colors).find(key => key.toLowerCase() === lang.toLowerCase());
5-
const color = colorKey ? colors[colorKey as keyof typeof colors].color ?? "#000" : "#000";
6-
7-
return `
8-
<div style="
9-
width: ${Math.round(100 / langs.length)}%;
10-
height: 10%;
11-
background: ${color};
12-
"/>
13-
`;
14-
}).join(joinLiteral);
3+
const userLangs = (langs: (Language & {
4+
size: number,
5+
})[], totalCount = 0, joinLiteral = "") => langs.map(({ color, size }) => `
6+
<div style="
7+
width: ${totalCount > 0 ? Math.round( size / totalCount * 100) : 100 / langs.length}%;
8+
height: 11px;
9+
background: ${color ?? "#000"};
10+
"/>`).join(joinLiteral);
1511

1612
export default userLangs;

src/social-card/templates/user-profile-card.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ const userProfileCard = (avatarUrl: string, name: string, langs: string, repos:
160160
gap: 8px;
161161
162162
width: 1136px;
163-
height: 10.5px;
163+
height: 11px;
164164
">
165165
<div style="
166166
@@ -171,7 +171,7 @@ const userProfileCard = (avatarUrl: string, name: string, langs: string, repos:
171171
gap: 4px;
172172
173173
width: 1136px;
174-
height: 10.5px;
174+
height: 11px;
175175
176176
border-radius: 20px;
177177
">

0 commit comments

Comments
 (0)