All files / src/components UserAvatar.vue

85.71% Statements 18/21
80% Branches 8/10
100% Functions 3/3
89.47% Lines 17/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  186x                                     178x                                 178x 178x     180x 180x 173x 173x   7x 7x       7x 7x           1x 1x 1x 1x     179x                                                                  
<template>
  <div class="user-avatar" :style="{ width: `${size}px`, height: `${size}px` }">
    <img v-if="url" :src="url" alt="" class="user-avatar-img" @error="handleError" />
    <font-awesome-icon v-else :icon="fallbackIcon" class="user-avatar-icon text-primary" />
  </div>
</template>
 
<script setup lang="ts">
// A user's profile picture, resolved from its S3 key via the shared
// presigned-URL cache (see useImageUrlCache.ts — same mechanism
// MembersPage.vue already uses), falling back to a generic icon when the
// user has none set or the key fails to resolve.
import { ref, watch } from 'vue'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import {
  resolveImageUrl,
  getCachedImageUrl,
  invalidateImageUrl
} from '@/composables/useImageUrlCache'
 
const props = withDefaults(
  defineProps<{
    pictureKey?: string | null
    size?: number
    fallbackIcon?: string
  }>(),
  {
    pictureKey: null,
    size: 48,
    // Matches SystemItemCard's avatarFallbackIcon convention (Members page)
    // rather than the plain user-circle icon used elsewhere before this
    // component existed — keeps the "no picture set" state visually
    // consistent everywhere, too.
    fallbackIcon: 'fa-solid fa-user'
  }
)
 
const url = ref('')
let retried = false
 
async function load() {
  retried = false
  if (!props.pictureKey) {
    url.value = ''
    return
  }
  const cached = getCachedImageUrl(props.pictureKey)
  Iif (cached !== undefined) {
    url.value = cached
    return
  }
  url.value = ''
  url.value = await resolveImageUrl(props.pictureKey)
}
 
// On img load error: clear the stale cache entry and retry once, mirroring
// ImageField.vue's handleSingleImageError.
function handleError() {
  Iif (retried || !props.pictureKey) return
  retried = true
  invalidateImageUrl(props.pictureKey)
  load()
}
 
watch(() => props.pictureKey, load, { immediate: true })
</script>
 
<style scoped>
/* Same rounded-square shape as SystemItemCard's .system-item-avatar (Members
   page) — that's the one place this already shipped, so it's the reference
   every other avatar display is harmonized against. A fixed-size flex box
   (rather than sizing the <img>/icon directly) is what keeps the fallback
   icon centered: FontAwesome's own stylesheet sizes its SVG via
   `width: var(--fa-width, 1.25em)`, a value derived from font-size that
   doesn't line up with a square photo of the same font-size — centering it
   inside a box of the requested size sidesteps that mismatch entirely
   instead of fighting FontAwesome's own CSS. */
.user-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 0.25rem;
  overflow: hidden;
  flex-shrink: 0;
}
 
.user-avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
 
.user-avatar-icon {
  width: 70%;
  height: 70%;
}
</style>