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/routes/chat.js
Joffrey JAFFEUX 21570410ab
FIX: makes sidebar links respect drawer mode (#18918)
Clicking a link of the sidebar will now open the drawer and load the correct channel.

This solution should correctly solve these cases:

closing drawer, clicking sidebar channel, should open the drawer on correct channel
visiting /chat then visiting / and clicking sidebar channel, should open full page chat on correct channel
2022-11-08 16:23:13 +01:00

58 lines
1.6 KiB
JavaScript

import DiscourseRoute from "discourse/routes/discourse";
import I18n from "I18n";
import { defaultHomepage } from "discourse/lib/utilities";
import { inject as service } from "@ember/service";
import { scrollTop } from "discourse/mixins/scroll-top";
import { schedule } from "@ember/runloop";
export default class ChatRoute extends DiscourseRoute {
@service chat;
@service router;
@service fullPageChat;
@service chatPreferredMode;
titleToken() {
return I18n.t("chat.title_capitalized");
}
beforeModel(transition) {
if (
transition.from && // don't intercept when directly loading chat
this.chatPreferredMode.isDrawer &&
transition.intent?.name === "chat.channel" // sidebar can only load a channel
) {
transition.abort();
const id = transition.intent.contexts[0];
return this.chat.getChannelBy("id", id).then((channel) => {
this.appEvents.trigger("chat:open-channel", channel);
});
}
if (!this.chat.userCanChat) {
return this.router.transitionTo(`discovery.${defaultHomepage()}`);
}
this.fullPageChat.enter(this.router.currentURL);
}
activate() {
this.chat.updatePresence();
schedule("afterRender", () => {
document.body.classList.add("has-full-page-chat");
document.documentElement.classList.add("has-full-page-chat");
});
}
deactivate() {
this.fullPageChat.exit();
this.chat.setActiveChannel(null);
schedule("afterRender", () => {
document.body.classList.remove("has-full-page-chat");
document.documentElement.classList.remove("has-full-page-chat");
scrollTop();
});
}
}