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/services/chat-state-manager.js
Joffrey JAFFEUX f12724b5a5
DEV: routable chat part 2 (#20232)
This commit is expanding on previous work making everything chat working through an URL.

Improves drawer templates to be all URLs
Implements some kind of router for the drawer
Removes few remaining actions for opening channels
2023-02-14 11:27:07 +01:00

123 lines
2.8 KiB
JavaScript

import Service, { inject as service } from "@ember/service";
import { defaultHomepage } from "discourse/lib/utilities";
import { tracked } from "@glimmer/tracking";
import KeyValueStore from "discourse/lib/key-value-store";
import Site from "discourse/models/site";
const PREFERRED_MODE_KEY = "preferred_mode";
const PREFERRED_MODE_STORE_NAMESPACE = "discourse_chat_";
const FULL_PAGE_CHAT = "FULL_PAGE_CHAT";
const DRAWER_CHAT = "DRAWER_CHAT";
export default class ChatStateManager extends Service {
@service chat;
@service router;
isDrawerExpanded = false;
isDrawerActive = false;
isSidePanelExpanded = false;
@tracked _chatURL = null;
@tracked _appURL = null;
_store = new KeyValueStore(PREFERRED_MODE_STORE_NAMESPACE);
reset() {
this._store.remove(PREFERRED_MODE_KEY);
this._chatURL = null;
this._appURL = null;
}
prefersFullPage() {
this._store.setObject({ key: PREFERRED_MODE_KEY, value: FULL_PAGE_CHAT });
}
prefersDrawer() {
this._store.setObject({ key: PREFERRED_MODE_KEY, value: DRAWER_CHAT });
}
openSidePanel() {
this.set("isSidePanelExpanded", true);
}
closeSidePanel() {
this.set("isSidePanelExpanded", false);
}
didOpenDrawer(url = null) {
this.set("isDrawerActive", true);
this.set("isDrawerExpanded", true);
if (url) {
this.storeChatURL(url);
}
this.chat.updatePresence();
}
didCloseDrawer() {
this.set("isDrawerActive", false);
this.set("isDrawerExpanded", false);
this.chat.updatePresence();
}
didExpandDrawer() {
this.set("isDrawerActive", true);
this.set("isDrawerExpanded", true);
this.chat.updatePresence();
}
didCollapseDrawer() {
this.set("isDrawerActive", true);
this.set("isDrawerExpanded", false);
}
didToggleDrawer() {
this.set("isDrawerExpanded", !this.isDrawerExpanded);
this.set("isDrawerActive", true);
}
get isFullPagePreferred() {
return !!(
Site.currentProp("mobileView") ||
this._store.getObject(PREFERRED_MODE_KEY) === FULL_PAGE_CHAT
);
}
get isDrawerPreferred() {
return !!(
!this.isFullPagePreferred ||
(!Site.currentProp("mobileView") &&
(!this._store.getObject(PREFERRED_MODE_KEY) ||
this._store.getObject(PREFERRED_MODE_KEY) === DRAWER_CHAT))
);
}
get isFullPageActive() {
return this.router.currentRouteName?.startsWith("chat");
}
get isActive() {
return this.isFullPageActive || this.isDrawerActive;
}
storeAppURL(url = null) {
this._appURL = url || this.router.currentURL;
}
storeChatURL(url = null) {
this._chatURL = url || this.router.currentURL;
}
get lastKnownAppURL() {
let url = this._appURL;
if (!url || url === "/") {
url = this.router.urlFor(`discovery.${defaultHomepage()}`);
}
return url;
}
get lastKnownChatURL() {
return this._chatURL || "/chat";
}
}