All files / features / pipelines useRepositoryOptions.ts

90.9% Statements 10/11
46.66% Branches 7/15
100% Functions 6/6
100% Lines 7/7

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        144x 368x 144x     94x 136x       94x       144x                                    
// depcruiser:ignore l3-not-lower — Custom view: repository FK resolution (wireframes/pipeline-detail.md)
import { useEntityList } from '@/layers/L1/entityResolver'
import { useMemo } from 'react'
 
eIxport interface RepositoryOption {
  value: string
  label: string
}
 
function resolveRepoLabel(
  repos: Array<Record<string, unknown>>,
  id: unknown,
): string {
  if (!id) return '\u2014'
  const found = repos.find((r) => r.id === id)
  return found ? String(found.name ?? id) : String(id)
}
 
export function useRepositoryOptions() {
  const list = useEntityList('repositories', {})
  const options = useMemo<RepositoryOption[]>(
    () =>
      ((list.data ?? []) as Array<Record<string, unknown>>).map((r) => ({
        value: String(r.id ?? ''),
        label: String(r.name ?? r.id ?? ''),
      })),
    [list.data],
  )
  return {
    options,
    isLoading: list.isLoading,
    error: list.error,
    byIdFallback: (id: unknown) =>
      resolveRepoLabel((list.data ?? []) as Array<Record<string, unknown>>, id),
  }
}