11import { action , computed } from '@ember/object' ;
22import { tracked } from '@glimmer/tracking' ;
3- import { isEmpty } from '@ember/utils' ;
43import Controller from '@ember/controller' ;
54import { inject as service } from '@ember/service' ;
6- import escapeRegExp from 'ember-inspector/utils/escape-reg-exp' ;
7- import debounceComputed from 'ember-inspector/computed/debounce' ;
8- import { and , equal } from '@ember/object/computed' ;
5+
6+ import escapeRegExp from '../utils/escape-reg-exp' ;
7+ import debounceComputed from '../computed/debounce' ;
8+ import type WebExtension from '../services/adapters/web-extension' ;
9+ import type PortService from '../services/port' ;
10+ import type StorageService from '../services/storage' ;
11+ import type { RenderTreeModel } from '../routes/render-tree' ;
12+ import { isNullish } from '../utils/nullish' ;
913
1014export default class RenderTreeController extends Controller {
11- @service adapter ;
12- @service port ;
15+ @service declare adapter : WebExtension ;
16+ @service declare port : PortService ;
1317 /**
1418 * Storage is needed for remembering if the user closed the warning
15- *
16- * @property storage
17- * @type {Service }
1819 */
19- @service storage ;
20+ @service declare storage : StorageService ;
21+
22+ declare model : RenderTreeModel ;
2023
21- initialEmpty = false ;
24+ @ tracked initialEmpty = false ;
2225 @tracked shouldHighlightRender = false ;
2326 @tracked search = '' ;
2427
25- @equal ( 'model.profiles.length' , 0 )
26- modelEmpty ;
28+ get escapedSearch ( ) {
29+ return escapeRegExp ( this . search ?. toLowerCase ( ) ) ;
30+ }
2731
28- @and ( 'initialEmpty' , 'modelEmpty' )
29- showEmpty ;
32+ /**
33+ * Indicate the table's header's height in pixels.
34+ *
35+ * @property headerHeight
36+ * @type {Number }
37+ */
38+ get headerHeight ( ) {
39+ return this . isWarningClosed ? 31 : 56 ;
40+ }
3041
3142 /**
3243 * Checks if the user previously closed the warning by referencing localStorage
33- *
34- * @property isWarningClosed
35- * @type {Boolean }
3644 */
3745 get isWarningClosed ( ) {
3846 return ! ! this . storage . getItem ( 'is-render-tree-warning-closed' ) ;
3947 }
4048
4149 set isWarningClosed ( value ) {
50+ // @ts -expect-error Ignore this boolean/string mismatch for now.
4251 this . storage . setItem ( 'is-render-tree-warning-closed' , value ) ;
4352 }
4453
45- /**
46- * Indicate the table's header's height in pixels.
47- *
48- * @property headerHeight
49- * @type {Number }
50- */
51- get headerHeight ( ) {
52- return this . isWarningClosed ? 31 : 56 ;
54+ get modelEmpty ( ) {
55+ return this . model . profiles . length === 0 ;
56+ }
57+
58+ get showEmpty ( ) {
59+ return this . initialEmpty && this . modelEmpty ;
5360 }
5461
5562 // bound to the input field, updates the `search` property
5663 // 300ms after changing
5764 @debounceComputed ( 'search' , 300 )
58- searchValue ;
59-
60- get escapedSearch ( ) {
61- return escapeRegExp ( this . search ?. toLowerCase ( ) ) ;
62- }
65+ declare searchValue : string ;
6366
6467 @computed ( 'model.isHighlightSupported' )
6568 get isHighlightEnabled ( ) {
@@ -68,14 +71,16 @@ export default class RenderTreeController extends Controller {
6871
6972 @
computed ( 'escapedSearch' , '[email protected] ' , 'search' ) 7073 get filtered ( ) {
71- if ( isEmpty ( this . escapedSearch ) ) {
74+ if ( isNullish ( this . escapedSearch ) ) {
7275 return this . model . profiles ;
7376 }
7477
75- return this . model . profiles . filter ( ( item ) => {
76- const regExp = new RegExp ( this . escapedSearch ) ;
77- return recursiveMatch ( item , regExp ) ;
78- } ) ;
78+ return this . model . profiles . filter (
79+ ( item : RenderTreeModel [ 'profiles' ] [ number ] ) => {
80+ const regExp = new RegExp ( this . escapedSearch as string ) ;
81+ return recursiveMatch ( item , regExp ) ;
82+ } ,
83+ ) ;
7984 }
8085
8186 @action
@@ -85,7 +90,7 @@ export default class RenderTreeController extends Controller {
8590
8691 @action
8792 closeWarning ( ) {
88- this . set ( ' isWarningClosed' , true ) ;
93+ this . isWarningClosed = true ;
8994 }
9095
9196 @action
@@ -98,18 +103,19 @@ export default class RenderTreeController extends Controller {
98103 }
99104}
100105
101- function recursiveMatch ( item , regExp ) {
102- let children , child ;
103- let name = item . name ;
104- if ( name . toLowerCase ( ) . match ( regExp ) ) {
106+ function recursiveMatch (
107+ item : RenderTreeModel [ 'profiles' ] [ number ] ,
108+ regExp : string | RegExp ,
109+ ) {
110+ if ( item . name . toLowerCase ( ) . match ( regExp ) ) {
105111 return true ;
106112 }
107- children = item . children ;
108- for ( let i = 0 ; i < children . length ; i ++ ) {
109- child = children [ i ] ;
113+
114+ for ( const child of item . children ) {
110115 if ( recursiveMatch ( child , regExp ) ) {
111116 return true ;
112117 }
113118 }
119+
114120 return false ;
115121}
0 commit comments