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

100% Statements 60/60
83.33% Branches 15/18
100% Functions 21/21
100% Lines 57/57

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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137              17x 17x     17x 17x 17x 17x   17x 15x               15x           17x 1x 1x 1x   1x 1x 1x   1x 1x 1x 3x 3x 3x   3x 2x   2x 2x         1x   1x           1x                 1x 1x 1x         3x 3x 3x 3x         1x 1x 1x 1x       17x 26x   15x   1x   1x   3x   1x   4x   1x       17x 38x   15x     4x     1x     1x     1x     1x        
/*
 * 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 produce from 'immer'
import { range } from 'lodash-es'
import type { Dispatch } from 'react'
import type { ExecutionLogDTO } from 'services/cv'
import { formatDatetoLocale } from '@common/utils/dateUtils'
import { escapeStringRegexp, sanitizeHTML } from '@common/utils/StringUtils'
import { Action, ActionType, State, LogLineData, TextKeys, UseActionCreatorReturn } from './ExecutionLog.types'
import { defaultReducerState } from './ExecutionLog.constants'
 
const setExecutionLogs = (state: State, action: Action<ActionType.SetExecutionLogs>): State => {
  const payload: LogLineData[] = action.payload.map(executionLog => ({
    text: {
      logLevel: executionLog.logLevel,
      createdAt: executionLog.createdAt ? formatDatetoLocale(executionLog.createdAt) : '',
      log: executionLog.log
    }
  }))
 
  return {
    ...state,
    data: [...state.data, ...payload]
  }
}
 
const search = (state: State, action: Action<ActionType.Search>): State => {
  const { payload } = action
  const dataKeys: TextKeys[] = ['logLevel', 'createdAt', 'log']
  const searchRegex = new RegExp(escapeStringRegexp(sanitizeHTML(payload)), 'gi')
 
  return produce(state, draft => {
    let prevLinesCount = 0
    const linesWithResults: number[] = []
 
    draft.data = state.data.map(log => {
      const searchIndices: Partial<Record<TextKeys, number[]>> = {}
      dataKeys.forEach(key => {
        const value = log.text[key]
        /* istanbul ignore else */ if (value) {
          const matches = value.match(searchRegex)
 
          if (matches && matches.length) {
            const prevResultsCount = linesWithResults.length
 
            searchIndices[key] = range(prevResultsCount, prevResultsCount + matches.length)
            linesWithResults.push(...Array.from<number>({ length: matches.length }).fill(prevLinesCount))
          }
        }
      })
 
      prevLinesCount++
 
      return {
        ...log,
        searchIndices
      }
    })
 
    draft.searchData = {
      currentIndex: 0,
      text: payload,
      linesWithResults
    }
  })
}
 
function resetSearch(state: State, _action: Action<ActionType.ResetSearch>): State {
  return produce(state, draft => {
    draft.searchData = { currentIndex: 0, text: '', linesWithResults: [] }
    draft.data = state.data.map(({ searchIndices, ...rest }) => rest)
  })
}
 
function goToNextSearchResult(state: State, _action: Action<ActionType.GoToNextSearchResult>): State {
  return produce(state, draft => {
    const index = state.searchData.currentIndex
    const total = draft.searchData.linesWithResults.length
    draft.searchData.currentIndex = total > 0 ? (index + 1) % total : 0
  })
}
 
function goToPrevSearchResult(state: State, _action: Action<ActionType.GoToPrevSearchResult>): State {
  return produce(state, draft => {
    const index = state.searchData.currentIndex
    const total = draft.searchData.linesWithResults.length
    draft.searchData.currentIndex = total > 0 ? (total + index - 1) % total : 0
  })
}
 
export function reducer<T extends ActionType>(state: State, action: Action<T>): State {
  switch (action.type) {
    case ActionType.SetExecutionLogs:
      return setExecutionLogs(state, action as Action<ActionType.SetExecutionLogs>)
    case ActionType.Search:
      return search(state, action as Action<ActionType.Search>)
    case ActionType.ResetSearch:
      return resetSearch(state, action as Action<ActionType.ResetSearch>)
    case ActionType.GoToNextSearchResult:
      return goToNextSearchResult(state, action as Action<ActionType.GoToNextSearchResult>)
    case ActionType.GoToPrevSearchResult:
      return goToPrevSearchResult(state, action as Action<ActionType.GoToPrevSearchResult>)
    case ActionType.ResetExecutionLogs:
      return defaultReducerState
    default:
      return state
  }
}
 
export const useActionCreator = (dispatch: Dispatch<Action<ActionType>>): UseActionCreatorReturn => {
  return {
    setExecutionLogs(payload: ExecutionLogDTO[]) {
      dispatch({ type: ActionType.SetExecutionLogs, payload })
    },
    resetExecutionLogs() {
      dispatch({ type: ActionType.ResetExecutionLogs, payload: '' })
    },
    search(payload: string) {
      dispatch({ type: ActionType.Search, payload })
    },
    resetSearch() {
      dispatch({ type: ActionType.ResetSearch, payload: '' })
    },
    goToNextSearchResult() {
      dispatch({ type: ActionType.GoToNextSearchResult, payload: '' })
    },
    goToPrevSearchResult() {
      dispatch({ type: ActionType.GoToPrevSearchResult, payload: '' })
    }
  }
}