Skip to content

Commit 768953a

Browse files
authored
feat: Support new PR and diff UI (#46)
1 parent 3056153 commit 768953a

File tree

9 files changed

+79
-19
lines changed

9 files changed

+79
-19
lines changed

src/components/CommitDiff.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
export const CommitDiff = createDiffComponent({
22
getAdditionsElement: () =>
3-
document.querySelector<HTMLElement>("#toc>*>strong:nth-child(2)"),
3+
querySelectorFirst(
4+
// 2023
5+
"#toc>*>strong:nth-child(2)",
6+
// 2025-10-24
7+
"#diff-content-parent .fgColor-success",
8+
),
49
getDeletionsElement: () =>
5-
document.querySelector<HTMLElement>("#toc>*>strong:nth-child(3)"),
10+
querySelectorFirst(
11+
// 2023
12+
"#toc>*>strong:nth-child(3)",
13+
// 2025-10-24
14+
"#diff-content-parent .fgColor-danger",
15+
),
616
addSpinnerToPage(spinner) {
7-
const container = this.getDeletionsElement()?.parentElement;
8-
container?.appendChild(spinner);
17+
this.getDeletionsElement()?.after(spinner);
918
},
1019
getAdditionsText: (count) => i18n.t("diffs.additionsText", count),
1120
getDeletionsText: (count) => i18n.t("diffs.deletionsText", count),
12-
getGeneratedText: (count) => i18n.t("diffs.generatedText", count),
21+
getGeneratedText: (count) => i18n.t("diffs.generatedText", [count]),
1322
});

src/components/CompareDiff.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
export const CompareDiff = createDiffComponent({
22
getAdditionsElement: () =>
3-
document.querySelectorAll<HTMLElement>(".toc-diff-stats>strong")[0],
3+
querySelectorFirst(
4+
// 2023
5+
[".toc-diff-stats>strong", 0],
6+
// 2025-10-24
7+
),
48
getDeletionsElement: () =>
5-
document.querySelectorAll<HTMLElement>(".toc-diff-stats>strong")[1],
9+
querySelectorFirst(
10+
// 2023
11+
[".toc-diff-stats>strong", 1],
12+
// 2025-10-24
13+
),
614
addSpinnerToPage(spinner) {
715
const container = this.getDeletionsElement()?.parentElement;
816
container?.appendChild(spinner);
917
},
1018
getAdditionsText: (count) => i18n.t("diffs.additionsText", count),
1119
getDeletionsText: (count) => i18n.t("diffs.deletionsText", count),
12-
getGeneratedText: (count) => i18n.t("diffs.generatedText", count),
20+
getGeneratedText: (count) => i18n.t("diffs.generatedText", [count]),
1321
});

src/components/PrDiff.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
export const PrDiff = createDiffComponent({
22
getAdditionsElement: () =>
3-
document.querySelector<HTMLElement>("#diffstat .color-fg-success"),
3+
querySelectorFirst(
4+
// 2023
5+
"#diffstat .color-fg-success",
6+
// 2025-10-24
7+
"*[data-component=PH_Navigation] .f6.text-bold.fgColor-success",
8+
),
49
getDeletionsElement: () =>
5-
document.querySelector<HTMLElement>("#diffstat .color-fg-danger"),
10+
querySelectorFirst(
11+
// 2023
12+
"#diffstat .color-fg-danger",
13+
// 2025-10-24
14+
"*[data-component=PH_Navigation] .f6.text-bold.fgColor-danger",
15+
),
616
addSpinnerToPage(spinner) {
717
const deletions = this.getDeletionsElement();
818
deletions?.replaceWith(deletions, spinner);

src/components/createDiffComponent.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ export function createDiffComponent(options: {
4747
generated.textContent =
4848
" " + options.getGeneratedText(stats.exclude.changes);
4949
generated.style.color = GREY_COLOR;
50+
console.log(additions?.classList);
51+
generated.classList.add(
52+
...[...(additions?.classList ?? [])].filter(
53+
(className) => !className.toLowerCase().includes("fg"),
54+
),
55+
);
5056
const generatedAdditionsText = i18n.t("diffs.additionsSymbol", [
5157
stats.exclude.additions,
5258
]);

src/entrypoints/content.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
const mountId = Math.random();
2+
13
export default defineContentScript({
24
matches: ["*://*.github.com/*"],
35
runAt: "document_end",
46

5-
main() {
7+
main(ctx) {
68
main();
79
// TODO: schedule next interval for 1 second AFTER the main function finishes. If the main
810
// function takes more than 1 second, it might cause problems.
9-
setInterval(main, SECOND);
11+
const loop = ctx.setInterval(main, SECOND);
12+
ctx.setTimeout(() => {
13+
clearInterval(loop);
14+
}, 10 * SECOND);
1015
},
1116
});
1217

@@ -16,19 +21,19 @@ function main() {
1621
if (!repo || !owner) return;
1722

1823
const pr = getCurrentPr();
19-
if (pr) return replaceCount({ type: "pr", repo, owner, pr }, PrDiff);
24+
if (pr) return replaceCount({ mountId, type: "pr", repo, owner, pr }, PrDiff);
2025

2126
const commitHash = getCurrentRef();
2227
if (commitHash)
2328
return replaceCount(
24-
{ type: "commit", repo, owner, ref: commitHash },
29+
{ mountId, type: "commit", repo, owner, ref: commitHash },
2530
CommitDiff,
2631
);
2732

2833
const commitRefs = getCurrentCompare();
2934
if (commitRefs)
3035
return replaceCount(
31-
{ type: "compare", repo, owner, commitRefs },
36+
{ mountId, type: "compare", repo, owner, commitRefs },
3237
CompareDiff,
3338
);
3439
}

src/locales/en.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ diffs:
99
deletionsText:
1010
1: $1 deletion
1111
n: $1 deletions
12-
generatedText:
13-
1: $1 generated line.
14-
n: $1 generated lines.
12+
generatedText: $1 generated
1513
additionsSymbol: +$1
1614
deletionsSymbol: −$1
1715
generatedSymbol: ⌁$1

src/utils/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export const DEFAULT_CUSTOM_LIST_ALL = `*.lock
22
*.lock.*
33
*-lock*`;
44

5-
export const GREY_COLOR = "var(--color-fg-muted)";
5+
export const GREY_COLOR = "var(--color-fg-muted, var(--fgColor-muted))";
66
export const DIFF_COMPONENT_ID = "github-better-line-counts";

src/utils/github/service.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export const [registerGithubService, getGithubService] = defineProxyService(
1010
);
1111

1212
function createGithubService(api: GithubApi) {
13+
const mountCache: { [mountId: number]: RecalculateResult } = {};
14+
1315
/**
1416
* Returns a list of generated files that should be excluded from diff counts.
1517
*
@@ -122,11 +124,18 @@ function createGithubService(api: GithubApi) {
122124
async recalculateDiff(
123125
options: RecalculateOptions,
124126
): Promise<RecalculateResult> {
127+
// Cache the result if the same content script tries to get the result multiple times.
128+
if (mountCache[options.mountId]) {
129+
logger.debug("[recalculateDiff] Using mount cache");
130+
return mountCache[options.mountId];
131+
}
132+
125133
const ref = await getCurrentCommit(options);
126134
const cacheKey = getCacheKey(ref, options);
127135
const cached = await commitHashDiffsCache.get(cacheKey);
128136
if (cached) {
129137
logger.debug("[recalculateDiff] Using cached result");
138+
mountCache[options.mountId] = cached;
130139
return cached;
131140
}
132141

@@ -172,6 +181,8 @@ function createGithubService(api: GithubApi) {
172181
include: calculateDiffForFiles(include),
173182
};
174183
await commitHashDiffsCache.set(cacheKey, result, 2 * HOUR);
184+
185+
mountCache[options.mountId] = result;
175186
return result;
176187
},
177188

@@ -187,20 +198,23 @@ export type RecalculateOptions =
187198
| RecalculateCompareOptions;
188199

189200
export interface RecalculatePrOptions {
201+
mountId: number;
190202
type: "pr";
191203
owner: string;
192204
repo: string;
193205
pr: number;
194206
}
195207

196208
export interface RecalculateCommitOptions {
209+
mountId: number;
197210
type: "commit";
198211
owner: string;
199212
repo: string;
200213
ref: string;
201214
}
202215

203216
export interface RecalculateCompareOptions {
217+
mountId: number;
204218
type: "compare";
205219
owner: string;
206220
repo: string;

src/utils/querySelectorFirst.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function querySelectorFirst(
2+
...selectors: Array<string | [selectAll: string, index: number]>
3+
): HTMLElement | undefined {
4+
for (const selector of selectors) {
5+
const element = Array.isArray(selector)
6+
? document.querySelectorAll<HTMLElement>(selector[0])[selector[1]]
7+
: document.querySelector<HTMLElement>(selector);
8+
if (element) return element;
9+
}
10+
}

0 commit comments

Comments
 (0)