@@ -17,6 +17,7 @@ import {
17
17
RTCConnection ,
18
18
TlsTunnel
19
19
} from '../../types' ;
20
+ import { UiStore } from '../../model/ui/ui-store' ;
20
21
21
22
import {
22
23
getSummaryColor ,
@@ -54,6 +55,7 @@ interface ViewEventListProps {
54
55
isPaused : boolean ;
55
56
56
57
contextMenuBuilder : ViewEventContextMenuBuilder ;
58
+ uiStore : UiStore ;
57
59
58
60
moveSelection : ( distance : number ) => void ;
59
61
onSelected : ( event : CollectedEvent | undefined ) => void ;
@@ -767,6 +769,17 @@ export class ViewEventList extends React.Component<ViewEventListProps> {
767
769
private listBodyRef = React . createRef < HTMLDivElement > ( ) ;
768
770
private listRef = React . createRef < List > ( ) ;
769
771
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
+
770
783
private KeyBoundListWindow = observer (
771
784
React . forwardRef < HTMLDivElement > (
772
785
( props : any , ref ) => < div
@@ -816,7 +829,7 @@ export class ViewEventList extends React.Component<ViewEventListProps> {
816
829
: < AutoSizer > { ( { height, width } ) =>
817
830
< Observer > { ( ) =>
818
831
< List
819
- innerRef = { this . listBodyRef }
832
+ innerRef = { this . setListBodyRef }
820
833
outerElementType = { this . KeyBoundListWindow }
821
834
ref = { this . listRef }
822
835
@@ -881,17 +894,28 @@ export class ViewEventList extends React.Component<ViewEventListProps> {
881
894
}
882
895
883
896
private wasListAtBottom = true ;
897
+
884
898
private updateScrolledState = ( ) => {
885
899
requestAnimationFrame ( ( ) => { // Measure async, once the scroll has actually happened
886
900
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
+ }
887
913
} ) ;
888
914
}
889
915
890
- componentDidMount ( ) {
891
- this . updateScrolledState ( ) ;
892
- }
916
+ private hasRestoredScrollState = false ;
893
917
894
- componentDidUpdate ( ) {
918
+ componentDidUpdate ( prevProps : ViewEventListProps ) {
895
919
if ( this . listBodyRef . current ?. parentElement ?. contains ( document . activeElement ) ) {
896
920
// If we previously had something here focused, and we've updated, update
897
921
// the focus too, to make sure it's in the right place.
@@ -902,6 +926,25 @@ export class ViewEventList extends React.Component<ViewEventListProps> {
902
926
// scroll there again ourselves now.
903
927
if ( this . wasListAtBottom && ! this . isListAtBottom ( ) ) {
904
928
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
+ }
905
948
}
906
949
}
907
950
0 commit comments