Skip to content

A platform for community discussion. Free, open, simple.

FIX: eager "load more" in users directory (#35439)

Régis Hanoledf7d27e3481
diff --git a/app/assets/javascripts/discourse/app/components/load-more.gjs b/app/assets/javascripts/discourse/app/components/load-more.gjs
index bcfd7f59225..41db4e6abc9 100644
--- a/app/assets/javascripts/discourse/app/components/load-more.gjs
+++ b/app/assets/javascripts/discourse/app/components/load-more.gjs
@@ -23,11 +23,16 @@ export function enableLoadMoreObserver() {
* in additional options to customize the observer's behavior;
* Refer to https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver#options for a full list.
*
- * Additionally, an @enabled boolean can be passed to allow for cases where
- * the element is visible in the viewport but you don't want to allow the `loadMore`
- * behaviour. A use case for this is when our controllers return some `canLoadMore`
- * boolean. There is no use attempting to load more from the server in this case,
- * there will be nothing else.
+ * @param {Function} action - The action to trigger when more content should be loaded
+ * @param {boolean} [enabled=true] - Whether to allow the loadMore action to trigger.
+ * Use this when you know there's no more content available (e.g., `model.canLoadMore`).
+ * When false, the observer continues to run but the action won't be triggered.
+ * @param {boolean} [isLoading=false] - Whether content is currently loading.
+ * When true, the IntersectionObserver won't be created, preventing premature triggers
+ * during initial content load. Pass this to avoid race conditions during page initialization.
+ * @param {string} [rootMargin="0px 0px 0px 0px"] - Margin around the root element for intersection detection
+ * @param {number} [threshold=0.0] - Threshold at which the intersection callback is triggered
+ * @param {string} [root=null] - CSS selector for the root element to observe intersection within
*
* @example Basic usage with a block:
* ```gjs
@@ -47,13 +52,24 @@ export function enableLoadMoreObserver() {
* <LoadMore @action={{this.loadMore}} />
* ```
*
- * @example With custom options:
+ * @example With enabled and isLoading to prevent premature loading:
+ * ```gjs
+ * <LoadMore
+ * @action={{this.loadMoreUsers}}
+ * @enabled={{this.model.canLoadMore}}
+ * @isLoading={{this.isLoading}}
+ * >
+ * <UserList @users={{this.model}} />
+ * </LoadMore>
+ * ```
+ *
+ * @example With custom IntersectionObserver options:
* ```gjs
* <LoadMore
* @action={{this.fetchMoreUsers}}
* @rootMargin="100px"
* @threshold={{0.2}}
- * @root={{this.root}}
+ * @root={{this.scrollContainer}}
* class="users-container"
* />
* ```
@@ -85,6 +101,7 @@ export default class LoadMore extends Component {
threshold=this.threshold
rootMargin=this.rootMargin
root=this.root
+ isLoading=@isLoading
}}
class="load-more-sentinel"
aria-hidden="true"
diff --git a/app/assets/javascripts/discourse/app/modifiers/observe-intersection.js b/app/assets/javascripts/discourse/app/modifiers/observe-intersection.js
index 9d0f277b673..6dffa31230c 100644
--- a/app/assets/javascripts/discourse/app/modifiers/observe-intersection.js
+++ b/app/assets/javascripts/discourse/app/modifiers/observe-intersection.js
@@ -4,8 +4,17 @@ export default modifier(
(
element,
[callback],
- { threshold = 1, rootMargin = "0px 0px 0px 0px", root = null }
+ {
+ threshold = 1,
+ rootMargin = "0px 0px 0px 0px",
+ root = null,
+ isLoading = false,
+ }
) => {
+ if (isLoading) {
+ return () => {};
+ }
+
if (Array.isArray(rootMargin)) {
rootMargin = rootMargin
.map((margin) => (typeof margin === "number" ? `${margin}px` : margin))
diff --git a/app/assets/javascripts/discourse/app/templates/users.gjs b/app/assets/javascripts/discourse/app/templates/users.gjs
index 11f7d932ac3..0f2a2ef7aa4 100644
--- a/app/assets/javascripts/discourse/app/templates/users.gjs
+++ b/app/assets/javascripts/discourse/app/templates/users.gjs
@@ -29,6 +29,7 @@ export default RouteTemplate(
<LoadMore
@action={{@controller.loadMore}}
@enabled={{@controller.model.canLoadMore}}
+ @isLoading={{@controller.isLoading}}
>
<div class="container">
<div class="users-directory directory">
diff --git a/app/assets/javascripts/discourse/tests/integration/components/load-more-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/load-more-test.gjs
index fcf5cd81c2e..4f23abc5bff 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/load-more-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/load-more-test.gjs
@@ -99,4 +99,34 @@ module("Integration | Component | load-more", function (hooks) {
"loadMore action should be called never"
);
});
+
+ test("does not call loadMore action when isLoading is true", async function (assert) {
+ let actionCalled = 0;
+ const loadMore = () => actionCalled++;
+
+ await render(
+ <template>
+ <LoadMore
+ @action={{loadMore}}
+ @root="#ember-testing"
+ @isLoading={{true}}
+ >
+ <table class="numbers">
+ <tbody>
+ <tr />
+ </tbody>
+ </table>
+ </LoadMore>
+ </template>
+ );
+
+ // Wait to ensure the observer callback would have fired if it was created
+ await new Promise((resolve) => setTimeout(resolve, 50));
+
+ assert.strictEqual(
+ actionCalled,
+ 0,
+ "loadMore action should not be called when isLoading is true"
+ );
+ });
});