Skip to content

Commit 9cfed7c

Browse files
authored
fix: Superadmin active crawl count inaccuracies (#2706)
- Fixes superadmin active crawl count not showing on first log in - Fixes `/all/crawls` endpoint running when auth is not available or not superadmin
1 parent 8152223 commit 9cfed7c

File tree

3 files changed

+63
-51
lines changed

3 files changed

+63
-51
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { localized } from "@lit/localize";
2+
import { Task } from "@lit/task";
3+
import { html } from "lit";
4+
import { customElement } from "lit/decorators.js";
5+
import queryString from "query-string";
6+
7+
import { BtrixElement } from "@/classes/BtrixElement";
8+
import type { APIPaginatedList } from "@/types/api";
9+
import type { Crawl } from "@/types/crawler";
10+
11+
const POLL_INTERVAL_SECONDS = 30;
12+
13+
@customElement("btrix-active-crawls-badge")
14+
@localized()
15+
export class ActiveCrawlsBadge extends BtrixElement {
16+
private readonly activeCrawlsTotalTask = new Task(this, {
17+
task: async () => {
18+
return await this.getActiveCrawlsTotal();
19+
},
20+
args: () => [] as const,
21+
});
22+
23+
private readonly pollTask = new Task(this, {
24+
task: async () => {
25+
window.clearTimeout(this.pollTask.value);
26+
27+
return window.setTimeout(() => {
28+
void this.activeCrawlsTotalTask.run();
29+
}, POLL_INTERVAL_SECONDS * 1000);
30+
},
31+
args: () => [this.activeCrawlsTotalTask.value] as const,
32+
});
33+
34+
disconnectedCallback(): void {
35+
super.disconnectedCallback();
36+
37+
window.clearTimeout(this.pollTask.value);
38+
}
39+
40+
render() {
41+
if (this.activeCrawlsTotalTask.value) {
42+
const { total } = this.activeCrawlsTotalTask.value;
43+
return html`<btrix-badge variant=${total > 0 ? "primary" : "blue"}>
44+
${this.localize.number(total)}
45+
</btrix-badge>`;
46+
}
47+
}
48+
49+
private async getActiveCrawlsTotal() {
50+
const query = queryString.stringify({
51+
pageSize: 1,
52+
});
53+
54+
const data = await this.api.fetch<APIPaginatedList<Crawl>>(
55+
`/orgs/all/crawls?${query}`,
56+
);
57+
58+
return data;
59+
}
60+
}

frontend/src/features/admin/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
import "./active-crawls-badge";
12
import "./stats";
23
import "./super-admin-banner";

frontend/src/index.ts

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import "./global";
33

44
import { provide } from "@lit/context";
55
import { localized, msg, str } from "@lit/localize";
6-
import { Task } from "@lit/task";
76
import type {
87
SlDialog,
98
SlDrawer,
@@ -15,7 +14,6 @@ import { ifDefined } from "lit/directives/if-defined.js";
1514
import { until } from "lit/directives/until.js";
1615
import { when } from "lit/directives/when.js";
1716
import isEqual from "lodash/fp/isEqual";
18-
import queryString from "query-string";
1917

2018
import "./components";
2119
import "./features";
@@ -35,9 +33,7 @@ import AuthService, {
3533
import { BtrixElement } from "@/classes/BtrixElement";
3634
import type { NavigateEventDetail } from "@/controllers/navigate";
3735
import type { NotifyEventDetail } from "@/controllers/notify";
38-
import type { APIPaginatedList } from "@/types/api";
3936
import { type Auth } from "@/types/auth";
40-
import type { Crawl } from "@/types/crawler";
4137
import {
4238
translatedLocales,
4339
type TranslatedLocaleEnum,
@@ -70,8 +66,6 @@ export interface UserGuideEventMap {
7066
"btrix-user-guide-show": CustomEvent<{ path?: string }>;
7167
}
7268

73-
const POLL_INTERVAL_SECONDS = 30;
74-
7569
@customElement("browsertrix-app")
7670
@localized()
7771
export class App extends BtrixElement {
@@ -114,24 +108,6 @@ export class App extends BtrixElement {
114108
@query("#userGuideDrawer")
115109
private readonly userGuideDrawer!: SlDrawer;
116110

117-
private readonly activeCrawlsTotalTask = new Task(this, {
118-
task: async () => {
119-
return await this.getActiveCrawlsTotal();
120-
},
121-
args: () => [] as const,
122-
});
123-
124-
private readonly pollTask = new Task(this, {
125-
task: async ([crawls]) => {
126-
if (!crawls) return;
127-
128-
return window.setTimeout(() => {
129-
void this.activeCrawlsTotalTask.run();
130-
}, POLL_INTERVAL_SECONDS * 1000);
131-
},
132-
args: () => [this.activeCrawlsTotalTask.value] as const,
133-
});
134-
135111
get orgSlugInPath() {
136112
return this.viewState.params.slug || "";
137113
}
@@ -188,12 +164,6 @@ export class App extends BtrixElement {
188164
this.startSyncBrowserTabs();
189165
}
190166

191-
disconnectedCallback(): void {
192-
super.disconnectedCallback();
193-
194-
window.clearTimeout(this.pollTask.value);
195-
}
196-
197167
private attachUserGuideListeners() {
198168
this.addEventListener(
199169
"btrix-user-guide-show",
@@ -479,7 +449,7 @@ export class App extends BtrixElement {
479449
}
480450

481451
private renderNavBar() {
482-
const isSuperAdmin = this.userInfo?.isSuperAdmin;
452+
const isSuperAdmin = this.authState && this.userInfo?.isSuperAdmin;
483453

484454
const showFullLogo =
485455
this.viewState.route === "login" || !this.authService.authState;
@@ -629,14 +599,7 @@ export class App extends BtrixElement {
629599
@click=${this.navigate.link}
630600
>
631601
${msg("Active Crawls")}
632-
${when(
633-
this.activeCrawlsTotalTask.value,
634-
(total) => html`
635-
<btrix-badge variant=${total > 0 ? "primary" : "blue"}>
636-
${this.localize.number(total)}
637-
</btrix-badge>
638-
`,
639-
)}
602+
<btrix-active-crawls-badge></btrix-active-crawls-badge>
640603
</a>
641604
</div>
642605
`
@@ -1183,16 +1146,4 @@ export class App extends BtrixElement {
11831146
private clearSelectedOrg() {
11841147
AppStateService.updateOrgSlug(null);
11851148
}
1186-
1187-
private async getActiveCrawlsTotal() {
1188-
const query = queryString.stringify({
1189-
pageSize: 1,
1190-
});
1191-
1192-
const data = await this.api.fetch<APIPaginatedList<Crawl>>(
1193-
`/orgs/all/crawls?${query}`,
1194-
);
1195-
1196-
return data.total;
1197-
}
11981149
}

0 commit comments

Comments
 (0)