DEV: Use helperFn in d-presence (#29461)

This commit is contained in:
Jarek Radosz 2024-10-29 11:21:33 +01:00 committed by GitHub
parent 7af728048c
commit a236e368e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 66 additions and 67 deletions

View File

@ -1,10 +1,10 @@
import Component from "@glimmer/component";
import { cached, tracked } from "@glimmer/tracking";
import { service } from "@ember/service";
import { modifier } from "ember-modifier";
import { gt } from "truth-helpers";
import UserLink from "discourse/components/user-link";
import avatar from "discourse/helpers/avatar";
import helperFn from "discourse/helpers/helper-fn";
import i18n from "discourse-common/helpers/i18n";
export default class ComposerPresenceDisplay extends Component {
@ -17,7 +17,7 @@ export default class ComposerPresenceDisplay extends Component {
@tracked whisperChannel;
@tracked editChannel;
setupReplyChannel = modifier(() => {
setupReplyChannel = helperFn((_, on) => {
const topic = this.args.model.topic;
if (!topic || !this.isReply) {
return;
@ -29,10 +29,10 @@ export default class ComposerPresenceDisplay extends Component {
replyChannel.subscribe();
this.replyChannel = replyChannel;
return () => replyChannel.unsubscribe();
on.cleanup(() => replyChannel.unsubscribe());
});
setupWhisperChannel = modifier(() => {
setupWhisperChannel = helperFn((_, on) => {
if (
!this.args.model.topic ||
!this.isReply ||
@ -48,10 +48,10 @@ export default class ComposerPresenceDisplay extends Component {
whisperChannel.subscribe();
this.whisperChannel = whisperChannel;
return () => whisperChannel.unsubscribe();
on.cleanup(() => whisperChannel.unsubscribe());
});
setupEditChannel = modifier(() => {
setupEditChannel = helperFn((_, on) => {
if (!this.args.model.post || !this.isEdit) {
return;
}
@ -62,10 +62,10 @@ export default class ComposerPresenceDisplay extends Component {
editChannel.subscribe();
this.editChannel = editChannel;
return () => editChannel.unsubscribe();
on.cleanup(() => editChannel.unsubscribe());
});
notifyState = modifier(() => {
notifyState = helperFn((_, on) => {
const { topic, post, reply } = this.args.model;
const raw = this.isEdit ? post?.raw || "" : "";
const entity = this.isEdit ? post : topic;
@ -74,7 +74,7 @@ export default class ComposerPresenceDisplay extends Component {
this.composerPresenceManager.notifyState(this.state, entity?.id);
}
return () => this.composerPresenceManager.leave();
on.cleanup(() => this.composerPresenceManager.leave());
});
get isReply() {
@ -112,12 +112,11 @@ export default class ComposerPresenceDisplay extends Component {
}
<template>
<div
{{this.setupReplyChannel}}
{{this.setupWhisperChannel}}
{{this.setupEditChannel}}
{{this.notifyState}}
>
{{#if (gt this.users.length 0)}}
<div class="presence-users">
<div class="presence-avatars">
@ -144,6 +143,5 @@ export default class ComposerPresenceDisplay extends Component {
</span>
</div>
{{/if}}
</div>
</template>
}

View File

@ -1,10 +1,10 @@
import Component from "@glimmer/component";
import { cached, tracked } from "@glimmer/tracking";
import { service } from "@ember/service";
import { modifier } from "ember-modifier";
import { gt } from "truth-helpers";
import UserLink from "discourse/components/user-link";
import avatar from "discourse/helpers/avatar";
import helperFn from "discourse/helpers/helper-fn";
import i18n from "discourse-common/helpers/i18n";
export default class TopicPresenceDisplay extends Component {
@ -14,17 +14,17 @@ export default class TopicPresenceDisplay extends Component {
@tracked replyChannel;
@tracked whisperChannel;
setupReplyChannel = modifier(() => {
setupReplyChannel = helperFn((_, on) => {
const replyChannel = this.presence.getChannel(
`/discourse-presence/reply/${this.args.topic.id}`
);
replyChannel.subscribe();
this.replyChannel = replyChannel;
return () => replyChannel.unsubscribe();
on.cleanup(() => replyChannel.unsubscribe());
});
setupWhisperChannels = modifier(() => {
setupWhisperChannels = helperFn((_, on) => {
if (!this.currentUser.staff) {
return;
}
@ -35,7 +35,7 @@ export default class TopicPresenceDisplay extends Component {
whisperChannel.subscribe();
this.whisperChannel = whisperChannel;
return () => whisperChannel.unsubscribe();
on.cleanup(() => whisperChannel.unsubscribe());
});
@cached
@ -49,7 +49,9 @@ export default class TopicPresenceDisplay extends Component {
}
<template>
<div {{this.setupReplyChannel}} {{this.setupWhisperChannels}}>
{{this.setupReplyChannel}}
{{this.setupWhisperChannels}}
{{#if (gt this.users.length 0)}}
<div class="presence-users">
<div class="presence-avatars">
@ -72,6 +74,5 @@ export default class TopicPresenceDisplay extends Component {
</span>
</div>
{{/if}}
</div>
</template>
}