1
1
import { localized , msg } from "@lit/localize" ;
2
- import { type SlSelect } from "@shoelace-style/shoelace" ;
2
+ import type { SlSelect } from "@shoelace-style/shoelace" ;
3
3
import { html } from "lit" ;
4
4
import { customElement , property , state } from "lit/decorators.js" ;
5
+ import { ifDefined } from "lit/directives/if-defined.js" ;
5
6
6
- import type { ProxiesAPIResponse , Proxy } from "@/pages/org/types " ;
7
- import LiteElement from "@/utils/LiteElement " ;
7
+ import { BtrixElement } from "@/classes/BtrixElement " ;
8
+ import type { Proxy } from "@/pages/org/types " ;
8
9
9
10
type SelectCrawlerProxyChangeDetail = {
10
11
value : string | null ;
@@ -26,30 +27,40 @@ export type SelectCrawlerProxyUpdateEvent =
26
27
* Usage example:
27
28
* ```ts
28
29
* <btrix-select-crawler-proxy
29
- * orgId =${orgId }
30
- * on -change=${({value}) => selectedcrawlerProxy = value}
30
+ * .proxyServers =${proxyServers }
31
+ * btrix -change=${({value}) => selectedcrawlerProxy = value}
31
32
* ></btrix-select-crawler-proxy>
32
33
* ```
33
34
*
34
- * @event on -change
35
+ * @fires btrix -change
35
36
*/
36
37
@customElement ( "btrix-select-crawler-proxy" )
37
38
@localized ( )
38
- export class SelectCrawlerProxy extends LiteElement {
39
+ export class SelectCrawlerProxy extends BtrixElement {
40
+ @property ( { type : String } )
41
+ defaultProxyId : string | null = null ;
42
+
43
+ @property ( { type : Array } )
44
+ proxyServers : Proxy [ ] = [ ] ;
45
+
39
46
@property ( { type : String } )
40
47
proxyId : string | null = null ;
41
48
49
+ @property ( { type : String } )
50
+ size ?: SlSelect [ "size" ] ;
51
+
42
52
@state ( )
43
53
private selectedProxy ?: Proxy ;
44
54
45
55
@state ( )
46
56
private defaultProxy ?: Proxy ;
47
57
48
- @state ( )
49
- private allProxies ?: Proxy [ ] ;
58
+ public get value ( ) {
59
+ return this . selectedProxy ?. id || "" ;
60
+ }
50
61
51
62
protected firstUpdated ( ) {
52
- void this . fetchOrgProxies ( ) ;
63
+ void this . initProxies ( ) ;
53
64
}
54
65
// credit: https://dev.to/jorik/country-code-to-flag-emoji-a21
55
66
private countryCodeToFlagEmoji ( countryCode : String ) : String {
@@ -61,10 +72,6 @@ export class SelectCrawlerProxy extends LiteElement {
61
72
}
62
73
63
74
render ( ) {
64
- /*if (this.crawlerProxys && this.crawlerProxys.length < 2) {
65
- return html``;
66
- }*/
67
-
68
75
return html `
69
76
< sl-select
70
77
name ="proxyId "
@@ -75,15 +82,12 @@ export class SelectCrawlerProxy extends LiteElement {
75
82
: msg ( "No Proxy" ) }
76
83
hoist
77
84
clearable
85
+ size=${ ifDefined ( this . size ) }
78
86
@sl-change=${ this . onChange }
79
- @sl-focus=${ ( ) => {
80
- // Refetch to keep list up to date
81
- void this . fetchOrgProxies ( ) ;
82
- } }
83
87
@sl-hide=${ this . stopProp }
84
88
@sl-after-hide=${ this . stopProp }
85
89
>
86
- ${ this . allProxies ? .map (
90
+ ${ this . proxyServers . map (
87
91
( server ) =>
88
92
html ` < sl-option value =${ server . id } >
89
93
${ server . country_code
@@ -121,7 +125,7 @@ export class SelectCrawlerProxy extends LiteElement {
121
125
private onChange ( e : Event ) {
122
126
this . stopProp ( e ) ;
123
127
124
- this . selectedProxy = this . allProxies ? .find (
128
+ this . selectedProxy = this . proxyServers . find (
125
129
( { id } ) => id === ( e . target as SlSelect ) . value ,
126
130
) ;
127
131
@@ -130,71 +134,32 @@ export class SelectCrawlerProxy extends LiteElement {
130
134
}
131
135
132
136
this . dispatchEvent (
133
- new CustomEvent < SelectCrawlerProxyChangeDetail > ( "on -change" , {
137
+ new CustomEvent < SelectCrawlerProxyChangeDetail > ( "btrix -change" , {
134
138
detail : {
135
139
value : this . selectedProxy ? this . selectedProxy . id : null ,
136
140
} ,
137
141
} ) ,
138
142
) ;
139
143
}
140
144
141
- /**
142
- * Fetch crawler proxies and update internal state
143
- */
144
- private async fetchOrgProxies ( ) : Promise < void > {
145
- try {
146
- const data = await this . getOrgProxies ( ) ;
147
- const defaultProxyId = data . default_proxy_id ;
148
-
149
- this . allProxies = data . servers ;
150
-
151
- if ( ! this . defaultProxy ) {
152
- this . defaultProxy = this . allProxies . find (
153
- ( { id } ) => id === defaultProxyId ,
154
- ) ;
155
- }
156
-
157
- if ( this . proxyId && ! this . selectedProxy ?. id ) {
158
- this . selectedProxy = this . allProxies . find (
159
- ( { id } ) => id === this . proxyId ,
160
- ) ;
161
- }
162
-
163
- if ( ! this . selectedProxy ) {
164
- this . proxyId = null ;
165
- this . dispatchEvent (
166
- new CustomEvent ( "on-change" , {
167
- detail : {
168
- value : null ,
169
- } ,
170
- } ) ,
171
- ) ;
172
- this . selectedProxy = this . allProxies . find (
173
- ( { id } ) => id === this . proxyId ,
174
- ) ;
175
- }
176
-
177
- this . dispatchEvent (
178
- new CustomEvent < SelectCrawlerProxyUpdateDetail > ( "on-update" , {
179
- detail : {
180
- show : this . allProxies . length > 1 ,
181
- } ,
182
- } ) ,
145
+ private async initProxies ( ) : Promise < void > {
146
+ const defaultProxyId = this . defaultProxyId ;
147
+
148
+ if ( ! this . defaultProxy ) {
149
+ this . defaultProxy = this . proxyServers . find (
150
+ ( { id } ) => id === defaultProxyId ,
183
151
) ;
184
- } catch ( e ) {
185
- this . notify ( {
186
- message : msg ( "Sorry, couldn't retrieve proxies at this time." ) ,
187
- variant : "danger" ,
188
- icon : "exclamation-octagon" ,
189
- id : "proxy-retrieve-status" ,
190
- } ) ;
191
152
}
192
- }
193
153
194
- private async getOrgProxies ( ) : Promise < ProxiesAPIResponse > {
195
- return this . apiFetch < ProxiesAPIResponse > (
196
- `/orgs/${ this . orgId } /crawlconfigs/crawler-proxies` ,
197
- ) ;
154
+ if ( this . proxyId && ! this . selectedProxy ) {
155
+ this . selectedProxy = this . proxyServers . find (
156
+ ( { id } ) => id === this . proxyId ,
157
+ ) ;
158
+ }
159
+
160
+ if ( ! this . selectedProxy ) {
161
+ this . proxyId = null ;
162
+ }
198
163
}
199
164
200
165
/**
0 commit comments