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