@@ -21,6 +21,8 @@ const {getSetting} = require('../settings')
21
21
const { isIntermediateAboutPage} = require ( '../lib/appUrlUtil' )
22
22
const urlParse = require ( '../../app/common/urlParse' )
23
23
24
+ let hoverTimeout = null
25
+
24
26
const comparatorByKeyAsc = ( a , b ) => a . get ( 'key' ) > b . get ( 'key' )
25
27
? 1 : b . get ( 'key' ) > a . get ( 'key' ) ? - 1 : 0
26
28
@@ -397,9 +399,8 @@ function removeFrame (state, frameProps, framePropsIndex) {
397
399
}
398
400
}
399
401
400
- function getFrameTabPageIndex ( state , frameProps , tabsPerTabPage ) {
401
- frameProps = makeImmutable ( frameProps )
402
- const index = findNonPinnedDisplayIndexForFrameKey ( state , frameProps . get ( 'key' ) )
402
+ function getFrameTabPageIndex ( state , frameKey , tabsPerTabPage = getSetting ( settings . TABS_PER_PAGE ) ) {
403
+ const index = findNonPinnedDisplayIndexForFrameKey ( state , frameKey )
403
404
if ( index === - 1 ) {
404
405
return - 1
405
406
}
@@ -451,7 +452,8 @@ function isPinned (state, frameKey) {
451
452
* @param frameProps Any frame belonging to the page
452
453
*/
453
454
function updateTabPageIndex ( state , frameProps ) {
454
- const index = getFrameTabPageIndex ( state , frameProps , getSetting ( settings . TABS_PER_PAGE ) )
455
+ frameProps = makeImmutable ( frameProps )
456
+ const index = getFrameTabPageIndex ( state , frameProps . get ( 'key' ) )
455
457
456
458
if ( index === - 1 ) {
457
459
return state
@@ -532,7 +534,51 @@ const getTabPageCount = (state) => {
532
534
return Math . ceil ( frames . size / tabsPerPage )
533
535
}
534
536
537
+ const getPreviewFrameKey = ( state ) => {
538
+ return state . get ( 'previewFrameKey' )
539
+ }
540
+
541
+ const setPreviewFrameKey = ( state , frameKey ) => {
542
+ clearTimeout ( hoverTimeout )
543
+ const frame = getFrameByKey ( state , frameKey )
544
+ const isActive = isFrameKeyActive ( state , frameKey )
545
+ const previewTabs = getSetting ( settings . SHOW_TAB_PREVIEWS )
546
+ let newPreviewFrameKey = frameKey
547
+
548
+ if ( ! previewTabs || frame == null || ! frame . get ( 'hoverState' ) || isActive ) {
549
+ newPreviewFrameKey = null
550
+ }
551
+
552
+ // if there is an existing preview frame key then we're already in preview mode
553
+ // we use actions here because that is the only way to delay updating the state
554
+ const previewMode = getPreviewFrameKey ( state ) != null
555
+ if ( previewMode && newPreviewFrameKey == null ) {
556
+ // add a small delay when we are clearing the preview frame key so we don't lose
557
+ // previewMode if the user mouses over another tab - see below
558
+ this . hoverTimeout = setTimeout ( windowActions . setPreviewFrame . bind ( null , null ) , 200 )
559
+ return state
560
+ }
561
+
562
+ if ( ! previewMode ) {
563
+ // If user isn't in previewMode so we add a bit of delay to avoid tab from flashing out
564
+ // as reported here: https://github.com/brave/browser-laptop/issues/1434
565
+ // using an action here because that is the only way we can do a delayed state update
566
+ hoverTimeout = setTimeout ( windowActions . setPreviewFrame . bind ( null , newPreviewFrameKey ) , 200 )
567
+ return state
568
+ }
569
+
570
+ const index = getFrameTabPageIndex ( state , frame )
571
+ if ( index !== state . getIn ( [ 'ui' , 'tabs' , 'tabPageIndex' ] ) ) {
572
+ state = state . setIn ( [ 'ui' , 'tabs' , 'previewTabPageIndex' ] , index )
573
+ } else {
574
+ state = state . deleteIn ( [ 'ui' , 'tabs' , 'previewTabPageIndex' ] )
575
+ }
576
+ return state . set ( 'previewFrameKey' , newPreviewFrameKey )
577
+ }
578
+
535
579
module . exports = {
580
+ setPreviewFrameKey,
581
+ getPreviewFrameKey,
536
582
deleteTabInternalIndex,
537
583
deleteFrameInternalIndex,
538
584
updateFramesInternalIndex,
0 commit comments