11import { localized , msg } from "@lit/localize" ;
2- import { type SlSelect } from "@shoelace-style/shoelace" ;
2+ import type { SlSelect } from "@shoelace-style/shoelace" ;
33import { html } from "lit" ;
44import { customElement , property , state } from "lit/decorators.js" ;
5+ import { ifDefined } from "lit/directives/if-defined.js" ;
56
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 " ;
89
910type SelectCrawlerProxyChangeDetail = {
1011 value : string | null ;
@@ -26,30 +27,40 @@ export type SelectCrawlerProxyUpdateEvent =
2627 * Usage example:
2728 * ```ts
2829 * <btrix-select-crawler-proxy
29- * orgId =${orgId }
30- * on -change=${({value}) => selectedcrawlerProxy = value}
30+ * .proxyServers =${proxyServers }
31+ * btrix -change=${({value}) => selectedcrawlerProxy = value}
3132 * ></btrix-select-crawler-proxy>
3233 * ```
3334 *
34- * @event on -change
35+ * @fires btrix -change
3536 */
3637@customElement ( "btrix-select-crawler-proxy" )
3738@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+
3946 @property ( { type : String } )
4047 proxyId : string | null = null ;
4148
49+ @property ( { type : String } )
50+ size ?: SlSelect [ "size" ] ;
51+
4252 @state ( )
4353 private selectedProxy ?: Proxy ;
4454
4555 @state ( )
4656 private defaultProxy ?: Proxy ;
4757
48- @state ( )
49- private allProxies ?: Proxy [ ] ;
58+ public get value ( ) {
59+ return this . selectedProxy ?. id || "" ;
60+ }
5061
5162 protected firstUpdated ( ) {
52- void this . fetchOrgProxies ( ) ;
63+ void this . initProxies ( ) ;
5364 }
5465 // credit: https://dev.to/jorik/country-code-to-flag-emoji-a21
5566 private countryCodeToFlagEmoji ( countryCode : String ) : String {
@@ -61,10 +72,6 @@ export class SelectCrawlerProxy extends LiteElement {
6172 }
6273
6374 render ( ) {
64- /*if (this.crawlerProxys && this.crawlerProxys.length < 2) {
65- return html``;
66- }*/
67-
6875 return html `
6976 < sl-select
7077 name ="proxyId "
@@ -75,15 +82,12 @@ export class SelectCrawlerProxy extends LiteElement {
7582 : msg ( "No Proxy" ) }
7683 hoist
7784 clearable
85+ size=${ ifDefined ( this . size ) }
7886 @sl-change=${ this . onChange }
79- @sl-focus=${ ( ) => {
80- // Refetch to keep list up to date
81- void this . fetchOrgProxies ( ) ;
82- } }
8387 @sl-hide=${ this . stopProp }
8488 @sl-after-hide=${ this . stopProp }
8589 >
86- ${ this . allProxies ? .map (
90+ ${ this . proxyServers . map (
8791 ( server ) =>
8892 html ` < sl-option value =${ server . id } >
8993 ${ server . country_code
@@ -121,7 +125,7 @@ export class SelectCrawlerProxy extends LiteElement {
121125 private onChange ( e : Event ) {
122126 this . stopProp ( e ) ;
123127
124- this . selectedProxy = this . allProxies ? .find (
128+ this . selectedProxy = this . proxyServers . find (
125129 ( { id } ) => id === ( e . target as SlSelect ) . value ,
126130 ) ;
127131
@@ -130,71 +134,32 @@ export class SelectCrawlerProxy extends LiteElement {
130134 }
131135
132136 this . dispatchEvent (
133- new CustomEvent < SelectCrawlerProxyChangeDetail > ( "on -change" , {
137+ new CustomEvent < SelectCrawlerProxyChangeDetail > ( "btrix -change" , {
134138 detail : {
135139 value : this . selectedProxy ? this . selectedProxy . id : null ,
136140 } ,
137141 } ) ,
138142 ) ;
139143 }
140144
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 ,
183151 ) ;
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- } ) ;
191152 }
192- }
193153
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+ }
198163 }
199164
200165 /**
0 commit comments