FIX: make the check better for drawer router (#28212)

* FIX: make the check better for drawer rerouter

* adds a redirect method to chat-drawer-routes

---------

Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
This commit is contained in:
Gabriel Grubba 2024-08-02 17:53:43 -03:00 committed by GitHub
parent 4167862a05
commit 2b577950af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 38 additions and 39 deletions

View File

@ -11,7 +11,28 @@ import ChatDrawerRoutesDirectMessages from "discourse/plugins/chat/discourse/com
import ChatDrawerRoutesThreads from "discourse/plugins/chat/discourse/components/chat/drawer-routes/threads";
const ROUTES = {
chat: { name: ChatDrawerRoutesChannels },
chat: {
name: ChatDrawerRoutesChannels,
redirect: (context) => {
if (
context.siteSettings.chat_preferred_index === "my_threads" &&
context.hasThreads
) {
return "/chat/threads";
}
if (
context.siteSettings.chat_preferred_index === "direct_messages" &&
context.hasDirectMessages
) {
return "/chat/direct-messages";
}
if (!context.siteSettings.enable_public_channels) {
return "/chat/direct-messages";
}
},
},
"chat.index": { name: ChatDrawerRoutesChannels },
// order matters, non index before index
"chat.browse": {
@ -154,6 +175,22 @@ export default class ChatDrawerRouter extends Service {
return this.chat.userCanAccessDirectMessages;
}
stateFor(route) {
this.drawerRoute?.deactivate?.(this.chatHistory.currentRoute);
this.chatHistory.visit(route);
this.drawerRoute = ROUTES[route.name];
this.params = this.drawerRoute?.extractParams?.(route) || route.params;
this.component = this.drawerRoute?.name || ChatDrawerRoutesChannels;
this.currentRouteName = route.name;
this.drawerRoute.activate?.(route);
const redirectedRoute = this.drawerRoute.redirect?.(this);
if (redirectedRoute) {
this.stateFor(this.#routeFromURL(redirectedRoute));
}
}
#routeFromURL(url) {
let route = this.router.recognize(url);
@ -164,42 +201,4 @@ export default class ChatDrawerRouter extends Service {
return route;
}
#redirect() {
if (
this.siteSettings.chat_preferred_index === "my_threads" &&
this.hasThreads
) {
return this.stateFor(this.#routeFromURL("/chat/threads"));
}
if (
this.siteSettings.chat_preferred_index === "direct_messages" &&
this.hasDirectMessages
) {
return this.stateFor(this.#routeFromURL("/chat/direct-messages"));
}
if (!this.siteSettings.enable_public_channels) {
return this.stateFor(this.#routeFromURL("/chat/direct-messages"));
}
}
stateFor(route) {
this.drawerRoute?.deactivate?.(this.chatHistory.currentRoute);
this.chatHistory.visit(route);
this.drawerRoute = ROUTES[route.name];
this.params = this.drawerRoute?.extractParams?.(route) || route.params;
this.component = this.drawerRoute?.name || ChatDrawerRoutesChannels;
if (
!this.chatStateManager.isDrawerActive && // only when opening the drawer
this.component.name === "ChatDrawerRoutesChannels" // we should check if redirect to channels
) {
this.#redirect();
}
this.currentRouteName = route.name;
this.drawerRoute.activate?.(route);
}
}