-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserMenu.vue
More file actions
152 lines (142 loc) · 4.42 KB
/
Copy pathUserMenu.vue
File metadata and controls
152 lines (142 loc) · 4.42 KB
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
<!-- User menu -->
<template>
<dropdown
v-if="currentUser && currentUser.username != null"
trigger-id="menu-account"
menu-id="ui-user-menu--account"
trigger-label="Account"
:trigger-icon="accountTriggerIcon"
:items="menuItems"
meta-label="Signed in as"
:meta-value="currentUser?.email || currentUser?.username"
:menu-width="226"
:hide-label-on-mobile="true"
@action="handleMenuAction"
>
<template #trigger>
<button class="relative flex items-center justify-center w-8 h-8 rounded-lg bg-blue-100 text-blue-600 hover:bg-blue-200 transition-colors focus:outline-none" type="button" aria-label="Account profile">
<span class="text-sm font-bold">{{ userInitials }}</span>
</button>
</template>
</dropdown>
</template>
<script lang="ts">
import { computed, markRaw, onBeforeMount, reactive, toRefs, watch } from "vue";
import { useRouter } from 'vue-router'
import {dealUser, getUser, postLogout} from "../services/user";
import { Key, Settings, LogOut, User } from "lucide-vue-next";
import Dropdown from "./Dropdown.vue";
export default {
name: "UserMenu",
components: { Dropdown },
props: {
user: {
type: Object,
default: null
}
},
setup(props) {
const router = useRouter()
const normalizeUser = (input) => {
if (!input) {
return {
username: null,
email: null,
first_name: null,
last_name: null
}
}
if (input.data) {
return dealUser(input)
}
return {
username: input.username ?? null,
email: input.email ?? null,
first_name: input.first_name ?? null,
last_name: input.last_name ?? null
}
}
const state = reactive({
currentUser :{
username: null,
email: null,
first_name: null,
last_name: null,
is_superuser: null
},
})
const logout = async () => {
try {
const res = await postLogout()
if (res.status === 200) {
sessionStorage.clear()
await router.push({ path: '/' })
location.reload()
}
} catch (error) {
console.error('Logout failed:', error)
}
}
const accountTriggerIcon = markRaw(User)
const userInitials = computed(() => {
if (state.currentUser?.first_name) {
return state.currentUser.first_name.charAt(0).toUpperCase()
}
if (state.currentUser?.username) {
return state.currentUser.username.charAt(0).toUpperCase()
}
if (state.currentUser?.email) {
return state.currentUser.email.charAt(0).toUpperCase()
}
return 'A'
})
const menuItems = computed(() => [
{
key: "settings",
label: "Settings",
href: "/account-setting",
icon: markRaw(Settings)
},
{
key: "access-tokens",
label: "Access Tokens",
href: "/access-tokens",
icon: markRaw(Key)
},
{
key: "sign-out",
label: "Sign Out",
actionKey: "logout",
icon: markRaw(LogOut)
}
])
const handleMenuAction = (actionKey) => {
if (actionKey === "logout") {
logout()
}
}
watch(
() => props.user,
(newUser) => {
if (newUser) {
state.currentUser = normalizeUser(newUser)
}
},
{ immediate: true }
)
onBeforeMount(async () => {
if (!props.user) {
const res = await getUser()
state.currentUser = normalizeUser(res)
}
})
return {
...toRefs(state),
accountTriggerIcon,
menuItems,
userInitials,
handleMenuAction
}
}
}
</script>