All files / modules/85-cv/hooks/useLogContentHook/views/ExecutionLog ExecutionLog.utils.ts

100% Statements 17/17
88.89% Branches 8/9
100% Functions 6/6
100% Lines 17/17

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 79 80 81 82 83 84 85 86 87 88 89 90                        4x 3x               18x 4x   4x   1x                 1x                 2x                     18x 45x 2x 1x   1x           18x                       18x 45x 4x                  
/*
 * Copyright 2022 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import type { ExpandingSearchInputHandle } from '@harness/uicore'
import type { LogLineData } from '@pipeline/components/LogsContent/LogsState/types'
import type { LogLineData as ExecutionLog, UseActionCreatorReturn } from './ExecutionLog.types'
 
function convertSearchIndices(searchIndices: ExecutionLog['searchIndices']): LogLineData['searchIndices'] | undefined {
  if (searchIndices) {
    return {
      level: searchIndices.logLevel,
      time: searchIndices.createdAt,
      out: searchIndices.log
    }
  }
}
 
export function convertLogDataToLogLineData(data: ExecutionLog): LogLineData {
  const { text, searchIndices } = data
 
  switch (text.logLevel) {
    case 'WARN':
      return {
        text: {
          level: text.logLevel,
          time: text.createdAt,
          out: `\u001b[1;33m\u001b[40m${text.log}\u001b[0m`
        },
        searchIndices: convertSearchIndices(searchIndices)
      }
    case 'ERROR':
      return {
        text: {
          level: `\u001b[1;31m\u001b[40m${text.logLevel}\u001b[0m`,
          time: text.createdAt,
          out: `\u001b[1;31m\u001b[40m${text.log}\u001b[0m`
        },
        searchIndices: convertSearchIndices(searchIndices)
      }
    default:
      return {
        text: {
          level: text.logLevel,
          time: text.createdAt,
          out: text.log
        },
        searchIndices: convertSearchIndices(searchIndices)
      }
  }
}
 
export function handleSearchChange(actions: UseActionCreatorReturn) {
  return (term: string): void => {
    if (term) {
      actions.search(term)
    } else {
      actions.resetSearch()
    }
  }
}
 
/* istanbul ignore next */
export function handleKeyDown(actions: UseActionCreatorReturn) {
  return (e: React.KeyboardEvent<HTMLElement>): void => {
    if (e.key === 'ArrowUp') {
      e.preventDefault()
      actions.goToPrevSearchResult()
    } else if (e.key === 'ArrowDown') {
      e.preventDefault()
      actions.goToNextSearchResult()
    }
  }
}
 
export function getKeyDownListener(searchRef: React.MutableRefObject<ExpandingSearchInputHandle | undefined>) {
  return (e: KeyboardEvent): void => {
    const isMetaKey = navigator.userAgent.includes('Mac') ? e.metaKey : e.ctrlKey
 
    /* istanbul ignore next */
    if (e.key === 'f' && isMetaKey && searchRef.current) {
      e.preventDefault()
      searchRef.current.focus()
    }
  }
}