-
Notifications
You must be signed in to change notification settings - Fork 5
feat: Support new PR and diff UI #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,22 @@ | ||
| export const CommitDiff = createDiffComponent({ | ||
| getAdditionsElement: () => | ||
| document.querySelector<HTMLElement>("#toc>*>strong:nth-child(2)"), | ||
| querySelectorFirst( | ||
| // 2023 | ||
| "#toc>*>strong:nth-child(2)", | ||
| // 2025-10-24 | ||
| "#diff-content-parent .fgColor-success", | ||
| ), | ||
| getDeletionsElement: () => | ||
| document.querySelector<HTMLElement>("#toc>*>strong:nth-child(3)"), | ||
| querySelectorFirst( | ||
| // 2023 | ||
| "#toc>*>strong:nth-child(3)", | ||
| // 2025-10-24 | ||
| "#diff-content-parent .fgColor-danger", | ||
| ), | ||
| addSpinnerToPage(spinner) { | ||
| const container = this.getDeletionsElement()?.parentElement; | ||
| container?.appendChild(spinner); | ||
| this.getDeletionsElement()?.after(spinner); | ||
| }, | ||
| getAdditionsText: (count) => i18n.t("diffs.additionsText", count), | ||
| getDeletionsText: (count) => i18n.t("diffs.deletionsText", count), | ||
| getGeneratedText: (count) => i18n.t("diffs.generatedText", count), | ||
| getGeneratedText: (count) => i18n.t("diffs.generatedText", [count]), | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,21 @@ | ||
| export const CompareDiff = createDiffComponent({ | ||
| getAdditionsElement: () => | ||
| document.querySelectorAll<HTMLElement>(".toc-diff-stats>strong")[0], | ||
| querySelectorFirst( | ||
| // 2023 | ||
| [".toc-diff-stats>strong", 0], | ||
| // 2025-10-24 | ||
| ), | ||
| getDeletionsElement: () => | ||
| document.querySelectorAll<HTMLElement>(".toc-diff-stats>strong")[1], | ||
| querySelectorFirst( | ||
| // 2023 | ||
| [".toc-diff-stats>strong", 1], | ||
| // 2025-10-24 | ||
| ), | ||
| addSpinnerToPage(spinner) { | ||
| const container = this.getDeletionsElement()?.parentElement; | ||
| container?.appendChild(spinner); | ||
| }, | ||
| getAdditionsText: (count) => i18n.t("diffs.additionsText", count), | ||
| getDeletionsText: (count) => i18n.t("diffs.deletionsText", count), | ||
| getGeneratedText: (count) => i18n.t("diffs.generatedText", count), | ||
| getGeneratedText: (count) => i18n.t("diffs.generatedText", [count]), | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,8 @@ export const [registerGithubService, getGithubService] = defineProxyService( | |
| ); | ||
|
|
||
| function createGithubService(api: GithubApi) { | ||
| const mountCache: { [mountId: number]: RecalculateResult } = {}; | ||
|
|
||
| /** | ||
| * Returns a list of generated files that should be excluded from diff counts. | ||
| * | ||
|
|
@@ -122,11 +124,18 @@ function createGithubService(api: GithubApi) { | |
| async recalculateDiff( | ||
| options: RecalculateOptions, | ||
| ): Promise<RecalculateResult> { | ||
| // Cache the result if the same content script tries to get the result multiple times. | ||
| if (mountCache[options.mountId]) { | ||
| logger.debug("[recalculateDiff] Using mount cache"); | ||
| return mountCache[options.mountId]; | ||
| } | ||
|
Comment on lines
+127
to
+131
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was previously fetching the PR details every second, now it fetches only once per page load. |
||
|
|
||
| const ref = await getCurrentCommit(options); | ||
| const cacheKey = getCacheKey(ref, options); | ||
| const cached = await commitHashDiffsCache.get(cacheKey); | ||
| if (cached) { | ||
| logger.debug("[recalculateDiff] Using cached result"); | ||
| mountCache[options.mountId] = cached; | ||
| return cached; | ||
| } | ||
|
|
||
|
|
@@ -172,6 +181,8 @@ function createGithubService(api: GithubApi) { | |
| include: calculateDiffForFiles(include), | ||
| }; | ||
| await commitHashDiffsCache.set(cacheKey, result, 2 * HOUR); | ||
|
|
||
| mountCache[options.mountId] = result; | ||
| return result; | ||
| }, | ||
|
|
||
|
|
@@ -187,20 +198,23 @@ export type RecalculateOptions = | |
| | RecalculateCompareOptions; | ||
|
|
||
| export interface RecalculatePrOptions { | ||
| mountId: number; | ||
| type: "pr"; | ||
| owner: string; | ||
| repo: string; | ||
| pr: number; | ||
| } | ||
|
|
||
| export interface RecalculateCommitOptions { | ||
| mountId: number; | ||
| type: "commit"; | ||
| owner: string; | ||
| repo: string; | ||
| ref: string; | ||
| } | ||
|
|
||
| export interface RecalculateCompareOptions { | ||
| mountId: number; | ||
| type: "compare"; | ||
| owner: string; | ||
| repo: string; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| export function querySelectorFirst( | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hopefully this helper will reduce lines changed of future UI changes. |
||
| ...selectors: Array<string | [selectAll: string, index: number]> | ||
| ): HTMLElement | undefined { | ||
| for (const selector of selectors) { | ||
| const element = Array.isArray(selector) | ||
| ? document.querySelectorAll<HTMLElement>(selector[0])[selector[1]] | ||
| : document.querySelector<HTMLElement>(selector); | ||
| if (element) return element; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fallback on the new PR UI's grey variable if the old one doesn't exist.