FIX: Improve chat route cleanup (#20557)
1. `this.chat.activeChannel = null` was being done in twice 2. using `willTransition()` and checking transition.to.name prefix for route cleanup rather than using `deactivate()` was unnecessarily verbose and could be premature (if something aborted the transition you'd end up in a broken state) 3. `activeChannel` on Chat service can be null, check for that before accessing
This commit is contained in:
parent
6487c8ef24
commit
a1b35601fc
|
@ -23,6 +23,8 @@ export default function withChatChannel(extendedClass) {
|
||||||
}
|
}
|
||||||
|
|
||||||
afterModel(model) {
|
afterModel(model) {
|
||||||
|
super.afterModel?.(...arguments);
|
||||||
|
|
||||||
this.controllerFor("chat-channel").set("targetMessageId", null);
|
this.controllerFor("chat-channel").set("targetMessageId", null);
|
||||||
this.chat.activeChannel = model;
|
this.chat.activeChannel = model;
|
||||||
|
|
||||||
|
|
|
@ -1,32 +1,21 @@
|
||||||
import DiscourseRoute from "discourse/routes/discourse";
|
import DiscourseRoute from "discourse/routes/discourse";
|
||||||
import withChatChannel from "./chat-channel-decorator";
|
import withChatChannel from "./chat-channel-decorator";
|
||||||
import { inject as service } from "@ember/service";
|
import { inject as service } from "@ember/service";
|
||||||
import { action } from "@ember/object";
|
|
||||||
|
|
||||||
@withChatChannel
|
@withChatChannel
|
||||||
export default class ChatChannelRoute extends DiscourseRoute {
|
export default class ChatChannelRoute extends DiscourseRoute {
|
||||||
@service chat;
|
@service chat;
|
||||||
@service chatStateManager;
|
@service chatStateManager;
|
||||||
|
|
||||||
@action
|
beforeModel() {
|
||||||
willTransition(transition) {
|
if (this.chat.activeChannel) {
|
||||||
|
// When moving between channels
|
||||||
this.#closeThread();
|
this.#closeThread();
|
||||||
|
|
||||||
if (transition?.to?.name === "chat.channel.index") {
|
|
||||||
const targetChannelId = transition?.to?.parent?.params?.channelId;
|
|
||||||
if (
|
|
||||||
targetChannelId &&
|
|
||||||
parseInt(targetChannelId, 10) !== this.chat.activeChannel.id
|
|
||||||
) {
|
|
||||||
this.chat.activeChannel.messagesManager.clearMessages();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!transition?.to?.name?.startsWith("chat.")) {
|
deactivate() {
|
||||||
this.chatStateManager.storeChatURL();
|
this.#closeThread();
|
||||||
this.chat.activeChannel = null;
|
|
||||||
this.chat.updatePresence();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#closeThread() {
|
#closeThread() {
|
||||||
|
|
|
@ -4,7 +4,6 @@ import { defaultHomepage } from "discourse/lib/utilities";
|
||||||
import { inject as service } from "@ember/service";
|
import { inject as service } from "@ember/service";
|
||||||
import { scrollTop } from "discourse/mixins/scroll-top";
|
import { scrollTop } from "discourse/mixins/scroll-top";
|
||||||
import { schedule } from "@ember/runloop";
|
import { schedule } from "@ember/runloop";
|
||||||
import { action } from "@ember/object";
|
|
||||||
|
|
||||||
export default class ChatRoute extends DiscourseRoute {
|
export default class ChatRoute extends DiscourseRoute {
|
||||||
@service chat;
|
@service chat;
|
||||||
|
@ -65,23 +64,17 @@ export default class ChatRoute extends DiscourseRoute {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
deactivate() {
|
deactivate(transition) {
|
||||||
|
const url = this.router.urlFor(transition.from.name);
|
||||||
|
this.chatStateManager.storeChatURL(url);
|
||||||
|
|
||||||
|
this.chat.activeChannel = null;
|
||||||
|
this.chat.updatePresence();
|
||||||
|
|
||||||
schedule("afterRender", () => {
|
schedule("afterRender", () => {
|
||||||
document.body.classList.remove("has-full-page-chat");
|
document.body.classList.remove("has-full-page-chat");
|
||||||
document.documentElement.classList.remove("has-full-page-chat");
|
document.documentElement.classList.remove("has-full-page-chat");
|
||||||
scrollTop();
|
scrollTop();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@action
|
|
||||||
willTransition(transition) {
|
|
||||||
if (!transition?.to?.name?.startsWith("chat.channel")) {
|
|
||||||
this.chat.activeChannel = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!transition?.to?.name?.startsWith("chat.")) {
|
|
||||||
this.chatStateManager.storeChatURL();
|
|
||||||
this.chat.updatePresence();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -113,11 +113,17 @@ export default class ChatStateManager extends Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
storeAppURL(url = null) {
|
storeAppURL(url = null) {
|
||||||
this._appURL = url || this.router.currentURL;
|
if (url) {
|
||||||
|
this._appURL = url;
|
||||||
|
} else if (this.router.currentURL?.startsWith("/chat")) {
|
||||||
|
this._appURL = "/";
|
||||||
|
} else {
|
||||||
|
this._appURL = this.router.currentURL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
storeChatURL(url = null) {
|
storeChatURL(url) {
|
||||||
this._chatURL = url || this.router.currentURL;
|
this._chatURL = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
get lastKnownAppURL() {
|
get lastKnownAppURL() {
|
||||||
|
|
|
@ -53,11 +53,6 @@ module(
|
||||||
test("lastKnownChatURL", function (assert) {
|
test("lastKnownChatURL", function (assert) {
|
||||||
assert.strictEqual(this.subject.lastKnownChatURL, "/chat");
|
assert.strictEqual(this.subject.lastKnownChatURL, "/chat");
|
||||||
|
|
||||||
sinon.stub(this.subject.router, "currentURL").value("/foo");
|
|
||||||
this.subject.storeChatURL();
|
|
||||||
|
|
||||||
assert.strictEqual(this.subject.lastKnownChatURL, "/foo");
|
|
||||||
|
|
||||||
this.subject.storeChatURL("/bar");
|
this.subject.storeChatURL("/bar");
|
||||||
|
|
||||||
assert.strictEqual(this.subject.lastKnownChatURL, "/bar");
|
assert.strictEqual(this.subject.lastKnownChatURL, "/bar");
|
||||||
|
|
Loading…
Reference in New Issue