OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Switch from infinite scrolling to managed lazy loaded pages
Kabiru Mwenja2484b4852526
diff --git a/app/components/concerns/work_packages/activities_tab/stimulus_controllers.rb b/app/components/concerns/work_packages/activities_tab/stimulus_controllers.rbindex 0f994cf6b7b..71c08776643 100644--- a/app/components/concerns/work_packages/activities_tab/stimulus_controllers.rb+++ b/app/components/concerns/work_packages/activities_tab/stimulus_controllers.rb@@ -37,7 +37,7 @@ module WorkPackagesdef editor_stimulus_controller(suffix = nil) = "#{stimulus_controller_namespace}--editor#{suffix}"def index_stimulus_controller(suffix = nil) = "#{stimulus_controller_namespace}--index#{suffix}"def internal_comment_stimulus_controller(suffix = nil) = "#{stimulus_controller_namespace}--internal-comment#{suffix}"- def infinite_scroll_stimulus_controller(suffix = nil) = "#{stimulus_controller_namespace}--infinite-scroll#{suffix}"+ def lazy_page_stimulus_controller(suffix = nil) = "#{stimulus_controller_namespace}--lazy-page#{suffix}"def polling_stimulus_controller(suffix = nil) = "#{stimulus_controller_namespace}--polling#{suffix}"def stems_stimulus_controller(suffix = nil) = "#{stimulus_controller_namespace}--stems#{suffix}"def quote_comments_stimulus_controller(suffix = nil) = "#{stimulus_controller_namespace}--quote-comment#{suffix}"diff --git a/app/components/work_packages/activities_tab/index_component.rb b/app/components/work_packages/activities_tab/index_component.rbindex 8020ad4d4cf..e926d749b74 100644--- a/app/components/work_packages/activities_tab/index_component.rb+++ b/app/components/work_packages/activities_tab/index_component.rb@@ -58,7 +58,7 @@ module WorkPackagesdef list_journals_componentWorkPackages::ActivitiesTab::Journals::IndexComponent- .new(work_package:, journals:, filter:, page: paginator.page, next_page: paginator.next)+ .new(work_package:, journals:, filter:, paginator:)enddef add_journal_componentdiff --git a/app/components/work_packages/activities_tab/journals/index_component.html.erb b/app/components/work_packages/activities_tab/journals/index_component.html.erbindex 7f2c2f97397..763773a9fd8 100644--- a/app/components/work_packages/activities_tab/journals/index_component.html.erb+++ b/app/components/work_packages/activities_tab/journals/index_component.html.erb@@ -13,20 +13,8 @@render(WorkPackages::ActivitiesTab::Journals::EmptyComponent.new)endelse- if journal_sorting.asc?- journals_index_container.with_row do- render(infinite_scroll_component)- end- end-journals_index_container.with_row(id: insert_target_modifier_id) do- render(page_component)- end-- if journal_sorting.desc?- journals_index_container.with_row do- render(infinite_scroll_component)- end+ pages.map { concat render(it) }endendenddiff --git a/app/components/work_packages/activities_tab/journals/index_component.rb b/app/components/work_packages/activities_tab/journals/index_component.rbindex 43c04764619..2bce3326e50 100644--- a/app/components/work_packages/activities_tab/journals/index_component.rb+++ b/app/components/work_packages/activities_tab/journals/index_component.rb@@ -37,30 +37,43 @@ module WorkPackagesinclude OpTurbo::Streamableinclude WorkPackages::ActivitiesTab::SharedHelpers- def initialize(work_package:, journals:, page: 1, next_page: nil, filter: :all)+ def initialize(work_package:, journals:, paginator:, filter: :all)super@work_package = work_package@journals = journals- @page = page- @next_page = next_page+ @paginator = paginator@filter = filterend- def infinite_scroll_component- WorkPackages::ActivitiesTab::Journals::InfiniteScrollComponent.new(work_package:, page:, next_page:)+ def pages+ current_page = paginator.page++ result = (1..paginator.pages).map do |page|+ if page == current_page+ page_component(page)+ else+ lazy_page_component(page)+ end+ end++ result.tap { it.reverse! if journal_sorting.asc? && paginator.pages > 1 }end- def page_component+ def page_component(page)WorkPackages::ActivitiesTab::Journals::PageComponent.new(journals:, emoji_reactions:, page:, filter:)end+ def lazy_page_component(page)+ WorkPackages::ActivitiesTab::Journals::LazyPageComponent.new(work_package:, page:)+ end+def self.insert_target_modifier_id = "#{wrapper_key}-pages"delegate :insert_target_modifier_id, to: :classprivate- attr_reader :work_package, :journals, :page, :next_page, :filter+ attr_reader :work_package, :journals, :paginator, :filterdef insert_target_modified?truediff --git a/app/components/work_packages/activities_tab/journals/infinite_scroll_component.html.erb b/app/components/work_packages/activities_tab/journals/infinite_scroll_component.html.erbdeleted file mode 100644index b28b82e7b21..00000000000--- a/app/components/work_packages/activities_tab/journals/infinite_scroll_component.html.erb+++ /dev/null@@ -1,17 +0,0 @@-<%=- component_wrapper(data: wrapper_data_attributes) do- flex_layout(- data: { "#{infinite_scroll_stimulus_controller}-target": "skeleton" },- class: "work-packages-activities-tab-journals-item-component",- bg: :default- ) do |skeleton|- skeleton.with_row(mb: 2) do- render(Primer::Alpha::SkeletonBox.new(width: "25%", height: "40px"))- end-- skeleton.with_row(mb: 3) do- render(Primer::Alpha::SkeletonBox.new(width: "100%", height: "150px"))- end- end- end-%>diff --git a/app/components/work_packages/activities_tab/journals/lazy_page_component.html.erb b/app/components/work_packages/activities_tab/journals/lazy_page_component.html.erbnew file mode 100644index 00000000000..f02e84f185a--- /dev/null+++ b/app/components/work_packages/activities_tab/journals/lazy_page_component.html.erb@@ -0,0 +1,47 @@+<%# -- copyright+OpenProject is an open source project management software.+Copyright (C) the OpenProject GmbH++This program is free software; you can redistribute it and/or+modify it under the terms of the GNU General Public License version 3.++OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+Copyright (C) 2006-2013 Jean-Philippe Lang+Copyright (C) 2010-2013 the ChiliProject Team++This program is free software; you can redistribute it and/or+modify it under the terms of the GNU General Public License+as published by the Free Software Foundation; either version 2+of the License, or (at your option) any later version.++This program is distributed in the hope that it will be useful,+but WITHOUT ANY WARRANTY; without even the implied warranty of+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+GNU General Public License for more details.++You should have received a copy of the GNU General Public License+along with this program; if not, write to the Free Software+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.++See COPYRIGHT and LICENSE files for more details.++++#%>++<%=+ component_wrapper(data: wrapper_data_attributes) do+ flex_layout(+ class: "work-packages-activities-tab-journals-item-component",+ bg: :default+ ) do |skeleton|+ 5.times do+ skeleton.with_row(mb: 2) do+ render(Primer::Alpha::SkeletonBox.new(width: "25%", height: "40px"))+ end++ skeleton.with_row(mb: 3) do+ render(Primer::Alpha::SkeletonBox.new(width: "100%", height: "150px"))+ end+ end+ end+ end+%>diff --git a/app/components/work_packages/activities_tab/journals/infinite_scroll_component.rb b/app/components/work_packages/activities_tab/journals/lazy_page_component.rbsimilarity index 71%rename from app/components/work_packages/activities_tab/journals/infinite_scroll_component.rbrename to app/components/work_packages/activities_tab/journals/lazy_page_component.rbindex b09fda12557..de80f189dc1 100644--- a/app/components/work_packages/activities_tab/journals/infinite_scroll_component.rb+++ b/app/components/work_packages/activities_tab/journals/lazy_page_component.rb@@ -31,36 +31,39 @@module WorkPackagesmodule ActivitiesTabmodule Journals- class InfiniteScrollComponent < ApplicationComponent+ class LazyPageComponent < ApplicationComponentinclude OpPrimer::ComponentHelpersinclude OpTurbo::Streamableinclude WorkPackages::ActivitiesTab::StimulusControllers- def initialize(work_package:, page: 1, next_page: nil)+ def initialize(work_package:, page:)super@work_package = work_package@page = page- @next_page = next_page+ end++ def self.wrapper_key+ WorkPackages::ActivitiesTab::Journals::PageComponent.wrapper_key+ end++ def wrapper_uniq_by+ pageendprivate- attr_reader :work_package, :page, :next_page+ attr_reader :work_package, :pagedef wrapper_data_attributes{- controller: infinite_scroll_stimulus_controller,- infinite_scroll_stimulus_controller("-insert-target-id-value") => insert_target_id,- infinite_scroll_stimulus_controller("-page-value") => page,- infinite_scroll_stimulus_controller("-is-last-page-value") => next_page.blank?,- infinite_scroll_stimulus_controller("-url-value") => page_streams_url+ controller: lazy_page_stimulus_controller,+ lazy_page_stimulus_controller("-insert-target-id-value") => wrapper_key,+ lazy_page_stimulus_controller("-page-value") => page,+ lazy_page_stimulus_controller("-url-value") => page_streams_url,+ lazy_page_stimulus_controller("-is-loaded-value") => false}end- def insert_target_id- WorkPackages::ActivitiesTab::Journals::IndexComponent.insert_target_modifier_id- end-def page_streams_urlpage_streams_work_package_activities_path(work_package, format: :turbo_stream)enddiff --git a/app/controllers/concerns/op_turbo/component_stream.rb b/app/controllers/concerns/op_turbo/component_stream.rbindex 2332542f07d..54d26aa95a0 100644--- a/app/controllers/concerns/op_turbo/component_stream.rb+++ b/app/controllers/concerns/op_turbo/component_stream.rb@@ -113,14 +113,6 @@ module OpTurboturbo_streams << instance.render_as_turbo_stream(view_context:, action: :flash)end- def set_dataset_attributes_via_turbo_stream(target, **attributes)- attributes.each do |attribute, value|- turbo_streams << OpTurbo::StreamComponent- .new(action: :set_dataset_attribute, target:, attribute:, value:)- .render_in(view_context)- end- end-def scroll_into_view_via_turbo_stream(target, behavior: :auto, block: :start)turbo_streams << OpTurbo::StreamComponent.new(action: :scroll_into_view, target:, behavior:, block:)diff --git a/app/controllers/work_packages/activities_tab_controller.rb b/app/controllers/work_packages/activities_tab_controller.rbindex d9d6b908b1c..b037318a942 100644--- a/app/controllers/work_packages/activities_tab_controller.rb+++ b/app/controllers/work_packages/activities_tab_controller.rb@@ -56,24 +56,13 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerenddef page_streams- insert_via_turbo_stream(- target_component: WorkPackages::ActivitiesTab::Journals::IndexComponent.new(- work_package: @work_package,- journals: Journal.none # we do not need to pass any journals here since we just want the component key- ),+ replace_via_turbo_stream(component: WorkPackages::ActivitiesTab::Journals::PageComponent.new(journals: @paginated_journals,emoji_reactions: wp_journals_emoji_reactions,page: @paginator.page,filter: @filter- ),- action: journal_sorting.desc? ? :append : :prepend- )-- set_dataset_attributes_via_turbo_stream(- WorkPackages::ActivitiesTab::Journals::InfiniteScrollComponent.wrapper_key,- infinite_scroll_stimulus_controller("-page-value") => @paginator.page,- infinite_scroll_stimulus_controller("-is-last-page-value") => @paginator.next.blank?+ ))respond_with_turbo_streams@@ -367,6 +356,7 @@ class WorkPackages::ActivitiesTabController < ApplicationControllercomponent: WorkPackages::ActivitiesTab::Journals::IndexComponent.new(work_package: @work_package,journals: @paginated_journals,+ paginator: @paginator,filter: @filter))@@ -476,6 +466,7 @@ class WorkPackages::ActivitiesTabController < ApplicationControllertarget_component = WorkPackages::ActivitiesTab::Journals::IndexComponent.new(work_package: @work_package,journals: Journal.none, # we do not need to pass any journals here since we just want the component key+ paginator: nil,filter: @filter)diff --git a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/infinite-scroll.controller.ts b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/lazy-page.controller.tssimilarity index 80%rename from frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/infinite-scroll.controller.tsrename to frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/lazy-page.controller.tsindex fcd4f28641d..e190426c1a8 100644--- a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/infinite-scroll.controller.ts+++ b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/lazy-page.controller.ts@@ -1,7 +1,7 @@/** -- copyright* OpenProject is an open source project management software.- * Copyright (C) 2023 the OpenProject GmbH+ * Copyright (C) the OpenProject GmbH** This program is free software; you can redistribute it and/or* modify it under the terms of the GNU General Public License version 3.@@ -34,31 +34,26 @@ import { useIntersection } from 'stimulus-use';import BaseController from './base.controller';import { DomHelpers } from './services/dom_helpers';-export default class extends BaseController {+export default class LazyPageController extends BaseController {static values = {url: String,insertTargetId: String,page: { type: Number, default: 1 },- isLastPage: Boolean,+ isLoaded: { type: Boolean, default: false },};- static targets = ['skeleton'];-declare urlValue:string;declare insertTargetIdValue:string;declare pageValue:number;- declare isLastPageValue:boolean;-- declare readonly skeletonTarget:HTMLElement;- declare readonly hasSkeletonTarget:boolean;+ declare isLoadedValue:boolean;- private updateInProgress = false;private turboRequests:TurboRequestsService;private abortController = new AbortController();private pageStreamHandler?:(_event:TurboBeforeStreamRenderEvent) => void;+ private stopObserving?:() => void;connect() {- if (this.isLastPageValue) return;+ if (this.isLoadedValue) return;super.connect();void this.initializeTurboRequestService();@@ -68,35 +63,28 @@ export default class extends BaseController {disconnect() {super.disconnect();this.tearDownScrollPreservation();+ this.stopObserving?.();}- async appear() {- if (this.updateInProgress || this.isLastPageValue) return;+ async appear(entry:IntersectionObserverEntry, observer:IntersectionObserver) {+ observer.unobserve(entry.target); // observe and emit `appear()` callback just once+ if (this.isLoadedValue) return;- this.updateInProgress = true;-- await this.fetchNextPageStream()+ await this.fetchPageStream().catch((error) => {console.error('Error fetching next page:', error);}).finally(() => {- this.updateInProgress = false;+ this.isLoadedValue = true;});}- isLastPageValueChanged(isLastPage:boolean, _previousValue:boolean) {- if (isLastPage) {- (this.element as HTMLElement).hidden = true;- if (this.hasSkeletonTarget) this.skeletonTarget.remove();- }- }-private setupScrollPreservation() {if (!this.scrollableContainer || this.pageStreamHandler) return;const { signal } = this.abortController;const scrollContainer = this.scrollableContainer;- useIntersection(this, { root: scrollContainer });+ this.setupIntersectionObserver({ root: scrollContainer });this.pageStreamHandler = (event:TurboBeforeStreamRenderEvent) => {event.preventDefault();@@ -118,12 +106,17 @@ export default class extends BaseController {document.addEventListener('turbo:before-stream-render', this.pageStreamHandler as EventListener, { signal });}+ private setupIntersectionObserver({ root }:{ root:HTMLElement }) {+ const [_observe, unobserve] = useIntersection(this, { root, threshold: 0.25, dispatchEvent: false });+ this.stopObserving = unobserve;+ }+private tearDownScrollPreservation() {this.abortController.abort();if (this.pageStreamHandler) this.pageStreamHandler = undefined;}- private fetchNextPageStream():Promise<{ html:string, headers:Headers }> {+ private fetchPageStream():Promise<{ html:string, headers:Headers }> {const url = this.preparePageStreamsUrl();return this.turboRequests.requestStream(url);}@@ -131,7 +124,6 @@ export default class extends BaseController {private preparePageStreamsUrl():string {const baseUrl = window.location.origin;const url = new URL(this.urlValue, baseUrl);- this.pageValue += 1;url.searchParams.set('page', this.pageValue.toString());url.searchParams.set('filter', this.indexOutlet.filterValue);diff --git a/frontend/src/stimulus/setup.ts b/frontend/src/stimulus/setup.tsindex 5bfb6995e2d..f2d125a389f 100644--- a/frontend/src/stimulus/setup.ts+++ b/frontend/src/stimulus/setup.ts@@ -24,7 +24,7 @@ import AutoScrollingController from './controllers/dynamic/work-packages/activitimport PollingController from './controllers/dynamic/work-packages/activities-tab/polling.controller';import StemsController from './controllers/dynamic/work-packages/activities-tab/stems.controller';import EditorController from './controllers/dynamic/work-packages/activities-tab/editor.controller';-import InfiniteScrollController from './controllers/dynamic/work-packages/activities-tab/infinite-scroll.controller';+import LazyPageController from './controllers/dynamic/work-packages/activities-tab/lazy-page.controller';import AutoSubmit from '@stimulus-components/auto-submit';import AutoThemeSwitcher from './controllers/auto-theme-switcher.controller';@@ -65,7 +65,7 @@ OpenProjectStimulusApplication.preregister('work-packages--activities-tab--auto-OpenProjectStimulusApplication.preregister('work-packages--activities-tab--polling', PollingController);OpenProjectStimulusApplication.preregister('work-packages--activities-tab--stems', StemsController);OpenProjectStimulusApplication.preregister('work-packages--activities-tab--editor', EditorController);-OpenProjectStimulusApplication.preregister('work-packages--activities-tab--infinite-scroll', InfiniteScrollController);+OpenProjectStimulusApplication.preregister('work-packages--activities-tab--lazy-page', LazyPageController);OpenProjectStimulusApplication.preregister('beforeunload', BeforeunloadController);OpenProjectStimulusApplication.preregister('auto-theme-switcher', AutoThemeSwitcher);OpenProjectStimulusApplication.preregister('external-links', ExternalLinksController);