Filter crawls, configs, browser profiles by user ID (#463)

* filter crawls by user id

* filter crawl configs by user id

* text change: Filter My Crawls -> Show Only Mine

Co-authored-by: Ilya Kreymer <ikreymer@users.noreply.github.com>
This commit is contained in:
sua yoo 2023-01-11 20:32:14 -08:00 committed by GitHub
parent 5efeaa58b1
commit a7457ca334
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 19 deletions

View File

@ -1,4 +1,4 @@
import type { HTMLTemplateResult } from "lit"; import type { HTMLTemplateResult, PropertyValueMap } from "lit";
import { state, property } from "lit/decorators.js"; import { state, property } from "lit/decorators.js";
import { msg, localized, str } from "@lit/localize"; import { msg, localized, str } from "@lit/localize";
import { parseCron } from "@cheap-glitch/mi-cron"; import { parseCron } from "@cheap-glitch/mi-cron";
@ -18,6 +18,7 @@ import {
humanizeSchedule, humanizeSchedule,
} from "../../utils/cron"; } from "../../utils/cron";
import "../../components/crawl-scheduler"; import "../../components/crawl-scheduler";
import { SlCheckbox } from "@shoelace-style/shoelace";
type RunningCrawlsMap = { type RunningCrawlsMap = {
/** Map of configId: crawlId */ /** Map of configId: crawlId */
@ -46,6 +47,9 @@ export class CrawlTemplatesList extends LiteElement {
@property({ type: String }) @property({ type: String })
archiveId!: string; archiveId!: string;
@property({ type: String })
userId!: string;
@state() @state()
crawlTemplates?: CrawlConfig[]; crawlTemplates?: CrawlConfig[];
@ -67,6 +71,9 @@ export class CrawlTemplatesList extends LiteElement {
direction: "desc", direction: "desc",
}; };
@state()
private filterByCurrentUser = true;
@state() @state()
private searchBy: string = ""; private searchBy: string = "";
@ -80,7 +87,11 @@ export class CrawlTemplatesList extends LiteElement {
threshold: 0.4, // stricter; default is 0.6 threshold: 0.4, // stricter; default is 0.6
}); });
async firstUpdated() { protected async willUpdate(changedProperties: Map<string, any>) {
if (
changedProperties.has("archiveId") ||
changedProperties.has("filterByCurrentUser")
) {
try { try {
this.crawlTemplates = await this.getCrawlTemplates(); this.crawlTemplates = await this.getCrawlTemplates();
@ -94,6 +105,7 @@ export class CrawlTemplatesList extends LiteElement {
}); });
} }
} }
}
render() { render() {
return html` return html`
@ -200,6 +212,21 @@ export class CrawlTemplatesList extends LiteElement {
</button> </button>
</div> </div>
<div class="flex items-center justify-end"> <div class="flex items-center justify-end">
${this.userId
? html`<label class="mr-3">
<span class="text-neutral-500 mr-1"
>${msg("Show Only Mine")}</span
>
<sl-switch
@sl-change=${(e: CustomEvent) =>
(this.filterByCurrentUser = (
e.target as SlCheckbox
).checked)}
?checked=${this.filterByCurrentUser}
></sl-switch>
</label>`
: ""}
<div class="whitespace-nowrap text-sm text-0-500 mr-2"> <div class="whitespace-nowrap text-sm text-0-500 mr-2">
${msg("Sort By")} ${msg("Sort By")}
</div> </div>
@ -553,8 +580,11 @@ export class CrawlTemplatesList extends LiteElement {
* associated with the crawl configs * associated with the crawl configs
**/ **/
private async getCrawlTemplates(): Promise<CrawlConfig[]> { private async getCrawlTemplates(): Promise<CrawlConfig[]> {
const params =
this.userId && this.filterByCurrentUser ? `?userid=${this.userId}` : "";
const data: { crawlConfigs: CrawlConfig[] } = await this.apiFetch( const data: { crawlConfigs: CrawlConfig[] } = await this.apiFetch(
`/archives/${this.archiveId}/crawlconfigs`, `/archives/${this.archiveId}/crawlconfigs${params}`,
this.authState! this.authState!
); );

View File

@ -12,6 +12,7 @@ import { RelativeDuration } from "../../components/relative-duration";
import type { AuthState } from "../../utils/AuthService"; import type { AuthState } from "../../utils/AuthService";
import LiteElement, { html } from "../../utils/LiteElement"; import LiteElement, { html } from "../../utils/LiteElement";
import type { Crawl, CrawlConfig, InitialCrawlConfig } from "./types"; import type { Crawl, CrawlConfig, InitialCrawlConfig } from "./types";
import { SlCheckbox } from "@shoelace-style/shoelace";
type CrawlSearchResult = { type CrawlSearchResult = {
item: Crawl; item: Crawl;
@ -50,6 +51,9 @@ export class CrawlsList extends LiteElement {
@property({ type: Object }) @property({ type: Object })
authState!: AuthState; authState!: AuthState;
@property({ type: String })
userId!: string;
// e.g. `/archive/${this.archiveId}/crawls` // e.g. `/archive/${this.archiveId}/crawls`
@property({ type: String }) @property({ type: String })
crawlsBaseUrl!: string; crawlsBaseUrl!: string;
@ -80,6 +84,9 @@ export class CrawlsList extends LiteElement {
direction: "desc", direction: "desc",
}; };
@state()
private filterByCurrentUser = true;
@state() @state()
private filterBy: string = ""; private filterBy: string = "";
@ -104,7 +111,8 @@ export class CrawlsList extends LiteElement {
if ( if (
changedProperties.has("shouldFetch") || changedProperties.has("shouldFetch") ||
changedProperties.get("crawlsBaseUrl") || changedProperties.get("crawlsBaseUrl") ||
changedProperties.get("crawlsAPIBaseUrl") changedProperties.get("crawlsAPIBaseUrl") ||
changedProperties.has("filterByCurrentUser")
) { ) {
if (this.shouldFetch) { if (this.shouldFetch) {
if (!this.crawlsBaseUrl) { if (!this.crawlsBaseUrl) {
@ -182,6 +190,21 @@ export class CrawlsList extends LiteElement {
</sl-input> </sl-input>
</div> </div>
<div class="col-span-12 md:col-span-1 flex items-center justify-end"> <div class="col-span-12 md:col-span-1 flex items-center justify-end">
${this.userId
? html`<label class="mr-3">
<span class="text-neutral-500 mr-1"
>${msg("Show Only Mine")}</span
>
<sl-switch
@sl-change=${(e: CustomEvent) =>
(this.filterByCurrentUser = (
e.target as SlCheckbox
).checked)}
?checked=${this.filterByCurrentUser}
></sl-switch>
</label>`
: ""}
<div class="whitespace-nowrap text-neutral-500 mr-2"> <div class="whitespace-nowrap text-neutral-500 mr-2">
${msg("Sort By")} ${msg("Sort By")}
</div> </div>
@ -554,13 +577,11 @@ export class CrawlsList extends LiteElement {
} }
private async getCrawls(): Promise<{ crawls: Crawl[] }> { private async getCrawls(): Promise<{ crawls: Crawl[] }> {
// Mock to use in dev: const params =
// return import("../../__mocks__/api/archives/[id]/crawls").then( this.userId && this.filterByCurrentUser ? `?userid=${this.userId}` : "";
// (module) => module.default
// );
const data = await this.apiFetch( const data = await this.apiFetch(
this.crawlsAPIBaseUrl || this.crawlsBaseUrl, `${this.crawlsAPIBaseUrl || this.crawlsBaseUrl}${params}`,
this.authState! this.authState!
); );

View File

@ -164,6 +164,7 @@ export class Archive extends LiteElement {
return html`<btrix-crawls-list return html`<btrix-crawls-list
.authState=${this.authState!} .authState=${this.authState!}
userId=${this.userInfo!.id}
crawlsBaseUrl=${crawlsBaseUrl} crawlsBaseUrl=${crawlsBaseUrl}
?shouldFetch=${this.archiveTab === "crawls"} ?shouldFetch=${this.archiveTab === "crawls"}
></btrix-crawls-list>`; ></btrix-crawls-list>`;
@ -196,6 +197,7 @@ export class Archive extends LiteElement {
return html`<btrix-crawl-templates-list return html`<btrix-crawl-templates-list
.authState=${this.authState!} .authState=${this.authState!}
.archiveId=${this.archiveId!} .archiveId=${this.archiveId!}
userId=${this.userInfo!.id}
></btrix-crawl-templates-list>`; ></btrix-crawl-templates-list>`;
} }