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 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

74 lines
1.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 router;
@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 });
}
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 isFullPage() {
return this.router.currentRouteName?.startsWith("chat");
}
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";
}
}