Skip to content

Commit

Permalink
fix crawling defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
SuaYoo committed Jan 7, 2025
1 parent 42ce97c commit 7420a39
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 61 deletions.
81 changes: 25 additions & 56 deletions frontend/src/components/ui/select-crawler-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ export type SelectCrawlerProxyUpdateEvent =
* Usage example:
* ```ts
* <btrix-select-crawler-proxy
* orgId=${orgId}
* on-change=${({value}) => selectedcrawlerProxy = value}
* .proxyServers=${proxyServers}
* btrix-change=${({value}) => selectedcrawlerProxy = value}
* ></btrix-select-crawler-proxy>
* ```
*
* @event on-change
* @fires btrix-change
*/
@customElement("btrix-select-crawler-proxy")
@localized()
Expand All @@ -55,8 +55,12 @@ export class SelectCrawlerProxy extends BtrixElement {
@state()
private defaultProxy?: Proxy;

public get value() {
return this.selectedProxy?.id || "";
}

protected firstUpdated() {
void this.fetchOrgProxies();
void this.initProxies();
}
// credit: https://dev.to/jorik/country-code-to-flag-emoji-a21
private countryCodeToFlagEmoji(countryCode: String): String {
Expand All @@ -68,10 +72,6 @@ export class SelectCrawlerProxy extends BtrixElement {
}

render() {
/*if (this.crawlerProxys && this.crawlerProxys.length < 2) {
return html``;
}*/

return html`
<sl-select
name="proxyId"
Expand All @@ -84,10 +84,6 @@ export class SelectCrawlerProxy extends BtrixElement {
clearable
size=${ifDefined(this.size)}
@sl-change=${this.onChange}
@sl-focus=${() => {
// Refetch to keep list up to date
void this.fetchOrgProxies();
}}
@sl-hide=${this.stopProp}
@sl-after-hide=${this.stopProp}
>
Expand Down Expand Up @@ -138,58 +134,31 @@ export class SelectCrawlerProxy extends BtrixElement {
}

this.dispatchEvent(
new CustomEvent<SelectCrawlerProxyChangeDetail>("on-change", {
new CustomEvent<SelectCrawlerProxyChangeDetail>("btrix-change", {
detail: {
value: this.selectedProxy ? this.selectedProxy.id : null,
},
}),
);
}

private async fetchOrgProxies(): Promise<void> {
try {
const defaultProxyId = this.defaultProxyId;

if (!this.defaultProxy) {
this.defaultProxy = this.proxyServers.find(
({ id }) => id === defaultProxyId,
);
}

if (this.proxyId && !this.selectedProxy?.id) {
this.selectedProxy = this.proxyServers.find(
({ id }) => id === this.proxyId,
);
}

if (!this.selectedProxy) {
this.proxyId = null;
this.dispatchEvent(
new CustomEvent("on-change", {
detail: {
value: null,
},
}),
);
this.selectedProxy = this.proxyServers.find(
({ id }) => id === this.proxyId,
);
}

this.dispatchEvent(
new CustomEvent<SelectCrawlerProxyUpdateDetail>("on-update", {
detail: {
show: this.proxyServers.length > 1,
},
}),
private async initProxies(): Promise<void> {
const defaultProxyId = this.defaultProxyId;

if (!this.defaultProxy) {
this.defaultProxy = this.proxyServers.find(
({ id }) => id === defaultProxyId,
);
} catch (e) {
this.notify.toast({
message: msg("Sorry, couldn't retrieve proxies at this time."),
variant: "danger",
icon: "exclamation-octagon",
id: "proxy-retrieve-status",
});
}

if (this.proxyId && !this.selectedProxy) {
this.selectedProxy = this.proxyServers.find(
({ id }) => id === this.proxyId,
);
}

if (!this.selectedProxy) {
this.proxyId = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ export class NewBrowserProfileDialog extends LiteElement {
)}
.proxyServers=${this.proxies.servers}
.proxyId="${this.proxyId || ""}"
@on-change=${(e: SelectCrawlerProxyChangeEvent) =>
(this.proxyId = e.detail.value!)}
@btrix-change=${(e: SelectCrawlerProxyChangeEvent) =>
(this.proxyId = e.detail.value)}
></btrix-select-crawler-proxy>
</div>
`
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/features/crawl-workflows/workflow-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,7 @@ https://archiveweb.page/images/${"logo.svg"}`}
)}
.proxyServers=${this.proxies.servers}
.proxyId="${this.formState.proxyId || ""}"
@on-change=${(e: SelectCrawlerProxyChangeEvent) =>
@btrix-change=${(e: SelectCrawlerProxyChangeEvent) =>
this.updateFormState({
proxyId: e.detail.value,
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ifDefined } from "lit/directives/if-defined.js";

import { BtrixElement } from "@/classes/BtrixElement";
import type { LanguageSelect } from "@/components/ui/language-select";
import type { SelectCrawlerProxy } from "@/components/ui/select-crawler-proxy";
import { proxiesContext, type ProxiesContext } from "@/context/org";
import type { QueueExclusionTable } from "@/features/crawl-workflows/queue-exclusion-table";
import { columns, type Cols } from "@/layouts/columns";
Expand Down Expand Up @@ -65,6 +66,9 @@ export class OrgSettingsCrawlWorkflows extends BtrixElement {
@query("btrix-language-select")
languageSelect?: LanguageSelect | null;

@query("btrix-select-crawler-proxy")
proxySelect?: SelectCrawlerProxy | null;

@query('sl-button[type="submit"]')
submitButton?: SlButton | null;

Expand All @@ -75,7 +79,6 @@ export class OrgSettingsCrawlWorkflows extends BtrixElement {
}

render() {
console.log("crawling defaults:", this.proxies);
return html` ${this.renderWorkflowDefaults()} `;
}

Expand Down Expand Up @@ -305,7 +308,7 @@ export class OrgSettingsCrawlWorkflows extends BtrixElement {
blockAds: values.blockAds === "on",
profileid: values.profileid,
crawlerChannel: values.crawlerChannel,
proxyId: values.proxyId,
proxyId: this.proxySelect?.value || undefined,
userAgent: values.userAgent,
lang: this.languageSelect?.value || undefined,
exclude: this.exclusionTable?.exclusions?.filter((v) => v) || [],
Expand Down

0 comments on commit 7420a39

Please sign in to comment.