@@ -104,6 +104,17 @@ interface RuleContentSwitcherProps {
104104 size ?: 'small' | 'default' ;
105105}
106106
107+ const baseStyle = css `
108+ .semi-dropdown-item ,
109+ .semi-dropdown-title {
110+ max-width : 160px ;
111+ overflow : hidden;
112+ white-space : nowrap;
113+ text-overflow : ellipsis;
114+ display : block;
115+ }
116+ ` ;
117+
107118const smallStyle = css `
108119 .semi-dropdown-item {
109120 padding : 4px 8px ;
@@ -139,31 +150,82 @@ const RuleContentSwitcher: FC<RuleContentSwitcherProps> = props => {
139150 RULE_TYPE . MODIFY_SEND_HEADER ,
140151 ] . includes ( rule . ruleType ) ;
141152
142- const result : DropdownProps [ 'menu' ] = (
143- isValidArray ( value ) ? value : [ ]
144- ) . map ( x => ( {
145- node : 'item' ,
146- name :
147- typeof x === 'string'
148- ? x
149- : ( (
150- < div className = { tagList } >
151- { Object . keys ( x ) . map ( k => (
152- < Tag color = "grey" key = { k } size = "small" shape = "circle" >
153- { k } : { x [ k ] }
154- </ Tag >
155- ) ) }
156- </ div >
157- ) as any ) ,
158- onClick : ( ) => {
159- if ( isHeader ) {
160- updateRule ( 'headers' , x ) ;
153+ const getCommonHeaderKey = ( ) => {
154+ if ( ! isValidArray ( value ) ) {
155+ return ;
156+ }
157+ let k : string | undefined ;
158+ for ( const item of value ) {
159+ if ( typeof item !== 'object' ) {
160+ return ;
161+ }
162+ const keys = Object . keys ( item ) ;
163+ if ( keys . length === 1 ) {
164+ if ( ! k ) k = keys [ 0 ] ;
165+ if ( k !== keys [ 0 ] ) {
166+ return ;
167+ }
168+ } else {
169+ return ;
170+ }
171+ }
172+ return k ;
173+ } ;
174+
175+ const commonKey = getCommonHeaderKey ( ) ;
176+
177+ const result : DropdownProps [ 'menu' ] = [ ] ;
178+
179+ if ( commonKey ) {
180+ result . push ( {
181+ node : 'title' ,
182+ name : commonKey ,
183+ } ) ;
184+ }
185+
186+ if ( isValidArray ( value ) ) {
187+ value . forEach ( x => {
188+ let content : any = null ;
189+
190+ if ( typeof x === 'string' ) {
191+ content = x ;
161192 }
162- if ( type === RULE_TYPE . REDIRECT ) {
163- updateRule ( 'action' , x ) ;
193+
194+ const itemKeys = Object . keys ( x ) ;
195+ if ( commonKey && typeof x === 'object' ) {
196+ content = x [ commonKey ] ;
197+ }
198+
199+ if ( typeof x === 'object' && ! content ) {
200+ content = (
201+ < div className = { tagList } >
202+ { itemKeys . map ( k => (
203+ < Tag color = "grey" key = { k } size = "small" shape = "circle" >
204+ < div className = "item-tag" title = { `${ k } : ${ x [ k ] } ` } >
205+ < span className = "key" > { k } </ span >
206+ < span className = "sp" > :</ span >
207+ < span className = "value" > { x [ k ] } </ span >
208+ </ div >
209+ </ Tag >
210+ ) ) }
211+ </ div >
212+ ) ;
164213 }
165- } ,
166- } ) ) ;
214+
215+ result . push ( {
216+ node : 'item' ,
217+ name : content ,
218+ onClick : ( ) => {
219+ if ( isHeader ) {
220+ updateRule ( 'headers' , x ) ;
221+ }
222+ if ( type === RULE_TYPE . REDIRECT ) {
223+ updateRule ( 'action' , x ) ;
224+ }
225+ } ,
226+ } ) ;
227+ } ) ;
228+ }
167229
168230 if ( add ) {
169231 if ( result . length === 0 ) {
@@ -236,7 +298,7 @@ const RuleContentSwitcher: FC<RuleContentSwitcherProps> = props => {
236298
237299 return (
238300 < Dropdown
239- className = { cx ( {
301+ className = { cx ( baseStyle , {
240302 [ smallStyle ] : size === 'small' ,
241303 } ) }
242304 style = { { minWidth : '120px' } }
0 commit comments