You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserMenu.vue 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <!--
  2. - @copyright 2023 Christopher Ng <chrng8@gmail.com>
  3. -
  4. - @author Christopher Ng <chrng8@gmail.com>
  5. -
  6. - @license AGPL-3.0-or-later
  7. -
  8. - This program is free software: you can redistribute it and/or modify
  9. - it under the terms of the GNU Affero General Public License as
  10. - published by the Free Software Foundation, either version 3 of the
  11. - License, or (at your option) any later version.
  12. -
  13. - This program is distributed in the hope that it will be useful,
  14. - but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. - GNU Affero General Public License for more details.
  17. -
  18. - You should have received a copy of the GNU Affero General Public License
  19. - along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. -
  21. -->
  22. <template>
  23. <NcHeaderMenu id="user-menu"
  24. class="user-menu"
  25. is-nav
  26. :aria-label="t('core', 'Settings menu')"
  27. :description="avatarDescription">
  28. <template #trigger>
  29. <NcAvatar v-if="!isLoadingUserStatus"
  30. class="user-menu__avatar"
  31. :disable-menu="true"
  32. :disable-tooltip="true"
  33. :user="userId"
  34. :preloaded-user-status="userStatus" />
  35. </template>
  36. <ul>
  37. <ProfileUserMenuEntry :id="profileEntry.id"
  38. :name="profileEntry.name"
  39. :href="profileEntry.href"
  40. :active="profileEntry.active" />
  41. <UserMenuEntry v-for="entry in otherEntries"
  42. :id="entry.id"
  43. :key="entry.id"
  44. :name="entry.name"
  45. :href="entry.href"
  46. :active="entry.active"
  47. :icon="entry.icon" />
  48. </ul>
  49. </NcHeaderMenu>
  50. </template>
  51. <script>
  52. import axios from '@nextcloud/axios'
  53. import { emit, subscribe } from '@nextcloud/event-bus'
  54. import { loadState } from '@nextcloud/initial-state'
  55. import { generateOcsUrl } from '@nextcloud/router'
  56. import { getCurrentUser } from '@nextcloud/auth'
  57. import { getCapabilities } from '@nextcloud/capabilities'
  58. import NcAvatar from '@nextcloud/vue/dist/Components/NcAvatar.js'
  59. import NcHeaderMenu from '@nextcloud/vue/dist/Components/NcHeaderMenu.js'
  60. import { getAllStatusOptions } from '../../../apps/user_status/src/services/statusOptionsService.js'
  61. import ProfileUserMenuEntry from '../components/UserMenu/ProfileUserMenuEntry.vue'
  62. import UserMenuEntry from '../components/UserMenu/UserMenuEntry.vue'
  63. import logger from '../logger.js'
  64. /**
  65. * @typedef SettingNavEntry
  66. * @property {string} id - id of the entry, used as HTML ID, for example, "settings"
  67. * @property {string} name - Label of the entry, for example, "Personal Settings"
  68. * @property {string} icon - Icon of the entry, for example, "/apps/settings/img/personal.svg"
  69. * @property {'settings'|'link'|'guest'} type - Type of the entry
  70. * @property {string} href - Link of the entry, for example, "/settings/user"
  71. * @property {boolean} active - Whether the entry is active
  72. * @property {number} order - Order of the entry
  73. * @property {number} unread - Number of unread pf this items
  74. * @property {string} classes - Classes for custom styling
  75. */
  76. /** @type {Record<string, SettingNavEntry>} */
  77. const settingsNavEntries = loadState('core', 'settingsNavEntries', [])
  78. const { profile: profileEntry, ...otherEntries } = settingsNavEntries
  79. const translateStatus = (status) => {
  80. const statusMap = Object.fromEntries(
  81. getAllStatusOptions()
  82. .map(({ type, label }) => [type, label]),
  83. )
  84. if (statusMap[status]) {
  85. return statusMap[status]
  86. }
  87. return status
  88. }
  89. export default {
  90. name: 'UserMenu',
  91. components: {
  92. NcAvatar,
  93. NcHeaderMenu,
  94. ProfileUserMenuEntry,
  95. UserMenuEntry,
  96. },
  97. data() {
  98. return {
  99. profileEntry,
  100. otherEntries,
  101. displayName: getCurrentUser()?.displayName,
  102. userId: getCurrentUser()?.uid,
  103. isLoadingUserStatus: true,
  104. userStatus: {
  105. status: null,
  106. icon: null,
  107. message: null,
  108. },
  109. }
  110. },
  111. computed: {
  112. translatedUserStatus() {
  113. return {
  114. ...this.userStatus,
  115. status: translateStatus(this.userStatus.status),
  116. }
  117. },
  118. avatarDescription() {
  119. const description = [
  120. t('core', 'Avatar of {displayName}', { displayName: this.displayName }),
  121. ...Object.values(this.translatedUserStatus).filter(Boolean),
  122. ].join(' — ')
  123. return description
  124. },
  125. },
  126. async created() {
  127. if (!getCapabilities()?.user_status?.enabled) {
  128. this.isLoadingUserStatus = false
  129. return
  130. }
  131. const url = generateOcsUrl('/apps/user_status/api/v1/user_status')
  132. try {
  133. const response = await axios.get(url)
  134. const { status, icon, message } = response.data.ocs.data
  135. this.userStatus = { status, icon, message }
  136. } catch (e) {
  137. logger.error('Failed to load user status')
  138. }
  139. this.isLoadingUserStatus = false
  140. },
  141. mounted() {
  142. subscribe('user_status:status.updated', this.handleUserStatusUpdated)
  143. emit('core:user-menu:mounted')
  144. },
  145. methods: {
  146. handleUserStatusUpdated(state) {
  147. if (this.userId === state.userId) {
  148. this.userStatus = {
  149. status: state.status,
  150. icon: state.icon,
  151. message: state.message,
  152. }
  153. }
  154. },
  155. },
  156. }
  157. </script>
  158. <style lang="scss" scoped>
  159. .user-menu {
  160. margin-right: 12px;
  161. &:deep {
  162. .header-menu {
  163. &__trigger {
  164. opacity: 1 !important;
  165. &:focus-visible {
  166. .user-menu__avatar {
  167. border: 2px solid var(--color-primary-element);
  168. }
  169. }
  170. }
  171. &__carret {
  172. display: none !important;
  173. }
  174. &__content {
  175. width: fit-content !important;
  176. }
  177. }
  178. }
  179. &__avatar {
  180. &:active,
  181. &:focus,
  182. &:hover {
  183. border: 2px solid var(--color-primary-element-text);
  184. }
  185. }
  186. ul {
  187. display: flex;
  188. flex-direction: column;
  189. gap: 2px;
  190. &:deep {
  191. li {
  192. a,
  193. button {
  194. border-radius: 6px;
  195. display: inline-flex;
  196. align-items: center;
  197. height: var(--header-menu-item-height);
  198. color: var(--color-main-text);
  199. padding: 10px 8px;
  200. box-sizing: border-box;
  201. white-space: nowrap;
  202. position: relative;
  203. width: 100%;
  204. &:hover {
  205. background-color: var(--color-background-hover);
  206. }
  207. &:focus-visible {
  208. background-color: var(--color-background-hover) !important;
  209. box-shadow: inset 0 0 0 2px var(--color-primary-element) !important;
  210. outline: none !important;
  211. }
  212. &:active,
  213. &.active {
  214. background-color: var(--color-primary-element);
  215. color: var(--color-primary-element-text);
  216. }
  217. span {
  218. padding-bottom: 0;
  219. color: var(--color-main-text);
  220. white-space: nowrap;
  221. overflow: hidden;
  222. text-overflow: ellipsis;
  223. max-width: 110px;
  224. }
  225. img {
  226. width: 16px;
  227. height: 16px;
  228. margin-right: 10px;
  229. }
  230. img,
  231. svg {
  232. filter: var(--background-invert-if-dark);
  233. }
  234. &:active,
  235. &.active {
  236. img,
  237. svg {
  238. filter: var(--primary-invert-if-dark);
  239. }
  240. }
  241. }
  242. // Override global button styles
  243. button {
  244. background-color: transparent;
  245. border: none;
  246. font-weight: normal;
  247. margin: 0;
  248. }
  249. }
  250. }
  251. }
  252. }
  253. </style>