All files / src/composables useFlowImagePreload.ts

100% Statements 36/36
92.3% Branches 24/26
100% Functions 9/9
100% Lines 29/29

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                                    5x 5x 5x   5x 4x 4x 3x 2x             5x 4x 4x 3x 2x 2x                         9x 9x   9x   8x         8x 11x   11x 20x           8x 8x 4x       8x 9x   1x 1x          
import { onMounted, watch } from 'vue'
import { getActivePinia } from 'pinia'
import { useAuthStore } from '@/stores/auth'
import { useFlowsStore } from '@/stores/flows'
import { useEntitiesStore } from '@/stores/entities'
import { preloadImageUrl } from '@/composables/useImageUrlCache'
import { hasFlowPermission } from '@/composables/usePermissions'
 
/**
 * Warms the shared image URL cache for a flow: member avatars and entity type
 * picture/icon images. Both sets are bounded per flow (unlike entity items),
 * so eagerly fetching and preloading all of them on flow activation is cheap.
 *
 * Each fetch is skipped when the membership lacks the corresponding `:list`
 * permission, mirroring FlowsSidebar's guards — otherwise every flow
 * activation would fire a guaranteed 403 for members without those rights.
 */
export async function preloadFlowImages(flowId: string): Promise<void> {
  const flowsStore = useFlowsStore()
  const entitiesStore = useEntitiesStore()
  const membership = flowsStore.myFlows.find((f) => f.flowId === flowId)
 
  if (hasFlowPermission(membership, 'members:list')) {
    try {
      const members = await flowsStore.fetchMembers(flowId)
      for (const member of members || []) {
        if (member.profilePicture) preloadImageUrl(member.profilePicture)
      }
    } catch {
      // Best-effort background preload — member list errors surface elsewhere.
    }
  }
 
  if (hasFlowPermission(membership, 'entities:list')) {
    try {
      await entitiesStore.fetchEntities()
      for (const entity of entitiesStore.entities) {
        if (entity.picture) preloadImageUrl(entity.picture)
        if (entity.icon) preloadImageUrl(entity.icon)
      }
    } catch {
      // Best-effort background preload — entity list errors surface elsewhere.
    }
  }
}
 
/**
 * Call once from the app root: preloads the active flow's images on activation
 * and on every flow switch, and the picture/icon of every flow the user belongs to.
 */
export function useFlowImagePreload(): void {
  const hasPinia = !!getActivePinia()
  const authStore = hasPinia ? useAuthStore() : null
 
  if (!hasPinia) return
 
  const flowsStore = useFlowsStore()
 
  // The flows page and the flow switcher show every membership's branding, not
  // just the active flow's. Watching the keys (not the array) also catches
  // in-place changes: updateFlow replaces an element, createFlow pushes one.
  watch(
    () => flowsStore.myFlows.flatMap((flow) => [flow.picture, flow.icon]),
    (keys) => {
      for (const key of keys) {
        if (key) preloadImageUrl(key)
      }
    },
    { immediate: true }
  )
 
  onMounted(() => {
    if (authStore?.currentFlow) {
      void preloadFlowImages(authStore.currentFlow)
    }
  })
 
  watch(
    () => authStore?.currentFlow ?? null,
    (flowId, previousFlowId) => {
      Eif (flowId && flowId !== previousFlowId) {
        void preloadFlowImages(flowId)
      }
    }
  )
}