All files / src/composables useFlowMemberReference.ts

96% Statements 24/25
90.62% Branches 29/32
83.33% Functions 10/12
95.83% Lines 23/24

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                          211x 211x 211x   211x 2x 1x     2x                   211x 1x 2x               308x 56x   252x     211x 164x 164x         164x 165x   1x     1x 1x               211x     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.userId,
      description: member.nickname ? `@${member.nickname}` : undefined,
      disabled: member.status !== 'active' && !member.isOwner
    }))
  })
 
  // Combines name and @nickname for read-only display contexts, where the
  // label/description split used by selectors doesn't apply.
  const memberLabelById = computed(() => {
    return new Map(
      memberOptions.value.map((option) => [
        option.value,
        option.description ? `${option.label} (${option.description})` : 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
  }
}