All files / modules/10-common/hooks useModuleInfo.ts

100% Statements 12/12
100% Branches 2/2
100% Functions 3/3
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 27743x 743x 743x     743x                     743x 743x   743x 3119x   3119x   3119x    
import { useMemo } from 'react'
import { useLocation } from 'react-router-dom'
import { match } from 'path-to-regexp'
 
import type { Module } from 'framework/types/ModuleName'
import { withAccountId } from '@common/utils/routeUtils'
 
export interface ModuleInfo {
  module?: Module
  accountId: string
}
 
export interface UseModuleInfoReturn {
  module?: Module
}
 
const pathToMatch = withAccountId(() => '/:module(ci|cd|cf|cv|ce)')({ accountId: ':accountId' })
const matchModuleFn = match<ModuleInfo>(pathToMatch, { end: false })
 
export function useModuleInfo(): UseModuleInfoReturn {
  const { pathname } = useLocation()
 
  const matchModule = useMemo(() => matchModuleFn(pathname), [pathname])
 
  return { module: matchModule === false ? undefined : matchModule.params.module }
}