All files / modules/72-templates-library/components/TemplateSelector/TemplateSelectorLeftView TemplateSelectorLeftView.tsx

91.89% Statements 68/74
75.68% Branches 84/111
94.12% Functions 16/17
91.67% Lines 66/72

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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288              21x 21x                       21x 21x 21x 21x   21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x           21x             11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 7x                                       11x 11x 11x 8x     7x   1x           11x 10x             11x 8x                                   11x 1x     1x     11x   14x       14x               11x   7x 14x       7x                 11x             11x 7x     11x 7x     11x 7x 1x       11x                                         1x                                                               2x                                                                                                      
/*
 * 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 React, { useEffect, useState } from 'react'
import {
  Container,
  DropDown,
  ExpandingSearchInput,
  ExpandingSearchInputHandle,
  GridListToggle,
  Layout,
  SelectOption,
  Text,
  Views,
  PageError
} from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { defaultTo, isEmpty } from 'lodash-es'
import { useParams } from 'react-router-dom'
import { Breadcrumbs } from '@common/components/Breadcrumbs/Breadcrumbs'
import type { GitQueryParams, ModulePathParams, ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { TemplateSummaryResponse, useGetTemplateList } from 'services/template-ng'
import { useStrings } from 'framework/strings'
import { PageSpinner } from '@common/components'
import { useMutateAsGet, useQueryParams } from '@common/hooks'
import { TemplateListType } from '@templates-library/pages/TemplatesPage/TemplatesPageUtils'
import TemplatesView from '@templates-library/pages/TemplatesPage/views/TemplatesView'
import { Scope } from '@common/interfaces/SecretsInterface'
import routes from '@common/RouteDefinitions'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import NoResultsView from '@templates-library/pages/TemplatesPage/views/NoResultsView/NoResultsView'
import { usePipelineContext } from '@pipeline/components/PipelineStudio/PipelineContext/PipelineContext'
import { TemplateType } from '@templates-library/utils/templatesUtils'
import factory from '@pipeline/components/PipelineSteps/PipelineStepFactory'
import { stagesCollection } from '@pipeline/components/PipelineStudio/Stages/StagesCollection'
import css from './TemplateSelectorLeftView.module.scss'
 
export interface TemplateSelectorLeftViewProps {
  setTemplate: (template?: TemplateSummaryResponse) => void
}
 
export const TemplateSelectorLeftView: React.FC<TemplateSelectorLeftViewProps> = ({ setTemplate }): JSX.Element => {
  const {
    state: {
      templateView: {
        templateDrawerData: { data }
      }
    }
  } = usePipelineContext()
  const { templateType, selectedChildType, allChildTypes = [], selectedTemplateRef } = data?.selectorData || {}
  const [selectedTemplate, setSelectedTemplate] = useState<TemplateSummaryResponse | undefined>()
  const { getString } = useStrings()
  const [page, setPage] = useState(0)
  const [view, setView] = useState<Views>(Views.GRID)
  const [searchParam, setSearchParam] = useState('')
  const { projectIdentifier, orgIdentifier, accountId, module } = useParams<ProjectPathProps & ModulePathParams>()
  const { repoIdentifier, branch } = useQueryParams<GitQueryParams>()
  const { isGitSyncEnabled } = useAppStore()
  const [childType, setChildType] = React.useState<string | undefined>(selectedChildType)
  const scopeOptions: SelectOption[] = React.useMemo(
    () => [
      {
        value: 'all',
        label: getString('all')
      },
      {
        value: Scope.PROJECT,
        label: getString('projectLabel')
      },
      {
        value: Scope.ORG,
        label: getString('orgLabel')
      },
      {
        value: Scope.ACCOUNT,
        label: getString('account')
      }
    ],
    []
  )
  const [selectedScope, setSelectedScope] = useState<SelectOption>(scopeOptions[0])
  const searchRef = React.useRef<ExpandingSearchInputHandle>({} as ExpandingSearchInputHandle)
  const { orgId, projectId } = React.useMemo(() => {
    switch (selectedScope.value) {
      case 'all':
      case Scope.PROJECT:
        return { orgId: orgIdentifier, projectId: projectIdentifier }
      case Scope.ORG:
        return { orgId: orgIdentifier }
      default:
        return {}
    }
  }, [selectedScope, orgIdentifier])
 
  const body = React.useMemo(() => {
    return {
      filterType: 'Template',
      templateEntityTypes: [templateType],
      childTypes: childType ? [childType] : allChildTypes
    }
  }, [templateType, childType, allChildTypes])
 
  const queryParams = React.useMemo(() => {
    return {
      accountIdentifier: accountId,
      orgIdentifier: orgId,
      projectIdentifier: projectId,
      templateListType: TemplateListType.Stable,
      searchTerm: searchParam,
      page,
      size: 20,
      includeAllTemplatesAvailableAtScope: selectedScope.value === 'all',
      ...(isGitSyncEnabled &&
        (selectedScope.value === Scope.PROJECT || selectedScope.value === 'all') && {
          repoIdentifier: repoIdentifier,
          branch: branch,
          getDefaultFromOtherRepo: true
        })
    }
  }, [accountId, orgId, projectId, searchParam, page, repoIdentifier, branch, selectedScope, isGitSyncEnabled])
 
  const reset = React.useCallback((): void => {
    Iif (searchParam) {
      searchRef.current.clear()
    }
    setChildType(selectedChildType)
  }, [searchParam, searchRef.current, selectedChildType])
 
  const getName = React.useCallback(
    (item: string): string => {
      switch (templateType) {
        case TemplateType.Stage:
          return defaultTo(stagesCollection.getStageAttributes(item, getString)?.name, item)
        case TemplateType.Step:
          return defaultTo(factory.getStepName(item), item)
        default:
          return item
      }
    },
    [templateType]
  )
 
  const dropdownItems = React.useMemo(
    (): SelectOption[] =>
      allChildTypes
        .map(item => ({
          label: getName(item),
          value: item
        }))
        .sort((a, b) => a.label.localeCompare(b.label)),
    [allChildTypes, getName]
  )
 
  const {
    data: templateData,
    refetch: reloadTemplates,
    loading,
    error
  } = useMutateAsGet(useGetTemplateList, {
    body,
    queryParams,
    queryParamStringifyOptions: { arrayFormat: 'comma' },
    debounce: true
  })
 
  useEffect(() => {
    setTemplate(selectedTemplate)
  }, [selectedTemplate])
 
  useEffect(() => {
    setSelectedTemplate(templateData?.data?.content?.find(template => template.identifier === selectedTemplateRef))
  }, [selectedTemplateRef, templateData?.data?.content])
 
  useEffect(() => {
    if (loading) {
      setSelectedTemplate(undefined)
    }
  }, [loading])
 
  return (
    <Container width={762} background={Color.FORM_BG} className={css.container}>
      <Layout.Vertical spacing={'xxlarge'} height={'100%'}>
        <Layout.Vertical spacing={'small'} padding={{ left: 'xxlarge', right: 'xxlarge' }}>
          <Breadcrumbs
            links={[
              {
                url: routes.toTemplates({ accountId, orgIdentifier, projectIdentifier, module }),
                label: getString('common.templates')
              },
              {
                url: '/',
                label: getString('templatesLibrary.templatesLabel', { entity: templateType })
              }
            ]}
          />
          <Container>
            <Layout.Horizontal spacing={'small'}>
              <DropDown
                items={scopeOptions}
                value={selectedScope.value.toString()}
                onChange={item => setSelectedScope(item)}
                filterable={false}
              />
              <ExpandingSearchInput
                alwaysExpanded
                className={css.searchBox}
                onChange={(text: string) => {
                  setPage(0)
                  setSearchParam(text)
                }}
                ref={searchRef}
                defaultValue={searchParam}
              />
            </Layout.Horizontal>
          </Container>
        </Layout.Vertical>
        <Container
          height={'100%'}
          style={{ overflow: 'auto', position: 'relative' }}
          padding={{ left: 'xxlarge', right: 'xxlarge' }}
        >
          <Layout.Vertical height={'100%'}>
            <Container>
              <Layout.Horizontal flex={{ alignItems: 'center', justifyContent: 'space-between' }}>
                <Text font={{ size: 'xsmall', weight: 'bold' }} color={Color.GREY_800}>
                  {getString('common.templates').toUpperCase()} ({templateData?.data?.totalElements})
                </Text>
                <Container>
                  <Layout.Horizontal flex={{ alignItems: 'center' }} spacing={'medium'}>
                    {!isEmpty(dropdownItems) && (
                      <DropDown
                        onChange={item => {
                          setChildType(item.value.toString())
                        }}
                        items={dropdownItems}
                        addClearBtn={true}
                        filterable={false}
                        placeholder={`${getString('typeLabel')}: ${getString('all')}`}
                        value={childType}
                      />
                    )}
                    <Container>
                      <GridListToggle initialSelectedView={Views.GRID} onViewToggle={setView} />
                    </Container>
                  </Layout.Horizontal>
                </Container>
              </Layout.Horizontal>
            </Container>
            {loading && <PageSpinner />}
            {!loading && error && (
              <PageError message={defaultTo((error.data as Error)?.message, error.message)} onClick={reloadTemplates} />
            )}
            {!loading && !error && !templateData?.data?.content?.length && (
              <NoResultsView
                hasSearchParam={!!searchParam || !!childType}
                onReset={reset}
                text={
                  selectedScope.value === 'all'
                    ? `There are no templates`
                    : getString('templatesLibrary.templatesPage.noTemplates', {
                        scope: selectedScope.label.toLowerCase()
                      })
                }
                minimal={true}
              />
            )}
            {!loading && !error && !!templateData?.data?.content?.length && (
              <Container style={{ flexGrow: 1 }}>
                <TemplatesView
                  data={templateData?.data}
                  gotoPage={setPage}
                  onSelect={setSelectedTemplate}
                  selectedIdentifier={selectedTemplate?.identifier}
                  view={view}
                />
              </Container>
            )}
          </Layout.Vertical>
        </Container>
      </Layout.Vertical>
    </Container>
  )
}