@@ -17,6 +17,7 @@ import {
1717    RTCConnection , 
1818    TlsTunnel 
1919}  from  '../../types' ; 
20+ import  {  UiStore  }  from  '../../model/ui/ui-store' ; 
2021
2122import  { 
2223    getSummaryColor , 
@@ -54,6 +55,7 @@ interface ViewEventListProps {
5455    isPaused : boolean ; 
5556
5657    contextMenuBuilder : ViewEventContextMenuBuilder ; 
58+     uiStore : UiStore ; 
5759
5860    moveSelection : ( distance : number )  =>  void ; 
5961    onSelected : ( event : CollectedEvent  |  undefined )  =>  void ; 
@@ -767,6 +769,17 @@ export class ViewEventList extends React.Component<ViewEventListProps> {
767769    private  listBodyRef  =  React . createRef < HTMLDivElement > ( ) ; 
768770    private  listRef  =  React . createRef < List > ( ) ; 
769771
772+     private  setListBodyRef  =  ( element : HTMLDivElement  |  null )  =>  { 
773+         // Update the ref 
774+         ( this . listBodyRef  as  any ) . current  =  element ; 
775+ 
776+         // If the element is being mounted and we haven't restored state yet, do it now 
777+         if  ( element  &&  ! this . hasRestoredScrollState )  { 
778+             this . restoreScrollPosition ( ) ; 
779+             this . hasRestoredScrollState  =  true ; 
780+         } 
781+     } ; 
782+ 
770783    private  KeyBoundListWindow  =  observer ( 
771784        React . forwardRef < HTMLDivElement > ( 
772785            ( props : any ,  ref )  =>  < div 
@@ -816,7 +829,7 @@ export class ViewEventList extends React.Component<ViewEventListProps> {
816829                : < AutoSizer > { ( {  height,  width } )  => 
817830                    < Observer > { ( )  => 
818831                        < List 
819-                             innerRef = { this . listBodyRef } 
832+                             innerRef = { this . setListBodyRef } 
820833                            outerElementType = { this . KeyBoundListWindow } 
821834                            ref = { this . listRef } 
822835
@@ -881,17 +894,28 @@ export class ViewEventList extends React.Component<ViewEventListProps> {
881894    } 
882895
883896    private  wasListAtBottom  =  true ; 
897+ 
884898    private  updateScrolledState  =  ( )  =>  { 
885899        requestAnimationFrame ( ( )  =>  {  // Measure async, once the scroll has actually happened 
886900            this . wasListAtBottom  =  this . isListAtBottom ( ) ; 
901+ 
902+             // Only save scroll position after we've restored the initial state 
903+             if  ( this . hasRestoredScrollState )  { 
904+                 const  listWindow  =  this . listBodyRef . current ?. parentElement ; 
905+ 
906+                 const  scrollPosition  =  this . wasListAtBottom 
907+                     ? 'end' 
908+                     : ( listWindow ?. scrollTop  ??  'end' ) ; 
909+                 if  ( listWindow )  { 
910+                     this . props . uiStore . setViewScrollPosition ( scrollPosition ) ; 
911+                 } 
912+             } 
887913        } ) ; 
888914    } 
889915
890-     componentDidMount ( )  { 
891-         this . updateScrolledState ( ) ; 
892-     } 
916+     private  hasRestoredScrollState  =  false ; 
893917
894-     componentDidUpdate ( )  { 
918+     componentDidUpdate ( prevProps :  ViewEventListProps )  { 
895919        if  ( this . listBodyRef . current ?. parentElement ?. contains ( document . activeElement ) )  { 
896920            // If we previously had something here focused, and we've updated, update 
897921            // the focus too, to make sure it's in the right place. 
@@ -902,6 +926,25 @@ export class ViewEventList extends React.Component<ViewEventListProps> {
902926        // scroll there again ourselves now. 
903927        if  ( this . wasListAtBottom  &&  ! this . isListAtBottom ( ) )  { 
904928            this . listRef . current ?. scrollToItem ( this . props . events . length  -  1 ) ; 
929+         }  else  if  ( prevProps . selectedEvent  !==  this . props . selectedEvent  &&  this . props . selectedEvent )  { 
930+             // If the selected event changed and we have a selected event, scroll to it 
931+             // This handles restoring the selected event when returning to the tab 
932+             this . scrollToEvent ( this . props . selectedEvent ) ; 
933+         } 
934+     } 
935+ 
936+     private  restoreScrollPosition  =  ( )  =>  { 
937+         const  savedPosition  =  this . props . uiStore . viewScrollPosition ; 
938+         const  listWindow  =  this . listBodyRef . current ?. parentElement ; 
939+         if  ( listWindow )  { 
940+             if  ( savedPosition  ===  'end' )  { 
941+                 listWindow . scrollTop  =  listWindow . scrollHeight ; 
942+             }  else  { 
943+                 // Only restore if we're not close to the current position (avoid unnecessary scrolling) 
944+                 if  ( Math . abs ( listWindow . scrollTop  -  savedPosition )  >  10 )  { 
945+                     listWindow . scrollTop  =  savedPosition ; 
946+                 } 
947+             } 
905948        } 
906949    } 
907950
0 commit comments