diff --git a/app/assets/javascripts/discourse/app/components/sidebar/messages-section.js b/app/assets/javascripts/discourse/app/components/sidebar/messages-section.js index a71dc9dd4b9..9c964bf4ab6 100644 --- a/app/assets/javascripts/discourse/app/components/sidebar/messages-section.js +++ b/app/assets/javascripts/discourse/app/components/sidebar/messages-section.js @@ -1,10 +1,96 @@ import { action } from "@ember/object"; +import { cached } from "@glimmer/tracking"; import GlimmerComponent from "discourse/components/glimmer"; import Composer from "discourse/models/composer"; import { getOwner } from "discourse-common/lib/get-owner"; +import GroupMessageSectionLink from "discourse/lib/sidebar/messages-section/group-message-section-link"; +import PersonalMessageSectionLink from "discourse/lib/sidebar/messages-section/personal-message-section-link"; + +export const INBOX = "inbox"; +const UNREAD = "unread"; +const SENT = "sent"; +const NEW = "new"; +const ARCHIVE = "archive"; + +export const PERSONAL_MESSAGES_INBOXES = [INBOX, UNREAD, NEW, SENT, ARCHIVE]; +export const GROUP_MESSAGES_INBOXES = [INBOX, UNREAD, NEW, ARCHIVE]; export default class SidebarMessagesSection extends GlimmerComponent { + constructor() { + super(...arguments); + + this.appEvents.on( + "page:changed", + this, + this._refreshSectionLinksDisplayState + ); + } + + willDestroy() { + this.appEvents.off( + "page:changed", + this, + this._refreshSectionLinksDisplayState + ); + } + + _refreshSectionLinksDisplayState({ + currentRouteName, + currentRouteParentName, + currentRouteParams, + }) { + const sectionLinks = [ + ...this.personalMessagesSectionLinks, + ...this.groupMessagesSectionLinks, + ]; + + if (currentRouteParentName !== "userPrivateMessages") { + sectionLinks.forEach((sectionLink) => { + sectionLink.collapse(); + }); + } else { + sectionLinks.forEach((sectionLink) => { + sectionLink.pageChanged(currentRouteName, currentRouteParams); + }); + } + } + + @cached + get personalMessagesSectionLinks() { + const links = []; + + PERSONAL_MESSAGES_INBOXES.forEach((type) => { + links.push( + new PersonalMessageSectionLink({ + currentUser: this.currentUser, + type, + }) + ); + }); + + return links; + } + + @cached + get groupMessagesSectionLinks() { + const links = []; + + this.currentUser.groupsWithMessages.forEach((group) => { + GROUP_MESSAGES_INBOXES.forEach((groupMessageLink) => { + links.push( + new GroupMessageSectionLink({ + group, + type: groupMessageLink, + currentUser: this.currentUser, + }) + ); + }); + }); + + return links; + } + @action composePersonalMessage() { const composerArgs = { diff --git a/app/assets/javascripts/discourse/app/lib/page-tracker.js b/app/assets/javascripts/discourse/app/lib/page-tracker.js index 3cb60f36ff5..66316654cf2 100644 --- a/app/assets/javascripts/discourse/app/lib/page-tracker.js +++ b/app/assets/javascripts/discourse/app/lib/page-tracker.js @@ -42,6 +42,8 @@ export function startPageTracking(router, appEvents, documentTitle) { url, title: documentTitle.getTitle(), currentRouteName: router.currentRouteName, + currentRouteParams: router.currentRoute.params, + currentRouteParentName: router.currentRoute.parent?.name, replacedOnlyQueryParams, }); }); diff --git a/app/assets/javascripts/discourse/app/lib/sidebar/messages-section/group-message-section-link.js b/app/assets/javascripts/discourse/app/lib/sidebar/messages-section/group-message-section-link.js new file mode 100644 index 00000000000..9de4993327b --- /dev/null +++ b/app/assets/javascripts/discourse/app/lib/sidebar/messages-section/group-message-section-link.js @@ -0,0 +1,80 @@ +import I18n from "I18n"; + +import { tracked } from "@glimmer/tracking"; +import { capitalize } from "@ember/string"; + +import { INBOX } from "discourse/components/sidebar/messages-section"; + +export default class GroupMessageSectionLink { + @tracked shouldDisplay = this._isInbox; + + routeNames = new Set([ + "userPrivateMessages.group", + "userPrivateMessages.groupUnread", + "userPrivateMessages.groupNew", + "userPrivateMessages.groupArchive", + ]); + + constructor({ group, type, currentUser, router }) { + this.group = group; + this.type = type; + this.currentUser = currentUser; + this.router = router; + } + + get name() { + return `group-messages-${this.type}`; + } + + get class() { + return this.group.name; + } + + get route() { + if (this._isInbox) { + return "userPrivateMessages.group"; + } else { + return `userPrivateMessages.group${capitalize(this.type)}`; + } + } + + get currentWhen() { + if (this._isInbox) { + return [...this.routeNames].join(" "); + } + } + + get models() { + return [this.currentUser, this.group.name]; + } + + get text() { + if (this._isInbox) { + return this.group.name; + } else { + return I18n.t(`sidebar.sections.messages.links.${this.type}`); + } + } + + collapse() { + if (this._isInbox) { + return; + } + + this.shouldDisplay = false; + } + + pageChanged(currentRouteName, currentRouteParams) { + if (this._isInbox) { + return; + } + + this.shouldDisplay = + this.routeNames.has(currentRouteName) && + currentRouteParams.name.toLowerCase() === this.group.name.toLowerCase(); + } + + get _isInbox() { + return this.type === INBOX; + } +} diff --git a/app/assets/javascripts/discourse/app/lib/sidebar/messages-section/personal-message-section-link.js b/app/assets/javascripts/discourse/app/lib/sidebar/messages-section/personal-message-section-link.js new file mode 100644 index 00000000000..66bc38e8564 --- /dev/null +++ b/app/assets/javascripts/discourse/app/lib/sidebar/messages-section/personal-message-section-link.js @@ -0,0 +1,68 @@ +import I18n from "I18n"; + +import { tracked } from "@glimmer/tracking"; +import { INBOX } from "discourse/components/sidebar/messages-section"; + +export default class PersonalMessageSectionLink { + @tracked shouldDisplay = this._isInbox; + + routeNames = new Set([ + "userPrivateMessages.index", + "userPrivateMessages.unread", + "userPrivateMessages.sent", + "userPrivateMessages.new", + "userPrivateMessages.archive", + ]); + + constructor({ currentUser, type, router }) { + this.currentUser = currentUser; + this.type = type; + this.router = router; + } + + get name() { + return `personal-messages-${this.type}`; + } + + get route() { + if (this._isInbox) { + return "userPrivateMessages.index"; + } else { + return `userPrivateMessages.${this.type}`; + } + } + + get currentWhen() { + if (this._isInbox) { + return [...this.routeNames].join(" "); + } + } + + get model() { + return this.currentUser; + } + + get text() { + return I18n.t(`sidebar.sections.messages.links.${this.type}`); + } + + collapse() { + if (this._isInbox) { + return; + } + + this.shouldDisplay = false; + } + + pageChanged(currentRouteName) { + if (this._isInbox) { + return; + } + + this.shouldDisplay = this.routeNames.has(currentRouteName); + } + + get _isInbox() { + return this.type === INBOX; + } +} diff --git a/app/assets/javascripts/discourse/app/templates/components/sidebar.hbs b/app/assets/javascripts/discourse/app/templates/components/sidebar.hbs index 7469358ed58..ee690e68081 100644 --- a/app/assets/javascripts/discourse/app/templates/components/sidebar.hbs +++ b/app/assets/javascripts/discourse/app/templates/components/sidebar.hbs @@ -8,7 +8,9 @@ {{/if}} - + {{#if this.siteSettings.enable_personal_messages}} + + {{/if}} {{/if}} diff --git a/app/assets/javascripts/discourse/app/templates/components/sidebar/messages-section.hbs b/app/assets/javascripts/discourse/app/templates/components/sidebar/messages-section.hbs index 735f04344cc..bed39761eff 100644 --- a/app/assets/javascripts/discourse/app/templates/components/sidebar/messages-section.hbs +++ b/app/assets/javascripts/discourse/app/templates/components/sidebar/messages-section.hbs @@ -7,4 +7,31 @@ @headerLinkText={{i18n "sidebar.sections.messages.header_link_text"}} @headerLinkTitle={{i18n "sidebar.sections.messages.header_link_title"}} > + {{#each this.personalMessagesSectionLinks as |personalMessageSectionLink|}} + {{#if personalMessageSectionLink.shouldDisplay}} + + {{/if}} + {{/each}} + + {{#if (gt this.groupMessagesSectionLinks.length 0)}} +
+ + {{#each this.groupMessagesSectionLinks as |groupMessageSectionLink|}} + {{#if groupMessageSectionLink.shouldDisplay}} + + {{/if}} + {{/each}} + {{/if}} + diff --git a/app/assets/javascripts/discourse/app/templates/components/sidebar/section-link.hbs b/app/assets/javascripts/discourse/app/templates/components/sidebar/section-link.hbs index 049cf540b19..3faa0becac1 100644 --- a/app/assets/javascripts/discourse/app/templates/components/sidebar/section-link.hbs +++ b/app/assets/javascripts/discourse/app/templates/components/sidebar/section-link.hbs @@ -1,6 +1,6 @@