Skip to content

Commit e98592c

Browse files
committed
Fix page detection for new UI
1 parent 0046b51 commit e98592c

File tree

2 files changed

+44
-49
lines changed

2 files changed

+44
-49
lines changed

src/github-injector.ts

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { readCoverageData } from "./api";
2-
import { createButton, createButtonContent } from "./github/components";
2+
import { createButton, createButtonContent } from "./github/components/button";
33
import { SELECTOR_PR_REVIEW_TOOLS } from "./github/components/selectors";
44

55
const coverageData = new Map<string, FileCoverage>();
66

77
export function tryInjectDiffUI(): void {
88
try {
9-
tryInjectDiffPullRequestUI();
109
tryInjectDiffCommitUI();
1110

1211
// Set up observer for future tab changes
@@ -19,7 +18,6 @@ export function tryInjectDiffUI(): void {
1918
});
2019

2120
if (update) {
22-
tryInjectDiffPullRequestUI();
2321
tryInjectDiffCommitUI();
2422
}
2523
});
@@ -49,39 +47,6 @@ async function loadCoverageForPath(path: string): Promise<FileCoverage | null> {
4947
return coverage;
5048
}
5149

52-
function tryInjectDiffPullRequestUI() {
53-
document.querySelectorAll(".js-diff-progressive-container").forEach((el) => {
54-
tryInjectDiffPullRequestUIElement(el as HTMLElement);
55-
});
56-
57-
const prReviewToolsDiv = document.querySelector(
58-
SELECTOR_PR_REVIEW_TOOLS,
59-
) as HTMLDivElement;
60-
addNextUncoveredLineButton(prReviewToolsDiv);
61-
setupJumpToUncoveredLineHotkey();
62-
}
63-
64-
function tryInjectDiffPullRequestUIElement(
65-
rootElement: HTMLElement | null,
66-
): void {
67-
if (!rootElement) return;
68-
if (rootElement.classList.contains("qlty-diff-ui")) return;
69-
70-
rootElement
71-
.querySelectorAll('[data-details-container-group="file"]')
72-
.forEach(async (container) => {
73-
await injectIntoFileContainer(
74-
container.querySelector(".file-info") ?? container,
75-
container.getAttribute("data-tagsearch-path"),
76-
container.querySelectorAll("td[data-line-number].js-blob-rnum"),
77-
);
78-
});
79-
80-
console.log("[qlty] injected diff PR UI");
81-
addPRPageBadge();
82-
rootElement.classList.add("qlty-diff-ui");
83-
}
84-
8550
const uncoveredLineKeyListener = (event: KeyboardEvent) => {
8651
const ignoredTags = new Set(["INPUT", "TEXTAREA", "SELECT"]);
8752

@@ -114,19 +79,14 @@ function setupJumpToUncoveredLineHotkey() {
11479
document.addEventListener("keydown", uncoveredLineKeyListener);
11580
}
11681

117-
function addPRPageBadge(): void {
118-
const badge = createBadge("pr");
119-
if (!badge) return;
120-
document.querySelector(".gh-header-actions")?.prepend(badge);
121-
}
122-
12382
function tryInjectDiffCommitUI(): void {
124-
const rootElement = document.getElementById("diff-content-parent");
125-
if (!rootElement) return;
83+
const rootElement =
84+
document.getElementById("diff-content-parent") || document.body;
12685
if (rootElement.classList.contains("qlty-diff-ui")) return;
12786

12887
const links: Element[] = [];
12988
rootElement.querySelectorAll('a[href^="#diff-"').forEach((link) => {
89+
if (!link.classList.contains("Link--primary")) return;
13090
if (link.classList.contains("qlty-diff-link")) return; // Skip if already injected
13191
link.classList.add("qlty-diff-link");
13292
links.push(link);
@@ -144,16 +104,45 @@ function tryInjectDiffCommitUI(): void {
144104
link.parentElement?.parentElement ?? link,
145105
path,
146106
rootElement.querySelectorAll(
147-
`[data-diff-anchor="${fileId}"] tr.diff-line-row td:nth-last-child(2)`,
107+
`[data-diff-anchor="${fileId}"] td:nth-last-child(2)`,
148108
),
149109
);
150110
});
151111

152-
console.log("[qlty] injected diff commit UI");
153-
addDiffPageBadge();
112+
if (isPRPage()) {
113+
addPRPageBadge();
114+
addNextUncoveredLineButton();
115+
} else {
116+
addDiffPageBadge();
117+
}
118+
119+
console.log("[qlty] injected diff UI");
154120
rootElement.classList.add("qlty-diff-ui");
155121
}
156122

123+
function isPRPage(): boolean {
124+
return !!(
125+
document.querySelector(".pull-request-tab-content") ||
126+
document.querySelector("react-app[app-name='pull-request-files']")
127+
);
128+
}
129+
130+
function addPRPageBadge(): void {
131+
const badge = createBadge("pr");
132+
if (!badge) return;
133+
134+
const header = document.querySelector(".gh-header-actions");
135+
if (header) {
136+
header.prepend(badge);
137+
}
138+
139+
const actions = document.querySelector("[data-component=PH_Actions]");
140+
if (actions) {
141+
actions.insertBefore(badge, actions.querySelector("button")!);
142+
badge.style.order = "inherit";
143+
}
144+
}
145+
157146
function addDiffPageBadge(): void {
158147
const badge = createBadge("commit");
159148
if (!badge) return;
@@ -203,7 +192,12 @@ function jumpToNextUncoveredLine() {
203192
linkableLine.dispatchEvent(new MouseEvent("click", { bubbles: true }));
204193
}
205194

206-
function addNextUncoveredLineButton(prReviewToolsDiv: HTMLDivElement): void {
195+
function addNextUncoveredLineButton(): void {
196+
const prReviewToolsDiv = document.querySelector(SELECTOR_PR_REVIEW_TOOLS);
197+
if (!prReviewToolsDiv) {
198+
return;
199+
}
200+
207201
let existingButton = document.querySelector(".qlty-btn-next-uncovered-line");
208202
if (existingButton) {
209203
return;
@@ -235,6 +229,8 @@ function addNextUncoveredLineButton(prReviewToolsDiv: HTMLDivElement): void {
235229
} else {
236230
prReviewToolsDiv.appendChild(button);
237231
}
232+
233+
setupJumpToUncoveredLineHotkey();
238234
}
239235

240236
async function injectIntoFileContainer(

src/github/components/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)