This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/plugins/chat/assets/javascripts/discourse/components/chat-replying-indicator.js
Roman Rizzi 0a5f548635
DEV: Move discourse-chat to the core repo. (#18776)
As part of this move, we are also renaming `discourse-chat` to `chat`.
2022-11-02 10:41:30 -03:00

86 lines
2.1 KiB
JavaScript

import { isBlank, isPresent } from "@ember/utils";
import Component from "@ember/component";
import { inject as service } from "@ember/service";
import discourseComputed from "discourse-common/utils/decorators";
import I18n from "I18n";
import { fmt } from "discourse/lib/computed";
import { next } from "@ember/runloop";
export default Component.extend({
tagName: "",
presence: service(),
presenceChannel: null,
chatChannel: null,
@discourseComputed("presenceChannel.users.[]")
usernames(users) {
return users
?.filter((u) => u.id !== this.currentUser.id)
?.mapBy("username");
},
@discourseComputed("usernames.[]")
text(usernames) {
if (isBlank(usernames)) {
return;
}
if (usernames.length === 1) {
return I18n.t("chat.replying_indicator.single_user", {
username: usernames[0],
});
}
if (usernames.length < 4) {
const lastUsername = usernames.pop();
const commaSeparatedUsernames = usernames.join(", ");
return I18n.t("chat.replying_indicator.multiple_users", {
commaSeparatedUsernames,
lastUsername,
});
}
const commaSeparatedUsernames = usernames.slice(0, 2).join(", ");
return I18n.t("chat.replying_indicator.many_users", {
commaSeparatedUsernames,
count: usernames.length - 2,
});
},
@discourseComputed("usernames.[]")
shouldDisplay(usernames) {
return isPresent(usernames);
},
channelName: fmt("chatChannel.id", "/chat-reply/%@"),
didReceiveAttrs() {
this._super(...arguments);
if (!this.chatChannel || this.chatChannel.isDraft) {
this.presenceChannel?.unsubscribe();
return;
}
if (this.presenceChannel?.name !== this.channelName) {
this.presenceChannel?.unsubscribe();
next(() => {
if (this.isDestroyed || this.isDestroying) {
return;
}
const presenceChannel = this.presence.getChannel(this.channelName);
this.set("presenceChannel", presenceChannel);
presenceChannel.subscribe();
});
}
},
willDestroyElement() {
this._super(...arguments);
this.presenceChannel?.unsubscribe();
},
});