Skip to content

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

DEV: Replace deprecated Ember's array `.addObjects` (#35402)

Sérgio Saquetimf0be6aacca85
diff --git a/frontend/discourse/admin/controllers/admin-api-keys/index.js b/frontend/discourse/admin/controllers/admin-api-keys/index.js
index e7c90f292b3..339a439f069 100644
--- a/frontend/discourse/admin/controllers/admin-api-keys/index.js
+++ b/frontend/discourse/admin/controllers/admin-api-keys/index.js
@@ -1,39 +1,47 @@
+import { tracked } from "@glimmer/tracking";
import Controller from "@ember/controller";
import { action } from "@ember/object";
import { popupAjaxError } from "discourse/lib/ajax-error";
+import { addUniqueValuesToArray } from "discourse/lib/array-tools";
export default class AdminApiKeysIndexController extends Controller {
- loading = false;
+ @tracked loading = false;
@action
- revokeKey(key) {
- key.revoke().catch(popupAjaxError);
+ async revokeKey(key) {
+ try {
+ await key.revoke();
+ } catch (error) {
+ popupAjaxError(error);
+ }
}
@action
- undoRevokeKey(key) {
- key.undoRevoke().catch(popupAjaxError);
+ async undoRevokeKey(key) {
+ try {
+ await key.undoRevoke();
+ } catch (error) {
+ popupAjaxError(error);
+ }
}
@action
- loadMore() {
- if (this.loading || this.model.loaded) {
- return;
- }
-
+ async loadMore() {
const limit = 50;
- this.set("loading", true);
- this.store
- .findAll("api-key", { offset: this.model.length, limit })
- .then((keys) => {
- this.model.addObjects(keys);
- if (keys.length < limit) {
- this.model.set("loaded", true);
- }
- })
- .finally(() => {
- this.set("loading", false);
+ try {
+ this.loading = true;
+ const keys = await this.store.findAll("api-key", {
+ offset: this.model.length,
+ limit,
});
+
+ addUniqueValuesToArray(this.model.content, keys);
+ if (keys.length < limit) {
+ this.model.loaded = true;
+ }
+ } finally {
+ this.loading = false;
+ }
}
}
diff --git a/frontend/discourse/app/components/post-list/index.gjs b/frontend/discourse/app/components/post-list/index.gjs
index 2cff8c365df..b437e30d5d5 100644
--- a/frontend/discourse/app/components/post-list/index.gjs
+++ b/frontend/discourse/app/components/post-list/index.gjs
@@ -3,7 +3,8 @@
*
* @component PostList
*
- * @args {Array<Object>} posts - The array of post objects to display
+ * @args {Array<Object>} posts - The array of post objects to display (it must be a tracked array to ensure that
+ * the component is re-rendered when the array changes)
* @args {Function} fetchMorePosts - A function that fetches more posts. Must return a Promise that resolves to an array of new posts.
* @args {String} emptyText (optional) - Custom text to display when there are no posts
* @args {String|Array} additionalItemClasses (optional) - Additional classes to add to each post list item
diff --git a/frontend/discourse/app/controllers/full-page-search.js b/frontend/discourse/app/controllers/full-page-search.js
index 87741c3f1ec..dd53691f2b2 100644
--- a/frontend/discourse/app/controllers/full-page-search.js
+++ b/frontend/discourse/app/controllers/full-page-search.js
@@ -6,6 +6,7 @@ import { isEmpty } from "@ember/utils";
import { observes } from "@ember-decorators/object";
import { Promise } from "rsvp";
import { ajax } from "discourse/lib/ajax";
+import { addUniqueValuesToArray } from "discourse/lib/array-tools";
import BulkSelectHelper from "discourse/lib/bulk-select-helper";
import { search as searchCategoryTag } from "discourse/lib/category-tag-search";
import discourseComputed, { bind } from "discourse/lib/decorators";
@@ -548,8 +549,9 @@ export default class FullPageSearchController extends Controller {
@action
selectAll() {
- this.bulkSelectHelper.selected.addObjects(
- this.get("searchResultPosts").map((item) => item.topic)
+ addUniqueValuesToArray(
+ this.bulkSelectHelper.selected,
+ this.searchResultPosts.map((item) => item.topic)
);
// Doing this the proper way is a HUGE pain,
@@ -566,7 +568,7 @@ export default class FullPageSearchController extends Controller {
@action
clearAll() {
- this.bulkSelectHelper.selected.clear();
+ this.bulkSelectHelper.selected = [];
document
.querySelectorAll(".fps-result input[type=checkbox]")
@@ -578,7 +580,7 @@ export default class FullPageSearchController extends Controller {
@action
toggleBulkSelect() {
this.toggleProperty("bulkSelectEnabled");
- this.bulkSelectHelper.selected.clear();
+ this.bulkSelectHelper.selected = [];
}
@action
diff --git a/frontend/discourse/app/deprecation-workflow.js b/frontend/discourse/app/deprecation-workflow.js
index 585c6391c36..923ec09eb9e 100644
--- a/frontend/discourse/app/deprecation-workflow.js
+++ b/frontend/discourse/app/deprecation-workflow.js
@@ -261,6 +261,10 @@ const DeprecationWorkflow = new DiscourseDeprecationWorkflow([
handler: "log",
matchId: "discourse.native-array-extensions.[]",
},
+ {
+ handler: "log",
+ matchId: "discourse.native-array-extensions.addObjects",
+ },
{
handler: "log",
matchId: "discourse.native-array-extensions.any",
diff --git a/frontend/discourse/app/lib/bulk-select-helper.js b/frontend/discourse/app/lib/bulk-select-helper.js
index 6adf4313a22..7254ed3e0b4 100644
--- a/frontend/discourse/app/lib/bulk-select-helper.js
+++ b/frontend/discourse/app/lib/bulk-select-helper.js
@@ -2,9 +2,9 @@ import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
import { getOwner, setOwner } from "@ember/owner";
import { service } from "@ember/service";
-import { TrackedArray } from "@ember-compat/tracked-built-ins";
import { uniqueItemsFromArray } from "discourse/lib/array-tools";
import { NotificationLevels } from "discourse/lib/notification-levels";
+import { trackedArray } from "discourse/lib/tracked-tools";
import Topic from "discourse/models/topic";
export default class BulkSelectHelper {
@@ -18,7 +18,7 @@ export default class BulkSelectHelper {
@tracked autoAddBookmarksToBulkSelect = false;
@tracked lastCheckedElementId = null;
- selected = new TrackedArray();
+ @trackedArray selected = [];
constructor(context) {
setOwner(this, getOwner(context));
diff --git a/frontend/discourse/app/routes/group/activity/posts.js b/frontend/discourse/app/routes/group/activity/posts.js
index b7b35073445..23e1cc44f0d 100644
--- a/frontend/discourse/app/routes/group/activity/posts.js
+++ b/frontend/discourse/app/routes/group/activity/posts.js
@@ -1,4 +1,5 @@
-import { action, get } from "@ember/object";
+import { action } from "@ember/object";
+import { TrackedArray } from "@ember-compat/tracked-built-ins";
import DiscourseRoute from "discourse/routes/discourse";
import { i18n } from "discourse-i18n";
@@ -12,9 +13,14 @@ export function buildGroupPage(type) {
return i18n(`groups.${type}`);
}
- model(params, transition) {
- let categoryId = get(transition.to, "queryParams.category_id");
- return this.modelFor("group").findPosts({ type, categoryId });
+ async model(params, transition) {
+ const categoryId = transition.to.queryParams.category_id;
+ const posts = await this.modelFor("group").findPosts({
+ type,
+ categoryId,
+ });
+
+ return new TrackedArray(posts);
}
setupController(controller, model) {
diff --git a/frontend/discourse/app/routes/posts.js b/frontend/discourse/app/routes/posts.js
index 1d178e0e9d3..2df2061929b 100644
--- a/frontend/discourse/app/routes/posts.js
+++ b/frontend/discourse/app/routes/posts.js
@@ -1,8 +1,10 @@
+import { TrackedArray } from "@ember-compat/tracked-built-ins";
import Posts from "discourse/models/posts";
import DiscourseRoute from "discourse/routes/discourse";
export default class PostsRoute extends DiscourseRoute {
async model() {
- return Posts.find();
+ const posts = await Posts.find();
+ return new TrackedArray(posts);
}
}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/admin-report-sentiment-analysis.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/admin-report-sentiment-analysis.gjs
index 2bd2776c898..c98b13821e8 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/admin-report-sentiment-analysis.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/admin-report-sentiment-analysis.gjs
@@ -5,6 +5,7 @@ import { on } from "@ember/modifier";
import { action } from "@ember/object";
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
import { service } from "@ember/service";
+import { TrackedArray } from "@ember-compat/tracked-built-ins";
import { modifier } from "ember-modifier";
import { and } from "truth-helpers";
import DButton from "discourse/components/d-button";
@@ -135,19 +136,23 @@ export default class AdminReportSentimentAnalysis extends Component {
}
get filteredPosts() {
+ let list;
+
if (!this.posts || !this.posts.length) {
- return [];
+ list = [];
+ } else {
+ list = this.posts.filter((post) => {
+ post.topic_title = replaceEmoji(post.topic_title);
+ post.category = Category.findById(post.category_id);
+
+ if (this.activeFilter === "all") {
+ return true;
+ }
+ return post.sentiment === this.activeFilter;
+ });
}
- return this.posts.filter((post) => {
- post.topic_title = replaceEmoji(post.topic_title);
- post.category = Category.findById(post.category_id);
-
- if (this.activeFilter === "all") {
- return true;
- }
- return post.sentiment === this.activeFilter;
- });
+ return new TrackedArray(list);
}
get postFilters() {
@@ -278,7 +283,7 @@ export default class AdminReportSentimentAnalysis extends Component {
this.nextOffset = response.next_offset;
const mappedPosts = response.posts.map((post) => Post.create(post));
- this.posts.pushObjects(mappedPosts);
+ this.posts.push(mappedPosts);
return mappedPosts;
} catch (e) {
popupAjaxError(e);
diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/controllers/user-activity/reactions.js b/plugins/discourse-reactions/assets/javascripts/discourse/controllers/user-activity/reactions.js
index 7d54dd85a99..f19b8c5d766 100644
--- a/plugins/discourse-reactions/assets/javascripts/discourse/controllers/user-activity/reactions.js
+++ b/plugins/discourse-reactions/assets/javascripts/discourse/controllers/user-activity/reactions.js
@@ -2,6 +2,7 @@ import { tracked } from "@glimmer/tracking";
import Controller, { inject as controller } from "@ember/controller";
import { action } from "@ember/object";
import { service } from "@ember/service";
+import { addUniqueValuesToArray } from "discourse/lib/array-tools";
import CustomReaction from "../../models/discourse-reactions-custom-reaction";
export default class UserActivityReactions extends Controller {
@@ -42,7 +43,7 @@ export default class UserActivityReactions extends Controller {
}
@action
- loadMore() {
+ async loadMore() {
if (!this.canLoadMore || this.loading) {
return;
}
@@ -50,27 +51,32 @@ export default class UserActivityReactions extends Controller {
this.loading = true;
const reactionUsers = this.model;
- if (!this.beforeReactionUserId) {
- this.#updateBeforeIds(reactionUsers);
- }
+ try {
+ if (!this.beforeReactionUserId) {
+ this.#updateBeforeIds(reactionUsers);
+ }
+
+ const opts = {
+ actingUsername: this.actingUsername,
+ includeLikes: this.includeLikes,
+ beforeLikeId: this.beforeLikeId,
+ beforeReactionUserId: this.beforeReactionUserId,
+ };
- const opts = {
- actingUsername: this.actingUsername,
- includeLikes: this.includeLikes,
- beforeLikeId: this.beforeLikeId,
- beforeReactionUserId: this.beforeReactionUserId,
- };
+ const newReactionUsers = await CustomReaction.findReactions(
+ this.reactionsUrl,
+ this.username,
+ opts
+ );
- CustomReaction.findReactions(this.reactionsUrl, this.username, opts)
- .then((newReactionUsers) => {
- reactionUsers.addObjects(newReactionUsers);
- this.#updateBeforeIds(newReactionUsers);
- if (newReactionUsers.length === 0) {
- this.canLoadMore = false;
- }
- })
- .finally(() => {
- this.loading = false;
- });
+ addUniqueValuesToArray(reactionUsers, newReactionUsers);
+ this.#updateBeforeIds(newReactionUsers);
+
+ if (newReactionUsers.length === 0) {
+ this.canLoadMore = false;
+ }
+ } finally {
+ this.loading = false;
+ }
}
}
diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/routes/user-activity/reactions.js b/plugins/discourse-reactions/assets/javascripts/discourse/routes/user-activity/reactions.js
index 103755dcb8d..e2c03929519 100644
--- a/plugins/discourse-reactions/assets/javascripts/discourse/routes/user-activity/reactions.js
+++ b/plugins/discourse-reactions/assets/javascripts/discourse/routes/user-activity/reactions.js
@@ -1,12 +1,15 @@
+import { TrackedArray } from "@ember-compat/tracked-built-ins";
import DiscourseRoute from "discourse/routes/discourse";
import CustomReaction from "../../models/discourse-reactions-custom-reaction";
export default class UserActivityReactions extends DiscourseRoute {
- model() {
- return CustomReaction.findReactions(
+ async model() {
+ const list = await CustomReaction.findReactions(
"reactions",
- this.modelFor("user").get("username")
+ this.modelFor("user").username
);
+
+ return new TrackedArray(list);
}
setupController(controller, model) {
@@ -15,7 +18,7 @@ export default class UserActivityReactions extends DiscourseRoute {
model,
canLoadMore: !loadedAll,
reactionsUrl: "reactions",
- username: this.modelFor("user").get("username"),
+ username: this.modelFor("user").username,
});
}
}