discourse/app/assets/javascripts/admin/addon/services/admin-emojis.js
Sérgio Saquetim 0c4f285618
DEV: Replace deprecated Ember's array uniq and uniqBy (#35227)
This commit replaces several instances of `.uniq`, `.uniqBy`, and related
array deduplication methods with a new utility function
`uniqueItemsFromArray`. This change ensures proper deduplication logic
across the codebase while addressing the deprecation issues.

**Main Changes:**

* Created new utility function `uniqueItemsFromArray`: Located in a new
file, `array-tools.js`, this function provides a reusable and
configurable alternative for deduplication.
* Replaced old methods: Updated multiple files to use the new utility
function instead of deprecated or custom implementations for
deduplication.
* Replaced manual deduplication: Replace uses of `[...new Set(array)]`
to standardize the deduplication logic across the codebase.
* Added unit tests: Introduced thorough test coverage
(`array-tools-test.js`) to validate functionality and edge cases for the
utility function.
* Updated deprecation workflow: Added logging for deprecated uniq and
uniqBy methods to the `deprecation-workflow.js`.

This change is primarily focused on code quality improvements, ensuring
future-proof deduplication, and maintaining alignment with deprecation
guidelines.
2025-10-13 16:47:36 -03:00

77 lines
1.8 KiB
JavaScript
Vendored

import { tracked } from "@glimmer/tracking";
import EmberObject, { action } from "@ember/object";
import Service, { service } from "@ember/service";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { uniqueItemsFromArray } from "discourse/lib/array-tools";
import { i18n } from "discourse-i18n";
const ALL_FILTER = "all";
const DEFAULT_GROUP = "default";
export default class AdminEmojis extends Service {
@service dialog;
@tracked emojis = [];
@tracked filter = ALL_FILTER;
@tracked sorting = ["group", "name"];
constructor() {
super(...arguments);
this.#fetchEmojis();
}
get filteredEmojis() {
if (!this.filter || this.filter === ALL_FILTER) {
return this.emojis;
} else {
return this.emojis.filter((e) => e.group === this.filter);
}
}
get sortedEmojis() {
return this.filteredEmojis.sort((a, b) => a.name.localeCompare(b.name));
}
get emojiGroups() {
return uniqueItemsFromArray(
[DEFAULT_GROUP].concat(this.emojis.map((e) => e.group))
);
}
get filteringGroups() {
return [ALL_FILTER].concat(this.emojiGroups);
}
async #fetchEmojis() {
try {
const data = await ajax("/admin/config/emoji.json");
this.emojis = data.map((emoji) => EmberObject.create(emoji));
} catch (err) {
popupAjaxError(err);
}
}
@action
destroyEmoji(emoji) {
this.dialog.deleteConfirm({
title: i18n("admin.emoji.delete_confirm", {
name: emoji.get("name"),
}),
didConfirm: () => this.#destroyEmoji(emoji),
});
}
async #destroyEmoji(emoji) {
try {
await ajax("/admin/config/emoji/" + emoji.get("name"), {
type: "DELETE",
});
this.emojis.removeObject(emoji);
} catch (err) {
popupAjaxError(err);
}
}
}