DEV: Refactor UnreadIndicator (#28969)

Moves related code from topic-cell to the component. Also fixes a couple autotracking cases in topic-cell.
This commit is contained in:
Jarek Radosz 2024-10-08 02:50:37 +09:00 committed by GitHub
parent 50cb7b897e
commit 1ba8b6b22a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 60 deletions

View File

@ -15,48 +15,12 @@ import categoryLink from "discourse/helpers/category-link";
import discourseTags from "discourse/helpers/discourse-tags";
import topicFeaturedLink from "discourse/helpers/topic-featured-link";
import { groupPath } from "discourse/lib/url";
import { bind } from "discourse-common/utils/decorators";
import I18n from "discourse-i18n";
export default class TopicCell extends Component {
@service currentUser;
@service messageBus;
constructor() {
super(...arguments);
if (this.includeUnreadIndicator) {
this.messageBus.subscribe(this.unreadIndicatorChannel, this.onMessage);
}
}
willDestroy() {
super.willDestroy(...arguments);
this.messageBus.unsubscribe(this.unreadIndicatorChannel, this.onMessage);
}
@bind
onMessage(data) {
const nodeClassList = document.querySelector(
`.indicator-topic-${data.topic_id}`
).classList;
nodeClassList.toggle("read", !data.show_indicator);
}
get unreadIndicatorChannel() {
return `/private-messages/unread-indicator/${this.args.topic.id}`;
}
get includeUnreadIndicator() {
return typeof this.args.topic.unread_by_group_member !== "undefined";
}
get unreadClass() {
return this.args.topic.unread_by_group_member ? "" : "read";
}
get newDotText() {
return this.currentUser?.trust_level > 0
? ""
@ -64,11 +28,11 @@ export default class TopicCell extends Component {
}
get participantGroups() {
if (!this.args.topic.participant_groups) {
if (!this.args.topic.get("participant_groups")) {
return [];
}
return this.args.topic.participant_groups.map((name) => ({
return this.args.topic.get("participant_groups").map((name) => ({
name,
url: groupPath(name),
}));
@ -115,11 +79,7 @@ export default class TopicCell extends Component {
@outletArgs={{hash topic=@topic}}
/>
{{~! no whitespace ~}}
<UnreadIndicator
@includeUnreadIndicator={{this.includeUnreadIndicator}}
@topicId={{@topic.id}}
class={{this.unreadClass}}
/>
<UnreadIndicator @topic={{@topic}} />
{{~#if @showTopicPostBadges~}}
<TopicPostBadges
@unreadPosts={{@topic.unread_posts}}

View File

@ -1,21 +1,43 @@
import { concat } from "@ember/helper";
import concatClass from "discourse/helpers/concat-class";
import Component from "@glimmer/component";
import { service } from "@ember/service";
import icon from "discourse-common/helpers/d-icon";
import i18n from "discourse-common/helpers/i18n";
import { bind } from "discourse-common/utils/decorators";
const UnreadIndicator = <template>
{{~#if @includeUnreadIndicator~}}
&nbsp;<span
title={{i18n "topic.unread_indicator"}}
class={{concatClass
"badge badge-notification unread-indicator"
(concat "indicator-topic-" @topicId)
}}
...attributes
>
{{~icon "asterisk"~}}
</span>
{{~/if~}}
</template>;
export default class UnreadIndicator extends Component {
@service messageBus;
export default UnreadIndicator;
constructor() {
super(...arguments);
this.messageBus.subscribe(this.unreadIndicatorChannel, this.onMessage);
}
willDestroy() {
super.willDestroy(...arguments);
this.messageBus.unsubscribe(this.unreadIndicatorChannel, this.onMessage);
}
@bind
onMessage(data) {
this.args.topic.set("unread_by_group_member", data.show_indicator);
}
get unreadIndicatorChannel() {
return `/private-messages/unread-indicator/${this.args.topic.id}`;
}
get isUnread() {
return typeof this.args.topic.get("unread_by_group_member") !== "undefined";
}
<template>
{{~#if this.isUnread~}}
&nbsp;<span
title={{i18n "topic.unread_indicator"}}
class="badge badge-notification unread-indicator"
>
{{~icon "asterisk"~}}
</span>
{{~/if~}}
</template>
}