1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
<!--
- SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<NcHeaderMenu id="user-menu"
class="account-menu"
is-nav
:aria-label="t('core', 'Settings menu')"
:description="avatarDescription">
<template #trigger>
<!-- The `key` is a hack as NcAvatar does not handle updating the preloaded status on show status change -->
<NcAvatar :key="String(showUserStatus)"
class="account-menu__avatar"
disable-menu
disable-tooltip
:show-user-status="showUserStatus"
:user="currentUserId"
:preloaded-user-status="userStatus" />
</template>
<ul class="account-menu__list">
<AccountMenuProfileEntry :id="profileEntry.id"
:name="profileEntry.name"
:href="profileEntry.href"
:active="profileEntry.active" />
<AccountMenuEntry v-for="entry in otherEntries"
:id="entry.id"
:key="entry.id"
:name="entry.name"
:href="entry.href"
:active="entry.active"
:icon="entry.icon" />
</ul>
</NcHeaderMenu>
</template>
<script lang="ts">
import { getCurrentUser } from '@nextcloud/auth'
import { emit, subscribe } from '@nextcloud/event-bus'
import { loadState } from '@nextcloud/initial-state'
import { t } from '@nextcloud/l10n'
import { generateOcsUrl } from '@nextcloud/router'
import { getCapabilities } from '@nextcloud/capabilities'
import { defineComponent } from 'vue'
import { getAllStatusOptions } from '../../../apps/user_status/src/services/statusOptionsService.js'
import axios from '@nextcloud/axios'
import logger from '../logger.js'
import NcAvatar from '@nextcloud/vue/dist/Components/NcAvatar.js'
import NcHeaderMenu from '@nextcloud/vue/dist/Components/NcHeaderMenu.js'
import AccountMenuProfileEntry from '../components/AccountMenu/AccountMenuProfileEntry.vue'
import AccountMenuEntry from '../components/AccountMenu/AccountMenuEntry.vue'
interface ISettingsNavigationEntry {
/**
* id of the entry, used as HTML ID, for example, "settings"
*/
id: string
/**
* Label of the entry, for example, "Personal Settings"
*/
name: string
/**
* Icon of the entry, for example, "/apps/settings/img/personal.svg"
*/
icon: string
/**
* Type of the entry
*/
type: 'settings'|'link'|'guest'
/**
* Link of the entry, for example, "/settings/user"
*/
href: string
/**
* Whether the entry is active
*/
active: boolean
/**
* Order of the entry
*/
order: number
/**
* Number of unread pf this items
*/
unread: number
/**
* Classes for custom styling
*/
classes: string
}
const USER_DEFINABLE_STATUSES = getAllStatusOptions()
export default defineComponent({
name: 'AccountMenu',
components: {
AccountMenuEntry,
AccountMenuProfileEntry,
NcAvatar,
NcHeaderMenu,
},
setup() {
const settingsNavEntries = loadState<Record<string, ISettingsNavigationEntry>>('core', 'settingsNavEntries', {})
const { profile: profileEntry, ...otherEntries } = settingsNavEntries
return {
currentDisplayName: getCurrentUser()?.displayName ?? getCurrentUser()!.uid,
currentUserId: getCurrentUser()!.uid,
profileEntry,
otherEntries,
t,
}
},
data() {
return {
showUserStatus: false,
userStatus: {
status: null,
icon: null,
message: null,
},
}
},
computed: {
translatedUserStatus() {
return {
...this.userStatus,
status: this.translateStatus(this.userStatus.status),
}
},
avatarDescription() {
const description = [
t('core', 'Avatar of {displayName}', { displayName: this.currentDisplayName }),
...Object.values(this.translatedUserStatus).filter(Boolean),
].join(' — ')
return description
},
},
async created() {
if (!getCapabilities()?.user_status?.enabled) {
return
}
const url = generateOcsUrl('/apps/user_status/api/v1/user_status')
try {
const response = await axios.get(url)
const { status, icon, message } = response.data.ocs.data
this.userStatus = { status, icon, message }
} catch (e) {
logger.error('Failed to load user status')
}
this.showUserStatus = true
},
mounted() {
subscribe('user_status:status.updated', this.handleUserStatusUpdated)
emit('core:user-menu:mounted')
},
methods: {
handleUserStatusUpdated(state) {
if (this.currentUserId === state.userId) {
this.userStatus = {
status: state.status,
icon: state.icon,
message: state.message,
}
}
},
translateStatus(status) {
const statusMap = Object.fromEntries(
USER_DEFINABLE_STATUSES.map(({ type, label }) => [type, label]),
)
if (statusMap[status]) {
return statusMap[status]
}
return status
},
},
})
</script>
<style lang="scss" scoped>
:deep(#header-menu-user-menu) {
padding: 0 !important;
}
.account-menu {
:deep(button) {
// Normally header menus are slightly translucent when not active
// this is generally ok but for the avatar this is weird so fix the opacity
opacity: 1 !important;
// The avatar is just the "icon" of the button
// So we add the focus-visible manually
&:focus-visible {
.account-menu__avatar {
border: var(--border-width-input-focused) solid var(--color-background-plain-text);
}
}
}
// Ensure we do not wast space, as the header menu sets a default width of 350px
:deep(.header-menu__content) {
width: fit-content !important;
}
&__avatar {
&:hover {
// Add hover styles similar to the focus-visible style
border: var(--border-width-input-focused) solid var(--color-background-plain-text);
}
}
&__list {
display: inline-flex;
flex-direction: column;
padding-block: var(--default-grid-baseline) 0;
padding-inline: 0 var(--default-grid-baseline);
> :deep(li) {
box-sizing: border-box;
// basically "fit-content"
flex: 0 1;
}
}
}
</style>
|