@@ -2,7 +2,7 @@ import PropTypes from 'prop-types';
22import React from 'react' ;
33import '../css/diff-container.css' ;
44import {
5- fetchWithTimeout , twoDigits , getKeyByValue , getUTCDateFormat , getShortUTCDateFormat , jsonResponse
5+ fetchWithTimeout , getUTCDateFormat , getShortUTCDateFormat , jsonResponse
66} from '../js/utils.js' ;
77import Loading from './loading.jsx' ;
88import isNil from 'lodash/isNil' ;
@@ -55,8 +55,8 @@ export default class YmdTimestampHeader extends React.Component {
5555 timestampB,
5656 leftYear : timestampA ?. substring ( 0 , 4 ) ?? '' ,
5757 rightYear : timestampB ?. substring ( 0 , 4 ) ?? '' ,
58- leftMonthIndex : - 1 ,
59- rightMonthIndex : - 1
58+ leftMonth : timestampA ?. substring ( 4 , 6 ) ?? '' ,
59+ rightMonth : timestampB ?. substring ( 4 , 6 ) ?? ''
6060 } ;
6161 }
6262
@@ -130,7 +130,7 @@ export default class YmdTimestampHeader extends React.Component {
130130 < select className = "form-control input-sm mr-sm-1 month-select month-select-left"
131131 ref = { this . monthSelectLeft }
132132 onChange = { this . _handleMonthChange } title = "Months and available captures"
133- defaultValue = "" >
133+ value = { this . state . leftMonth } >
134134 < option value = "" disabled > Month</ option >
135135 { this . state . leftMonthOptions }
136136 </ select >
@@ -164,7 +164,7 @@ export default class YmdTimestampHeader extends React.Component {
164164 < select className = "form-control input-sm mr-sm-1 month-select month-select-right"
165165 ref = { this . monthSelectRight }
166166 onChange = { this . _handleMonthChange } title = "Months and available captures"
167- defaultValue = "" >
167+ value = { this . state . rightMonth } >
168168 < option value = "" disabled > Month</ option >
169169 { this . state . rightMonthOptions }
170170 </ select >
@@ -237,31 +237,29 @@ export default class YmdTimestampHeader extends React.Component {
237237 * aren't rendered yet.
238238 */
239239 _fetchCDXData = ( ) => {
240- let leftMonthIndex = - 1 ;
241- let rightMonthIndex = - 1 ;
242- if ( this . state . leftMonthIndex !== - 1 && this . monthSelectLeft . current ) {
243- const monthLeft = this . monthSelectLeft . current . value ;
244- leftMonthIndex = parseInt ( getKeyByValue ( monthNames , monthLeft ) ) ;
240+ let leftMonth = '' ;
241+ let rightMonth = '' ;
242+ if ( this . state . leftMonth !== '' && this . monthSelectLeft . current ) {
243+ leftMonth = this . monthSelectLeft . current . value ;
245244 } else if ( this . state . timestampA ) {
246- leftMonthIndex = parseInt ( this . state . timestampA . substring ( 4 , 6 ) ) ;
245+ leftMonth = this . state . timestampA . substring ( 4 , 6 ) ;
247246 }
248- if ( this . state . rightMonthIndex !== - 1 && this . monthSelectRight . current ) {
249- const monthRight = this . monthSelectRight . current . value ;
250- rightMonthIndex = parseInt ( getKeyByValue ( monthNames , monthRight ) ) ;
247+ if ( this . state . rightMonth !== '' && this . monthSelectRight . current ) {
248+ rightMonth = this . monthSelectRight . current . value ;
251249 } else if ( this . state . timestampB ) {
252- rightMonthIndex = parseInt ( this . state . timestampB . substring ( 4 , 6 ) ) ;
250+ rightMonth = this . state . timestampB . substring ( 4 , 6 ) ;
253251 }
254252
255- this . setState ( { leftMonthIndex , rightMonthIndex } ) ;
253+ this . setState ( { leftMonth , rightMonth } ) ;
256254
257255 let leftFetchPromise ;
258256 let rightFetchPromise ;
259257
260- if ( leftMonthIndex !== - 1 ) {
261- leftFetchPromise = this . fetchYearMonthCaptures ( this . state . leftYear , twoDigits ( leftMonthIndex ) ) ;
258+ if ( leftMonth !== '' ) {
259+ leftFetchPromise = this . fetchYearMonthCaptures ( this . state . leftYear , leftMonth ) ;
262260 }
263- if ( rightMonthIndex !== - 1 ) {
264- rightFetchPromise = this . fetchYearMonthCaptures ( this . state . rightYear , twoDigits ( rightMonthIndex ) ) ;
261+ if ( rightMonth !== '' ) {
262+ rightFetchPromise = this . fetchYearMonthCaptures ( this . state . rightYear , rightMonth ) ;
265263 }
266264
267265 if ( leftFetchPromise ) {
@@ -302,13 +300,24 @@ export default class YmdTimestampHeader extends React.Component {
302300 this . setState ( { leftSnaps, rightSnaps } ) ;
303301 } ;
304302
303+ /** Each item has [key, value, counter] **/
305304 _showOptions = ( data ) => {
306305 const limit = parseInt ( this . props . conf . limit ) ;
307306 return (
308307 data &&
309308 data . slice ( 0 ) . reverse ( ) . map ( ( item , index ) => {
310- const count = Math . min ( item [ 1 ] , limit ) ;
311- return < option key = { index } value = { item [ 0 ] } > { `${ item [ 0 ] } (${ count } )` } </ option > ;
309+ let key = '' , val = '' , cnt = 0 ;
310+ if ( item . length == 3 ) {
311+ key = item [ 0 ] ;
312+ val = item [ 1 ] ;
313+ cnt = item [ 2 ] ;
314+ } else {
315+ key = item [ 0 ] ;
316+ val = item [ 0 ] ;
317+ cnt = item [ 1 ] ;
318+ }
319+ const count = Math . min ( cnt , limit ) ;
320+ return < option key = { index } value = { key } > { `${ val } (${ count } )` } </ option > ;
312321 } )
313322 ) ;
314323 } ;
@@ -322,28 +331,20 @@ export default class YmdTimestampHeader extends React.Component {
322331 const { timestampA, timestampB, leftSnaps, rightSnaps } = this . state ;
323332 if ( ! isEmpty ( leftSnaps ) && timestampA ) {
324333 this . timestampSelectLeft . current . value = timestampA ;
334+ this . monthSelectLeft . current . value = this . state . leftMonth ;
325335 }
326336 if ( ! isEmpty ( rightSnaps ) && timestampB ) {
327337 this . timestampSelectRight . current . value = timestampB ;
338+ this . monthSelectRight . current . value = this . state . rightMonth ;
328339 }
329- this . _selectMonth ( this . monthSelectLeft . current , this . state . leftMonthIndex ) ;
330- this . _selectMonth ( this . monthSelectRight . current , this . state . rightMonthIndex ) ;
331340
332341 if ( this . state . sparkline && ! this . state . leftMonthOptions && ! this . state . rightMonthOptions ) {
333- if ( this . state . leftMonthIndex !== - 1 || this . state . rightMonthIndex !== - 1 ) {
342+ if ( this . state . leftMonth !== '' || this . state . rightMonth !== '' ) {
334343 this . _showMonths ( this . state . leftYear , this . state . rightYear ) ;
335344 }
336345 }
337346 } ;
338347
339- _selectMonth = ( monthSelect , monthIndex ) => {
340- if ( Array . from ( monthSelect . options ) . some ( option => option . value === monthNames [ monthIndex ] ) ) {
341- monthSelect . value = monthNames [ monthIndex ] ;
342- } else {
343- monthSelect . selectedIndex = 0 ;
344- }
345- } ;
346-
347348 _getHeaderInfo = ( firstTimestamp , lastTimestamp , count ) => {
348349 const first = getShortUTCDateFormat ( firstTimestamp ) ;
349350 const last = getShortUTCDateFormat ( lastTimestamp ) ;
@@ -391,11 +392,11 @@ export default class YmdTimestampHeader extends React.Component {
391392
392393 _showMonths = ( leftYear , rightYear ) => {
393394 if ( ! isNil ( leftYear ) ) {
394- const leftMonths = this . state . sparkline [ leftYear ] . filter ( val => val > 0 ) . map ( ( val , idx ) => [ monthNames [ idx + 1 ] , val ] ) ;
395+ const leftMonths = this . state . sparkline [ leftYear ] . filter ( val => val > 0 ) . map ( ( val , idx ) => [ String ( idx + 1 ) . padStart ( 2 , '0' ) , monthNames [ idx + 1 ] , val ] ) ;
395396 this . setState ( { leftMonthOptions : this . _showOptions ( leftMonths ) } ) ;
396397 }
397398 if ( ! isNil ( rightYear ) ) {
398- const rightMonths = this . state . sparkline [ rightYear ] . filter ( val => val > 0 ) . map ( ( val , idx ) => [ monthNames [ idx + 1 ] , val ] ) ;
399+ const rightMonths = this . state . sparkline [ rightYear ] . filter ( val => val > 0 ) . map ( ( val , idx ) => [ String ( idx + 1 ) . padStart ( 2 , '0' ) , monthNames [ idx + 1 ] , val ] ) ;
399400 this . setState ( { rightMonthOptions : this . _showOptions ( rightMonths ) } ) ;
400401 }
401402 } ;
@@ -418,14 +419,14 @@ export default class YmdTimestampHeader extends React.Component {
418419 _handleYearChange = ( e ) => {
419420 if ( e . target . id === 'year-select-left' ) {
420421 this . setState ( {
421- leftMonthIndex : 0 ,
422+ leftMonth : '' ,
422423 leftYear : e . target . value ,
423424 leftSnaps : null ,
424425 } ) ;
425426 this . _showMonths ( e . target . value , null ) ;
426427 } else {
427428 this . setState ( {
428- rightMonthIndex : 0 ,
429+ rightMonth : '' ,
429430 rightYear : e . target . value ,
430431 rightSnaps : null ,
431432 } ) ;
0 commit comments