DEV: Clean up user-tips service (#25361)

1. TrackedSet instead of TrackedMap of `true`s
2. Use Set#has for early exit
3. Use the site service instead of Site import
This commit is contained in:
Jarek Radosz 2024-01-22 12:13:02 +01:00 committed by GitHub
parent abbc6cf314
commit 4035513901
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,7 +1,6 @@
import Service, { inject as service } from "@ember/service";
import { TrackedMap } from "@ember-compat/tracked-built-ins";
import { TrackedSet } from "@ember-compat/tracked-built-ins";
import { disableImplicitInjections } from "discourse/lib/implicit-injections";
import Site from "discourse/models/site";
import { isTesting } from "discourse-common/config/environment";
@disableImplicitInjections
@ -11,15 +10,14 @@ export default class UserTips extends Service {
#availableTips = new Set();
#renderedId;
#shouldRenderMap = new TrackedMap();
#shouldRenderSet = new TrackedSet();
#updateRenderedId() {
const tipsArray = [...this.#availableTips];
if (tipsArray.find((tip) => tip.id === this.#renderedId)) {
if (this.#availableTips.has(this.#renderedId)) {
return;
}
const newId = tipsArray
const newId = [...this.#availableTips]
.sortBy("priority")
.reverse()
.find((tip) => {
@ -29,14 +27,14 @@ export default class UserTips extends Service {
})?.id;
if (this.#renderedId !== newId) {
this.#shouldRenderMap.delete(this.#renderedId);
this.#shouldRenderMap.set(newId, true);
this.#shouldRenderSet.delete(this.#renderedId);
this.#shouldRenderSet.add(newId);
this.#renderedId = newId;
}
}
shouldRender(id) {
return this.#shouldRenderMap.get(id);
return this.#shouldRenderSet.has(id);
}
addAvailableTip(tip) {
@ -54,8 +52,7 @@ export default class UserTips extends Service {
return false;
}
const userTips = Site.currentProp("user_tips");
const userTips = this.site.user_tips;
if (!userTips || this.currentUser.user_option?.skip_new_user_tips) {
return false;
}
@ -81,7 +78,7 @@ export default class UserTips extends Service {
return;
}
const userTips = Site.currentProp("user_tips");
const userTips = this.site.user_tips;
if (!userTips || this.currentUser.user_option?.skip_new_user_tips) {
return;
}