All files / modules/72-templates-library/pages/TemplatesPage TemplatesPageUtils.tsx

87.18% Statements 34/39
67.5% Branches 27/40
100% Functions 5/5
86.49% Lines 32/37

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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164                53x   53x   53x 53x     53x                                                                                     53x                                                                                     53x 53x 53x     53x 53x 53x 53x     53x 53x 53x 53x 53x 53x     53x       4x 4x 4x   4x               53x         78x 78x       78x   78x 56x   56x             22x        
/*
 * Copyright 2021 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 { IconName } from '@wings-software/uicore'
import { defaultTo, get } from 'lodash-es'
import type { UseStringsReturn } from 'framework/strings'
import { TemplateType } from '@templates-library/utils/templatesUtils'
import type { TemplateSummaryResponse } from 'services/template-ng'
import factory from '@pipeline/components/PipelineSteps/PipelineStepFactory'
import { stagesCollection } from '@pipeline/components/PipelineStudio/Stages/StagesCollection'
import type { NGTemplateInfoConfigWithGitDetails } from 'framework/Templates/TemplateConfigModal/TemplateConfigModal'
 
export const templateColorStyleMap: { [keyof in TemplateType]: React.CSSProperties } = {
  [TemplateType.Step]: {
    color: '#592BAA',
    stroke: '#E1D0FF',
    fill: '#EADEFF'
  },
  [TemplateType.Stage]: {
    color: '#06B7C3',
    stroke: '#C0FBFE',
    fill: '#D3FCFE'
  },
  [TemplateType.Pipeline]: {
    color: '#004BA4',
    stroke: '#CCCBFF',
    fill: '#E8E8FF'
  },
  [TemplateType.Service]: {
    color: '#299B2C',
    stroke: '#D4E7D1',
    fill: '#E4F7E1'
  },
  [TemplateType.StepGroup]: {
    color: '#299B2C',
    stroke: '#D4E7D1',
    fill: '#E4F7E1'
  },
  [TemplateType.Execution]: {
    color: '#299B2C',
    stroke: '#D4E7D1',
    fill: '#E4F7E1'
  },
  [TemplateType.Infrastructure]: {
    color: '#299B2C',
    stroke: '#D4E7D1',
    fill: '#E4F7E1'
  },
  [TemplateType.MonitoredService]: {
    color: '#06B7C3',
    stroke: '#D4E7D1',
    fill: '#E4F7E1'
  }
}
 
export const templateStudioColorStyleMap: { [keyof in TemplateType]: React.CSSProperties } = {
  [TemplateType.Step]: {
    color: '#EADEFF',
    stroke: '#6938C0',
    fill: '#7D4DD3'
  },
  [TemplateType.Stage]: {
    color: '#E8E8FF',
    stroke: '#03C0CD',
    fill: '#0BC8D6'
  },
  [TemplateType.Service]: {
    color: '#299B2C',
    stroke: '#D4E7D1',
    fill: '#E4F7E1'
  },
  [TemplateType.Pipeline]: {
    color: '#E8E8FF',
    stroke: '#5452F6',
    fill: '#6563F0'
  },
  [TemplateType.StepGroup]: {
    color: '#299B2C',
    stroke: '#D4E7D1',
    fill: '#E4F7E1'
  },
  [TemplateType.Execution]: {
    color: '#299B2C',
    stroke: '#D4E7D1',
    fill: '#E4F7E1'
  },
  [TemplateType.Infrastructure]: {
    color: '#299B2C',
    stroke: '#D4E7D1',
    fill: '#E4F7E1'
  },
  [TemplateType.MonitoredService]: {
    color: '#06B7C3',
    stroke: '#D4E7D1',
    fill: '#E4F7E1'
  }
}
 
export enum Sort {
  DESC = 'DESC',
  ASC = 'ASC'
}
 
export enum TemplateListType {
  Stable = 'Stable',
  LastUpdated = 'LastUpdated',
  All = 'All'
}
 
export enum SortFields {
  LastUpdatedAt = 'lastUpdatedAt',
  RecentActivity = 'executionSummaryInfo.lastExecutionTs',
  AZ09 = 'AZ09',
  ZA90 = 'ZA90',
  Name = 'name'
}
 
export const getTypeForTemplate = (
  template: TemplateSummaryResponse,
  getString: UseStringsReturn['getString']
): string => {
  const entityType = template.templateEntityType as TemplateType
  const type = defaultTo(template.childType, '')
  switch (entityType) {
    case TemplateType.Step:
      return defaultTo(factory.getStepName(type), '')
    case TemplateType.Stage:
      return defaultTo(stagesCollection.getStageAttributes(type, getString)?.name, '')
    default:
      return ''
  }
}
 
export const getIconForTemplate = (
  getString: UseStringsReturn['getString'],
  template?: NGTemplateInfoConfigWithGitDetails | TemplateSummaryResponse
): IconName | undefined => {
  const templateTye =
    (template as TemplateSummaryResponse)?.templateEntityType || (template as NGTemplateInfoConfigWithGitDetails)?.type
  Iif (templateTye === TemplateType.Pipeline) {
    return 'pipeline'
  } else {
    const childType =
      (template as TemplateSummaryResponse)?.childType ||
      get(template as NGTemplateInfoConfigWithGitDetails, 'spec.type')
    if (childType) {
      switch (templateTye) {
        case TemplateType.Step:
          return factory.getStepIcon(childType)
        case TemplateType.Stage:
          return stagesCollection.getStageAttributes(childType, getString)?.icon
        default:
          return undefined
      }
    } else {
      return undefined
    }
  }
}