All files / modules/40-gitsync/pages/entities GitSyncEntityTab.tsx

76.19% Statements 32/42
50% Branches 25/50
60% Functions 6/10
76.19% Lines 32/42

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              1x 1x                       1x 1x 1x 1x 1x   1x 1x 1x 1x             1x 2x 2x 2x 2x   2x                   2x         2x         2x           2x   2x 2x                               2x 2x                                     2x                                                                   2x                 1x 1x   1x       2x             1x  
/*
 * 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 React, { useState } from 'react'
import {
  Container,
  Collapse,
  IconName,
  Text,
  Layout,
  FormInput,
  Formik,
  FormikForm,
  SelectOption
} from '@wings-software/uicore'
import type { IconProps } from '@wings-software/uicore/dist/icons/Icon'
import { noop } from 'lodash-es'
import { Color } from '@harness/design-system'
import { useParams } from 'react-router-dom'
import { useGitSyncStore } from 'framework/GitRepoStore/GitSyncStoreContext'
import { GitBranchDTO, GitSyncConfig, useGetListOfBranchesWithStatus } from 'services/cd-ng'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { branchSyncStatus } from '@common/components/GitContextForm/GitContextForm'
import { Page } from '@common/exports'
import EntitiesPreview from './EntitiesPreview'
import css from './GitSyncEntityTab.module.scss'
 
interface IEntitiesPreviewWrapper {
  key?: string
  repository: GitSyncConfig
}
 
const EntitiesPreviewWrapper: React.FC<IEntitiesPreviewWrapper> = prop => {
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
  const { repository } = prop
  const [selectedBranch, setSelectedBranch] = useState(repository.branch)
  const [searchTerm, setSearchTerm] = useState('')
 
  const collapseProps = {
    collapsedIcon: 'main-chevron-right' as IconName,
    expandedIcon: 'main-chevron-down' as IconName,
    iconProps: { size: 16 } as IconProps,
    isOpen: false,
    isRemovable: false,
    className: 'collapse',
    collapseHeaderClassName: css.collapseHeader
  }
 
  const GetCollapseHeading = (gitRepo: GitSyncConfig): JSX.Element => {
    const {
      data: branchList,
      loading: loadingBranchList,
      refetch: getListOfBranchesWithStatus
    } = useGetListOfBranchesWithStatus({
      lazy: true,
      debounce: 500
    })
 
    const defaultBranchSelect: SelectOption = {
      label: gitRepo.branch || '',
      value: gitRepo.branch || '',
      icon: { name: 'git-new-branch' }
    }
 
    const [branchSelectOptions, setBranchSelectOptions] = React.useState<SelectOption[]>([defaultBranchSelect])
 
    React.useEffect(() => {
      getListOfBranchesWithStatus({
        queryParams: {
          accountIdentifier: accountId,
          orgIdentifier,
          projectIdentifier,
          yamlGitConfigIdentifier: gitRepo.identifier,
          page: 0,
          size: 20,
          branchSyncStatus: branchSyncStatus.SYNCED,
          searchTerm
        },
        debounce: 300
      })
      // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [searchTerm])
 
    React.useEffect(() => {
      Iif (!loadingBranchList && branchList?.data?.branches?.content?.length) {
        const syncedBranchOption: SelectOption[] = []
 
        branchList.data.branches.content.forEach((branch: GitBranchDTO) => {
          if (branch.branchSyncStatus === branchSyncStatus.SYNCED) {
            syncedBranchOption.push({
              label: branch.branchName || '',
              value: branch.branchName || '',
              icon: { name: 'git-new-branch' }
            })
          }
        })
 
        setBranchSelectOptions(syncedBranchOption)
      }
 
      // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [loadingBranchList])
 
    return (
      <Layout.Horizontal flex className={css.header}>
        <Text font={{ size: 'medium', weight: 'semi-bold' }} margin={{ left: 'small' }} color={Color.PRIMARY_7} inline>
          {gitRepo.name}
        </Text>
 
        <Formik
          initialValues={{
            branch: gitRepo.branch
          }}
          formName="gitSyncEntityForm"
          onSubmit={noop}
        >
          <FormikForm
            onClick={(event: React.MouseEvent<HTMLElement, MouseEvent>) => {
              event.stopPropagation()
            }}
          >
            <FormInput.Select
              name="branch"
              items={branchSelectOptions}
              onQueryChange={(query: string) => setSearchTerm(query)}
              onChange={(branch: SelectOption, event) => {
                setSelectedBranch(branch.value as string)
                event?.preventDefault()
                event?.stopPropagation()
              }}
            />
          </FormikForm>
        </Formik>
      </Layout.Horizontal>
    )
  }
 
  return (
    <Container className={css.collapseFeatures}>
      <Collapse {...collapseProps} heading={GetCollapseHeading(repository)}>
        <EntitiesPreview repo={repository} branch={selectedBranch || ''} />
      </Collapse>
    </Container>
  )
}
 
const GitSyncEntityTab: React.FC = () => {
  const { gitSyncRepos } = useGitSyncStore()
 
  return (
    <Page.Body>
      <Container className={css.bodyContainer}>
        {gitSyncRepos?.map((gitRepo: GitSyncConfig, index: number) => {
          return <EntitiesPreviewWrapper repository={gitRepo} key={(gitRepo?.identifier || '') + index} />
        })}
      </Container>
    </Page.Body>
  )
}
 
export default GitSyncEntityTab