UserAvatar.vue
963 Bytes
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
<script setup lang="ts">
import { type PropType } from 'vue'
import { type User } from '../types'
const avatarColor = (userName: string) => {
const colors = ['primary', '#FFD43A', '#ADFF00', '#262824', 'danger']
const index = userName.charCodeAt(0) % colors.length
return colors[index]
}
defineProps({
user: {
type: Object as PropType<User>,
required: true,
},
size: {
type: String,
default: 'medium',
},
})
const isUrl = (avatar: string) => {
if (!avatar) return false
return avatar.startsWith('http') || avatar.startsWith('blob:')
}
const fallback = (fullname: string) => {
try {
const [firstName, lastName] = fullname.split(' ')
return `${firstName[0]}${lastName[0]}`
} catch {
return fullname[0]
}
}
</script>
<template>
<VaAvatar
:size="size"
:src="isUrl(user.avatar) ? user.avatar : ''"
:fallback-text="fallback(user.fullname)"
:color="avatarColor(user.fullname)"
/>
</template>