Skip to content

Commit bcfefc7

Browse files
committed
feat: improve avatar component
1 parent 6762a48 commit bcfefc7

3 files changed

Lines changed: 32 additions & 11 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 6762a4855ff402087e325693781a24f8485fabcd

app/components/matrix/avatar.vue

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<script lang="ts">
2-
import type { Room } from 'matrix-js-sdk'
32
import type { ImgProps } from '~/components/img.vue'
43
54
export type MatrixAvatarProps = Omit<ImgProps, 'src' | 'alt'> & {
65
square?: boolean
7-
room?: Room | undefined
8-
user: MaybeUserOrId | undefined | null
96
imageSize?: AvatarImageSize
10-
}
7+
} & (
8+
| { room: MaybeRoomOrId | undefined | null, user?: never, src?: never }
9+
| { user: MaybeUserOrId | undefined | null, room?: never, src?: never }
10+
| { src: string | undefined | null, room?: never, user?: never }
11+
)
1112
</script>
1213
1314
<script setup lang="ts">
@@ -18,16 +19,32 @@ const props = withDefaults(
1819
},
1920
)
2021
21-
const userProfile = useUserProfile(() => typeof props.user === 'string' ? props.user : props.user?.userId)
22-
const resolvedAvatar = useResolveAvatarUrl(() => userProfile.value?.avatar_url, { size: props.imageSize })
22+
const room = useRoom(() => props.room ?? undefined)
23+
const userProfile = useUserProfile(() => props.user ?? undefined)
24+
25+
const src = computed(() => {
26+
if (props.src) return props.src
27+
28+
if (room.value)
29+
return room.value.getMxcAvatarUrl() ?? undefined
30+
31+
if (userProfile.value)
32+
return userProfile.value.avatar_url
33+
34+
return undefined
35+
})
36+
37+
const resolvedAvatar = useResolveAvatarUrl(src, { size: props.imageSize })
2338
2439
const isError = ref(false)
40+
41+
const delegatedProps = reactiveOmit(props, 'room', 'user', 'src', 'square', 'imageSize')
2542
</script>
2643

2744
<template>
2845
<Img
2946
v-if="resolvedAvatar"
30-
v-bind="props"
47+
v-bind="delegatedProps"
3148
:key="resolvedAvatar"
3249
data-slot="avatar"
3350
:alt="userProfile?.displayname ? `${userProfile?.displayname}'s avatar` : 'Avatar'"

app/composables/use-user-profile.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { IMatrixProfile, User } from 'matrix-js-sdk'
22
import type { EffectScope, ShallowRef } from 'vue'
3-
import { toRef } from '@vueuse/core'
43

54
interface Entry {
65
scope: EffectScope
@@ -74,11 +73,15 @@ function release(key: string) {
7473
}
7574
}
7675

77-
export function useUserProfile(userId: MaybeRefOrGetter<string | undefined>) {
78-
const userIdRef = toRef(userId)
76+
export function useUserProfile(user: MaybeRefOrGetter<MaybeUserOrId | undefined>) {
77+
const userRef = computed(() => {
78+
const u = toValue(user)
79+
if (u)
80+
return resolveUserId(u)
81+
})
7982
const current = shallowRef<Entry | undefined>(undefined)
8083

81-
watch(userIdRef, (key, _, onCleanup) => {
84+
watch(userRef, (key, _, onCleanup) => {
8285
if (!key) {
8386
current.value = undefined
8487
return

0 commit comments

Comments
 (0)