All files / src/composables useFlowMemberReference.ts

96% Statements 24/25
83.87% Branches 26/31
83.33% Functions 10/12
95.65% Lines 22/23

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                          89x 89x 89x   89x 2x 1x     2x               89x 2x       132x 39x   93x     89x 51x 51x         51x 52x   1x     1x 1x               89x     1x        
import { computed, onMounted, watch } from 'vue'
import { getActivePinia } from 'pinia'
import { useAuthStore } from '@/stores/auth'
import { useFlowsStore } from '@/stores/flows'
 
export interface ReferenceOption {
  value: string
  label: string
  description?: string
  disabled?: boolean
}
 
export function useFlowMemberReference() {
  const hasPinia = !!getActivePinia()
  const authStore = hasPinia ? useAuthStore() : null
  const flowsStore = hasPinia ? useFlowsStore() : null
 
  const memberOptions = computed<ReferenceOption[]>(() => {
    if (!flowsStore) {
      return []
    }
 
    return (flowsStore.currentFlowMembers || []).map((member) => ({
      value: member.userId,
      label: member.name || member.email || member.userId,
      description: member.email || undefined,
      disabled: member.status !== 'active' && !member.isOwner
    }))
  })
 
  const memberLabelById = computed(() => {
    return new Map(memberOptions.value.map((option) => [option.value, option.label]))
  })
 
  async function ensureMembersLoaded(force = false) {
    if (!authStore || !flowsStore || !authStore.currentFlow) {
      return
    }
    await flowsStore.fetchMembers(authStore.currentFlow, force)
  }
 
  if (hasPinia) {
    onMounted(() => {
      void ensureMembersLoaded().catch(() => {
        // Avoid unhandled rejections when background loading fails.
      })
    })
 
    watch(
      () => authStore?.currentFlow ?? null,
      (flowId, previousFlowId) => {
        Iif (!flowId) {
          return
        }
        Eif (flowId !== previousFlowId) {
          void ensureMembersLoaded().catch(() => {
            // Avoid unhandled rejections when background loading fails.
          })
        }
      }
    )
  }
 
  return {
    memberOptions,
    memberLabelById,
    membersLoading: computed(() => flowsStore?.membersLoading ?? false),
    ensureMembersLoaded
  }
}