mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-23 16:56:50 +08:00
This PR makes the following key changes to the load-more component: * Updating to a Glimmer component * Changing from an eyeline/scrolling-based mechanism to an IntersectionObserver to determine when to load * Keeping track of a single invisible sentinel element to help trigger the loadMore action instead of having to find and track the last item of the collection upon every load * The component can now be used without wrapping around some content to be yielded - the intent is to use this for cases like [DiscoveryTopicsList](f0057c7353/app/assets/javascripts/discourse/app/components/discovery/topics.gjs (L222)) where we might want more precise placement of the sentinel element. * Added utility toggle functions to control observer behaviour in this class for testing We will replace the load-more mixin in DiscoveryTopicsList in another PR. https://github.com/user-attachments/assets/50d9763f-b5f8-40f6-8630-41bdf107baf7 ### Technical Considerations 1. Keeping track of a single sentinel element simplifies the logic greatly and is also more robust to changes in the collection that's being loaded. (ref: a [previous commit](https://github.com/discourse/discourse/pull/32285/commits/2279519081eef9649864453c90d72dbb2bd8970c) that was following the previous approach of tracking specifically the last item of the loaded collection); this also sidesteps odd edge cases like if the tracked element is larger than the entire viewport. 2. Using [isIntersecting](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry/isIntersecting) instead of calculating manually whether the element is in the viewport is also less flaky - I ran into issues with the boundingClientRect inconsistently being calculated as outside the viewport on different sized screens. 3. We need to properly bind `loadMore` functions with the action decorator, otherwise the way we pass the loadMore callbacks through to the observe-intersection modifier results in attempting to call it on the loadMore component context instead. I've done this for all such functions except for the one in [`category-list`](0ed4b09527/app/assets/javascripts/discourse/app/models/category-list.js (L117)) which uses `@bind` that should be equivalent in terms of binding to the correct `this`.
244 lines
6 KiB
JavaScript
Vendored
244 lines
6 KiB
JavaScript
Vendored
import { tracked } from "@glimmer/tracking";
|
|
import Controller from "@ember/controller";
|
|
import { action, computed } from "@ember/object";
|
|
import { service } from "@ember/service";
|
|
import { TrackedArray } from "@ember-compat/tracked-built-ins";
|
|
import CanCheckEmailsHelper from "discourse/lib/can-check-emails-helper";
|
|
import { computedI18n, setting } from "discourse/lib/computed";
|
|
import discourseDebounce from "discourse/lib/debounce";
|
|
import discourseComputed, { bind } from "discourse/lib/decorators";
|
|
import { INPUT_DELAY } from "discourse/lib/environment";
|
|
import { i18n } from "discourse-i18n";
|
|
import BulkUserDeleteConfirmation from "admin/components/bulk-user-delete-confirmation";
|
|
import AdminUser from "admin/models/admin-user";
|
|
|
|
const MAX_BULK_SELECT_LIMIT = 100;
|
|
|
|
export default class AdminUsersListShowController extends Controller {
|
|
@service dialog;
|
|
@service modal;
|
|
@service toasts;
|
|
|
|
@tracked bulkSelect = false;
|
|
@tracked displayBulkActions = false;
|
|
@tracked bulkSelectedUserIdsSet = new Set();
|
|
@tracked bulkSelectedUsersMap = {};
|
|
@setting("moderators_view_emails") canModeratorsViewEmails;
|
|
|
|
query = null;
|
|
order = null;
|
|
asc = null;
|
|
showEmails = false;
|
|
refreshing = false;
|
|
listFilter = null;
|
|
lastSelected = null;
|
|
|
|
@computedI18n("search_hint") searchHint;
|
|
|
|
_page = 1;
|
|
_results = new TrackedArray();
|
|
_canLoadMore = true;
|
|
|
|
get users() {
|
|
return this._results.flat();
|
|
}
|
|
|
|
@discourseComputed("query")
|
|
title(query) {
|
|
return i18n("admin.users.titles." + query);
|
|
}
|
|
|
|
@discourseComputed("showEmails")
|
|
columnCount(showEmails) {
|
|
let colCount = 7; // note that the first column is hardcoded in the template
|
|
|
|
if (showEmails) {
|
|
colCount += 1;
|
|
}
|
|
|
|
if (this.siteSettings.must_approve_users) {
|
|
colCount += 1;
|
|
}
|
|
|
|
return colCount;
|
|
}
|
|
|
|
@computed("model.id", "currentUser.id")
|
|
get canCheckEmails() {
|
|
return new CanCheckEmailsHelper(
|
|
this.model?.id,
|
|
this.canModeratorsViewEmails,
|
|
this.currentUser
|
|
).canCheckEmails;
|
|
}
|
|
|
|
@computed("model.id", "currentUser.id")
|
|
get canAdminCheckEmails() {
|
|
return new CanCheckEmailsHelper(
|
|
this.model?.id,
|
|
this.canModeratorsViewEmails,
|
|
this.currentUser
|
|
).canAdminCheckEmails;
|
|
}
|
|
|
|
@computed("query")
|
|
get showSilenceReason() {
|
|
return this.query === "silenced";
|
|
}
|
|
|
|
resetFilters() {
|
|
this._page = 1;
|
|
this._results.length = 0;
|
|
this._canLoadMore = true;
|
|
return this._refreshUsers();
|
|
}
|
|
|
|
stripHtml(html) {
|
|
const doc = new DOMParser().parseFromString(html, "text/html");
|
|
return doc.body.textContent || "";
|
|
}
|
|
|
|
_refreshUsers() {
|
|
if (!this._canLoadMore) {
|
|
return;
|
|
}
|
|
|
|
const page = this._page;
|
|
this.set("refreshing", true);
|
|
|
|
return AdminUser.findAll(this.query, {
|
|
filter: this.listFilter,
|
|
show_emails: this.showEmails,
|
|
order: this.order,
|
|
asc: this.asc,
|
|
page,
|
|
})
|
|
.then((result) => {
|
|
this._results[page] = result;
|
|
|
|
if (result.length === 0) {
|
|
this._canLoadMore = false;
|
|
}
|
|
})
|
|
.finally(() => {
|
|
this.set("refreshing", false);
|
|
});
|
|
}
|
|
|
|
@action
|
|
onListFilterChange(event) {
|
|
this.set("listFilter", event.target.value);
|
|
discourseDebounce(this, this.resetFilters, INPUT_DELAY);
|
|
}
|
|
|
|
@action
|
|
loadMore() {
|
|
if (this.refreshing) {
|
|
return;
|
|
}
|
|
this._page += 1;
|
|
this._refreshUsers();
|
|
}
|
|
|
|
@action
|
|
toggleEmailVisibility() {
|
|
this.toggleProperty("showEmails");
|
|
this.resetFilters();
|
|
}
|
|
|
|
@action
|
|
updateOrder(field, asc) {
|
|
this.setProperties({
|
|
order: field,
|
|
asc,
|
|
});
|
|
}
|
|
|
|
@action
|
|
toggleBulkSelect() {
|
|
this.bulkSelect = !this.bulkSelect;
|
|
this.displayBulkActions = false;
|
|
this.bulkSelectedUsersMap = {};
|
|
this.bulkSelectedUserIdsSet = new Set();
|
|
}
|
|
|
|
@action
|
|
bulkSelectItemToggle(userId, event) {
|
|
if (event.target.checked) {
|
|
if (!this.#canBulkSelectMoreUsers(1)) {
|
|
this.#showBulkSelectionLimitToast(event);
|
|
return;
|
|
}
|
|
|
|
if (event.shiftKey && this.lastSelected) {
|
|
const list = Array.from(
|
|
document.querySelectorAll(
|
|
"input.directory-table__cell-bulk-select:not([disabled])"
|
|
)
|
|
);
|
|
const lastSelectedIndex = list.indexOf(this.lastSelected);
|
|
if (lastSelectedIndex !== -1) {
|
|
const newSelectedIndex = list.indexOf(event.target);
|
|
const start = Math.min(lastSelectedIndex, newSelectedIndex);
|
|
const end = Math.max(lastSelectedIndex, newSelectedIndex);
|
|
|
|
if (!this.#canBulkSelectMoreUsers(end - start)) {
|
|
this.#showBulkSelectionLimitToast(event);
|
|
return;
|
|
}
|
|
|
|
list.slice(start, end).forEach((input) => {
|
|
input.checked = true;
|
|
this.#addUserToBulkSelection(parseInt(input.dataset.userId, 10));
|
|
});
|
|
}
|
|
}
|
|
this.#addUserToBulkSelection(userId);
|
|
this.lastSelected = event.target;
|
|
} else {
|
|
this.bulkSelectedUserIdsSet.delete(userId);
|
|
delete this.bulkSelectedUsersMap[userId];
|
|
}
|
|
|
|
this.displayBulkActions = this.bulkSelectedUserIdsSet.size > 0;
|
|
}
|
|
|
|
@bind
|
|
async afterBulkDelete() {
|
|
await this.resetFilters();
|
|
this.bulkSelectedUsersMap = {};
|
|
this.bulkSelectedUserIdsSet = new Set();
|
|
this.displayBulkActions = false;
|
|
}
|
|
|
|
@action
|
|
openBulkDeleteConfirmation() {
|
|
this.modal.show(BulkUserDeleteConfirmation, {
|
|
model: {
|
|
userIds: Array.from(this.bulkSelectedUserIdsSet),
|
|
afterBulkDelete: this.afterBulkDelete,
|
|
},
|
|
});
|
|
}
|
|
|
|
#addUserToBulkSelection(userId) {
|
|
this.bulkSelectedUserIdsSet.add(userId);
|
|
this.bulkSelectedUsersMap[userId] = 1;
|
|
}
|
|
|
|
#canBulkSelectMoreUsers(count) {
|
|
return this.bulkSelectedUserIdsSet.size + count <= MAX_BULK_SELECT_LIMIT;
|
|
}
|
|
|
|
#showBulkSelectionLimitToast(event) {
|
|
this.toasts.error({
|
|
duration: 3000,
|
|
data: {
|
|
message: i18n("admin.users.bulk_actions.too_many_selected_users", {
|
|
count: MAX_BULK_SELECT_LIMIT,
|
|
}),
|
|
},
|
|
});
|
|
event.preventDefault();
|
|
}
|
|
}
|