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 66130dc8c1
REFACTOR: handles every chat resource as an URL (#18961)
- Note this is also tweaking the UI a little bit as we are now using links/buttons in the header as needed
- It disables the find ideal channel in drawer mode, if loading `/chat` in drawer mode it will either reopen at the last position or just stay on index
2022-11-11 06:39:15 +01:00

85 lines
2.2 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";
import { action } from "@ember/object";
export default class ChatRoute extends DiscourseRoute {
@service chat;
@service router;
@service chatStateManager;
titleToken() {
return I18n.t("chat.title_capitalized");
}
beforeModel(transition) {
if (!this.chat.userCanChat) {
return this.router.transitionTo(`discovery.${defaultHomepage()}`);
}
const INTERCEPTABLE_ROUTES = [
"chat.channel.index",
"chat.channel",
"chat",
"chat.index",
"chat.draft-channel",
];
if (
transition.from && // don't intercept when directly loading chat
this.chatStateManager.isDrawerPreferred &&
INTERCEPTABLE_ROUTES.includes(transition.targetName)
) {
transition.abort();
let URL = transition.intent.url;
if (
transition.targetName === "chat.channel.index" ||
transition.targetName === "chat.channel"
) {
URL ??= this.router.urlFor(
transition.targetName,
...transition.intent.contexts
);
} else {
URL ??= this.router.urlFor(transition.targetName);
}
this.appEvents.trigger("chat:open-url", URL);
return;
}
this.appEvents.trigger("chat:toggle-close");
}
activate() {
this.chatStateManager.storeAppURL();
this.chat.updatePresence();
schedule("afterRender", () => {
document.body.classList.add("has-full-page-chat");
document.documentElement.classList.add("has-full-page-chat");
});
}
deactivate() {
this.chat.setActiveChannel(null);
schedule("afterRender", () => {
document.body.classList.remove("has-full-page-chat");
document.documentElement.classList.remove("has-full-page-chat");
scrollTop();
});
}
@action
willTransition(transition) {
if (!transition?.to?.name?.startsWith("chat.")) {
this.chatStateManager.storeChatURL();
}
}
}