Skip to content

Commit 5f78f28

Browse files
committed
Don't automatically scroll to restored view list selections
Also preserves scrolling to zero & the end, instead of resetting you unexpectedly, and preserves selected events even if selected by links from elsewhere.
1 parent c300f10 commit 5f78f28

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

src/components/view/view-event-list.tsx

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -772,11 +772,11 @@ export class ViewEventList extends React.Component<ViewEventListProps> {
772772
private setListBodyRef = (element: HTMLDivElement | null) => {
773773
// Update the ref
774774
(this.listBodyRef as any).current = element;
775-
775+
776776
// If the element is being mounted and we haven't restored state yet, do it now
777-
if (element && !this.hasRestoredInitialState) {
777+
if (element && !this.hasRestoredScrollState) {
778778
this.restoreScrollPosition();
779-
this.hasRestoredInitialState = true;
779+
this.hasRestoredScrollState = true;
780780
}
781781
};
782782

@@ -900,16 +900,20 @@ export class ViewEventList extends React.Component<ViewEventListProps> {
900900
this.wasListAtBottom = this.isListAtBottom();
901901

902902
// Only save scroll position after we've restored the initial state
903-
if (this.hasRestoredInitialState) {
903+
if (this.hasRestoredScrollState) {
904904
const listWindow = this.listBodyRef.current?.parentElement;
905+
906+
const scrollPosition = this.wasListAtBottom
907+
? 'end'
908+
: (listWindow?.scrollTop ?? 'end');
905909
if (listWindow) {
906-
this.props.uiStore.setViewScrollPosition(listWindow.scrollTop);
910+
this.props.uiStore.setViewScrollPosition(scrollPosition);
907911
}
908912
}
909913
});
910914
}
911915

912-
private hasRestoredInitialState = false;
916+
private hasRestoredScrollState = false;
913917

914918
componentDidUpdate(prevProps: ViewEventListProps) {
915919
if (this.listBodyRef.current?.parentElement?.contains(document.activeElement)) {
@@ -930,11 +934,12 @@ export class ViewEventList extends React.Component<ViewEventListProps> {
930934
}
931935

932936
private restoreScrollPosition = () => {
933-
// Only restore if we have a saved position
934937
const savedPosition = this.props.uiStore.viewScrollPosition;
935-
if (savedPosition > 0) {
936-
const listWindow = this.listBodyRef.current?.parentElement;
937-
if (listWindow) {
938+
const listWindow = this.listBodyRef.current?.parentElement;
939+
if (listWindow) {
940+
if (savedPosition === 'end') {
941+
listWindow.scrollTop = listWindow.scrollHeight;
942+
} else {
938943
// Only restore if we're not close to the current position (avoid unnecessary scrolling)
939944
if (Math.abs(listWindow.scrollTop - savedPosition) > 10) {
940945
listWindow.scrollTop = savedPosition;
@@ -1044,13 +1049,4 @@ export class ViewEventList extends React.Component<ViewEventListProps> {
10441049

10451050
event.preventDefault();
10461051
}
1047-
1048-
// Public method to force scroll and selection restoration
1049-
public restoreViewState = () => {
1050-
if (this.props.selectedEvent) {
1051-
this.scrollToEvent(this.props.selectedEvent);
1052-
} else {
1053-
this.restoreScrollPosition();
1054-
}
1055-
}
10561052
}

src/components/view/view-page.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,11 @@ class ViewPage extends React.Component<ViewPageProps> {
244244
);
245245

246246
componentDidMount() {
247-
// After first render, scroll to the selected event (or the end of the list) by default:
247+
// After first render, if we're jumping to an event, then scroll to it:
248248
requestAnimationFrame(() => {
249-
if ((this.props.eventId || this.props.uiStore.selectedEventId) && this.selectedEvent) {
249+
if (this.props.eventId && this.selectedEvent) {
250+
this.props.uiStore.setSelectedEventId(this.props.eventId);
250251
this.onScrollToCenterEvent(this.selectedEvent);
251-
} else if (!this.props.uiStore.viewScrollPosition) {
252-
this.onScrollToEnd();
253252
}
254253
});
255254

@@ -499,9 +498,9 @@ class ViewPage extends React.Component<ViewPageProps> {
499498
onSearchFiltersConsidered(filters: FilterSet | undefined) {
500499
this.searchFiltersUnderConsideration = filters;
501500
}
501+
502502
@action.bound
503503
onSelected(event: CollectedEvent | undefined) {
504-
// Persist the selected event to UiStore for tab switching
505504
this.props.uiStore.setSelectedEventId(event?.id);
506505

507506
this.props.navigate(event

src/model/ui/ui-store.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,8 @@ export class UiStore {
251251
@observable
252252
expandedViewCard: ExpandableViewCardKey | undefined;
253253

254-
// Store view list scroll position and selected entry to persist when switching tabs
255254
@observable
256-
viewScrollPosition: number = 0;
255+
viewScrollPosition: number | 'end' = 'end';
257256

258257
@observable
259258
selectedEventId: string | undefined;
@@ -445,7 +444,7 @@ export class UiStore {
445444

446445
// Actions for persisting view state when switching tabs
447446
@action.bound
448-
setViewScrollPosition(position: number) {
447+
setViewScrollPosition(position: number | 'end') {
449448
this.viewScrollPosition = position;
450449
}
451450

0 commit comments

Comments
 (0)