A platform for community discussion. Free, open, simple.
FIX: preserve scroll position when returning to user activity streams (#42435)
David Battersbyb5961484c99b
diff --git a/frontend/discourse/app/routes/user-activity-stream.js b/frontend/discourse/app/routes/user-activity-stream.jsindex 9eccb75c448..aa95843c128 100644--- a/frontend/discourse/app/routes/user-activity-stream.js+++ b/frontend/discourse/app/routes/user-activity-stream.js@@ -1,28 +1,36 @@+import { service } from "@ember/service";import DiscourseRoute from "discourse/routes/discourse";import { i18n } from "discourse-i18n";+const STREAM_KEY = Symbol("user-activity-stream");+export default class UserActivityStream extends DiscourseRoute {+ @service historyStore;+templateName = "user/stream";queryParams = {acting_username: { refreshModel: true },};- model() {- const user = this.modelFor("user");- const stream = user.get("stream");+ async model(params) {+ // Reuse the stream we left behind, including the items infinite scroll+ // appended. Reloading it would leave the page shorter than it was, and the+ // scroll position restored by route-scroll-manager would be clamped.+ let stream = this.historyStore.isPoppedState+ ? this.historyStore.get(STREAM_KEY)+ : null;- return {- stream,- emptyState: this.emptyState(),- };- }+ if (!stream) {+ stream = this.modelFor("user").stream;+ await stream.filterBy({+ filter: this.userActionType,+ actingUsername: params.acting_username,+ });+ this.historyStore.set(STREAM_KEY, stream);+ }- afterModel(model, transition) {- return model.stream.filterBy({- filter: this.userActionType,- actingUsername: transition.to.queryParams.acting_username,- });+ return { stream, emptyState: this.emptyState() };}setupController() {diff --git a/spec/system/page_objects/pages/user_activity_stream.rb b/spec/system/page_objects/pages/user_activity_stream.rbnew file mode 100644index 00000000000..b479eade838--- /dev/null+++ b/spec/system/page_objects/pages/user_activity_stream.rb@@ -0,0 +1,37 @@+# frozen_string_literal: true++module PageObjects+ module Pages+ class UserActivityStream < PageObjects::Pages::Base+ def visit_replies(user)+ page.visit("/u/#{user.username_lower}/activity/replies")+ self+ end++ def has_items?(count:)+ page.has_css?(".user-stream-item", count:)+ end++ def scroll_to_bottom+ page.scroll_to(:bottom)+ self+ end++ def scroll_to_item(index)+ page.execute_script(+ "document.querySelectorAll('.user-stream-item')[#{index}].scrollIntoView(true)",+ )+ self+ end++ def open_item(index)+ page.all(".user-stream-item .title a")[index].click+ self+ end++ def scroll_position+ page.evaluate_script("window.scrollY")+ end+ end+ end+enddiff --git a/spec/system/user_activity_scroll_restoration_spec.rb b/spec/system/user_activity_scroll_restoration_spec.rbnew file mode 100644index 00000000000..808b1420998--- /dev/null+++ b/spec/system/user_activity_scroll_restoration_spec.rb@@ -0,0 +1,41 @@+# frozen_string_literal: true++describe "User activity scroll restoration" do+ before_all { UserActionManager.enable }++ fab!(:user)++ # 40 replies: the stream serves 30 per page, so the rest arrive via infinite scroll+ fab!(:topics) do+ Fabricate+ .times(5, :topic)+ .each do |topic|+ Fabricate+ .times(9, :post, topic:, user:)+ .each { |post| UserActionManager.post_created(post) }+ end+ end++ let(:activity_stream) { PageObjects::Pages::UserActivityStream.new }++ it "takes the user back to where they were reading after they open a post and go back" do+ activity_stream.visit_replies(user)+ expect(activity_stream).to have_items(count: 30)++ activity_stream.scroll_to_bottom+ expect(activity_stream).to have_items(count: 40)++ activity_stream.scroll_to_item(35)+ position = activity_stream.scroll_position++ activity_stream.open_item(36)+ expect(page).to have_css("#topic-title")++ page.go_back+ expect(activity_stream).to have_items(count: 40)++ try_until_success(reason: "scroll position is restored") do+ expect(activity_stream.scroll_position).to be_within(20).of(position)+ end+ end+end