All files / src/composables useFlowLink.ts

100% Statements 14/14
100% Branches 11/11
100% Functions 6/6
100% Lines 10/10

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                          400x 400x   400x 177x         19x               1023x 314x 314x 259x     400x    
import { computed } from 'vue'
import type { RouteLocationNamedRaw } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { useFlowsStore } from '@/stores/flows'
import { flowlessRouteOf, flowRoute, type FlowRouteName } from '@/router/flowRoutes'
 
/**
 * Links to the pages of the tab's active flow, from anywhere — including the
 * global pages (Notifications, Messages...), whose URL names no flow to
 * inherit. Without an active flow, the Catalog and its entities open
 * outside any flow, and other links lead to the flows list.
 */
export function useFlowLink() {
  const authStore = useAuthStore()
  const flowsStore = useFlowsStore()
 
  const currentFlowSlug = computed(
    () => flowsStore.myFlows.find((flow) => flow.flowId === authStore.currentFlow)?.flowSlug ?? null
  )
 
  /** The slug of one of the user's flows, by id */
  function slugOf(flowId: string | null | undefined): string | null {
    return flowsStore.myFlows.find((flow) => flow.flowId === flowId)?.flowSlug ?? null
  }
 
  function flowLink(
    name: FlowRouteName,
    params: Record<string, string> = {},
    query?: RouteLocationNamedRaw['query']
  ): RouteLocationNamedRaw {
    if (currentFlowSlug.value) return flowRoute(currentFlowSlug.value, name, params, query)
    const flowless = flowlessRouteOf(name)
    if (!flowless) return { name: 'flows' }
    return { name: flowless, params, ...(query ? { query } : {}) }
  }
 
  return { currentFlowSlug, slugOf, flowLink }
}