@@ -20,37 +20,39 @@ import { repeat } from "lit/directives/repeat.js";
2020import queryString from "query-string" ;
2121import { isFocusable } from "tabbable" ;
2222
23+ import type {
24+ BtrixChangeTagFilterEvent ,
25+ TagCount ,
26+ TagCounts ,
27+ TagType ,
28+ } from "./types" ;
29+
2330import { BtrixElement } from "@/classes/BtrixElement" ;
24- import type { BtrixChangeEvent } from "@/events/btrix-change" ;
25- import type { ArchivedItem } from "@/types/crawler" ;
26- import { type WorkflowTag , type WorkflowTags } from "@/types/workflow" ;
2731import { stopProp } from "@/utils/events" ;
2832import { isNotEqual } from "@/utils/is-not-equal" ;
2933import { tw } from "@/utils/tailwind" ;
3034
3135const MAX_TAGS_IN_LABEL = 5 ;
32-
33- type ChangeArchivedItemTagEventDetails =
34- | { tags : string [ ] ; type : "and" | "or" }
35- | undefined ;
36-
37- export type BtrixChangeArchivedItemTagFilterEvent =
38- BtrixChangeEvent < ChangeArchivedItemTagEventDetails > ;
36+ const apiPathForTagType : Record < TagType , string > = {
37+ workflow : "crawlconfigs" ,
38+ "workflow-crawl" : "crawls" ,
39+ "archived-item" : "all-crawls" ,
40+ "archived-item-crawl" : "crawls" ,
41+ upload : "uploads" ,
42+ profile : "profiles" ,
43+ } ;
3944
4045/**
4146 * @fires btrix-change
4247 */
43- @customElement ( "btrix-archived-item- tag-filter" )
48+ @customElement ( "btrix-tag-filter" )
4449@localized ( )
45- export class ArchivedItemTagFilter extends BtrixElement {
46- @property ( { type : Array } )
47- tags ?: string [ ] ;
48-
50+ export class TagFilter extends BtrixElement {
4951 @property ( { type : String } )
50- itemType ?: ArchivedItem [ "type" ] ;
52+ tagType ?: TagType ;
5153
52- @property ( { type : Boolean } )
53- includeNotSuccessful = false ;
54+ @property ( { type : Array } )
55+ tags ?: string [ ] ;
5456
5557 @state ( )
5658 private searchString = "" ;
@@ -61,7 +63,7 @@ export class ArchivedItemTagFilter extends BtrixElement {
6163 @queryAll ( "sl-checkbox" )
6264 private readonly checkboxes ! : NodeListOf < SlCheckbox > ;
6365
64- private readonly fuse = new Fuse < WorkflowTag > ( [ ] , {
66+ private readonly fuse = new Fuse < TagCount > ( [ ] , {
6567 keys : [ "tag" ] ,
6668 } ) ;
6769
@@ -82,14 +84,22 @@ export class ArchivedItemTagFilter extends BtrixElement {
8284 }
8385
8486 private readonly orgTagsTask = new Task ( this , {
85- task : async ( [ itemType ] , { signal } ) => {
86- const query = queryString . stringify ( {
87- onlySuccessful : ! this . includeNotSuccessful ,
88- crawlType : itemType ,
89- } ) ;
90-
91- const { tags } = await this . api . fetch < WorkflowTags > (
92- `/orgs/${ this . orgId } /all-crawls/tagCounts?${ query } ` ,
87+ task : async ( [ tagType ] , { signal } ) => {
88+ if ( ! tagType ) {
89+ console . debug ( "no tagType" ) ;
90+ return ;
91+ }
92+
93+ let query = "" ;
94+
95+ if ( tagType === "workflow-crawl" ) {
96+ query = queryString . stringify ( {
97+ onlySuccessful : false ,
98+ } ) ;
99+ }
100+
101+ const { tags } = await this . api . fetch < TagCounts > (
102+ `/orgs/${ this . orgId } /${ apiPathForTagType [ tagType ] } /tagCounts${ query && `?${ query } ` } ` ,
93103 { signal } ,
94104 ) ;
95105
@@ -98,7 +108,7 @@ export class ArchivedItemTagFilter extends BtrixElement {
98108 // Match fuse shape
99109 return tags . map ( ( item ) => ( { item } ) ) ;
100110 } ,
101- args : ( ) => [ this . itemType ] as const ,
111+ args : ( ) => [ this . tagType ] as const ,
102112 } ) ;
103113
104114 render ( ) {
@@ -149,9 +159,8 @@ export class ArchivedItemTagFilter extends BtrixElement {
149159 this . checkboxes . forEach ( ( checkbox ) => {
150160 checkbox . checked = false ;
151161 } ) ;
152-
162+ this . selected = new Map ( ) ;
153163 this . type = "or" ;
154-
155164 void this . dispatchChange ( ) ;
156165 } }
157166 > ${ msg ( "Clear" ) } </ sl-button
@@ -190,6 +199,8 @@ export class ArchivedItemTagFilter extends BtrixElement {
190199
191200 ${ this . orgTagsTask . render ( {
192201 complete : ( tags ) => {
202+ if ( ! tags ) return ;
203+
193204 let options = tags ;
194205
195206 if ( tags . length && this . searchString ) {
@@ -267,8 +278,8 @@ export class ArchivedItemTagFilter extends BtrixElement {
267278 ` ;
268279 }
269280
270- private renderList ( opts : { item : WorkflowTag } [ ] ) {
271- const tag = ( tag : WorkflowTag ) => {
281+ private renderList ( opts : { item : TagCount } [ ] ) {
282+ const tag = ( tag : TagCount ) => {
272283 const checked = this . selected . get ( tag . tag ) === true ;
273284
274285 return html `
@@ -314,9 +325,7 @@ export class ArchivedItemTagFilter extends BtrixElement {
314325 . filter ( ( [ _tag , selected ] ) => selected )
315326 . map ( ( [ tag ] ) => tag ) ;
316327 this . dispatchEvent (
317- new CustomEvent <
318- BtrixChangeEvent < ChangeArchivedItemTagEventDetails > [ "detail" ]
319- > ( "btrix-change" , {
328+ new CustomEvent < BtrixChangeTagFilterEvent [ "detail" ] > ( "btrix-change" , {
320329 detail : {
321330 value : selectedTags . length
322331 ? { tags : selectedTags , type : this . type }
0 commit comments