All files / modules/75-ci/pages/get-started-with-ci/InfraProvisioningWizard SelectRepository.tsx

90.48% Statements 38/42
81.25% Branches 26/32
90% Functions 9/10
90.48% Lines 38/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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185              4x 4x 4x   4x                     4x   4x   4x                               4x       3x 3x 3x 3x   3x             3x 1x         3x 3x       3x       3x 1x           3x   3x                           1x 1x 1x                                                           4x 4x   4x 2x 1x       4x 4x         84x 84x 84x                                               4x                 1x         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 React, { useCallback, useEffect, useState } from 'react'
import cx from 'classnames'
import { debounce } from 'lodash-es'
import type { Column, CellProps } from 'react-table'
import {
  Text,
  FontVariation,
  Layout,
  TableV2,
  Container,
  RadioButton,
  Color,
  TextInput,
  FormError
} from '@harness/uicore'
import { useStrings } from 'framework/strings'
 
import { repos } from './mocks/repositories'
 
import css from './InfraProvisioningWizard.module.scss'
 
export interface SelectRepositoryRef {
  repository: Repository
}
 
export type SelectRepositoryForwardRef =
  | ((instance: SelectRepositoryRef | null) => void)
  | React.MutableRefObject<SelectRepositoryRef | null>
  | null
 
interface SelectRepositoryProps {
  selectedRepository?: Repository
  showError?: boolean
}
 
const SelectRepositoryRef = (
  props: SelectRepositoryProps,
  forwardRef: SelectRepositoryForwardRef
): React.ReactElement => {
  const { selectedRepository, showError } = props
  const { getString } = useStrings()
  const [repository, setRepository] = useState<Repository | undefined>(selectedRepository)
  const [, setQuery] = useState<string>()
 
  const debouncedRepositorySearch = useCallback(
    debounce((query: string): void => {
      setQuery(query)
    }, 500),
    []
  )
 
  useEffect(() => {
    Iif (selectedRepository) {
      setRepository(selectedRepository)
    }
  }, [selectedRepository])
 
  useEffect(() => {
    Iif (!forwardRef) {
      return
    }
 
    Iif (typeof forwardRef === 'function') {
      return
    }
 
    if (repository) {
      forwardRef.current = {
        repository: repository
      }
    }
  })
 
  const showValidationErrorForRepositoryNotSelected = showError && !repository?.name
 
  return (
    <Layout.Vertical spacing="small">
      <Text font={{ variation: FontVariation.H4 }}>{getString('ci.getStartedWithCI.selectYourRepo')}</Text>
      <Text font={{ variation: FontVariation.BODY2 }}>{getString('ci.getStartedWithCI.codebaseHelptext')}</Text>
      <Container
        padding={{ top: 'small' }}
        className={cx(css.repositories, { [css.repositoriesWithError]: showValidationErrorForRepositoryNotSelected })}
      >
        <TextInput
          leftIcon="search"
          placeholder={getString('ci.getStartedWithCI.searchRepo')}
          className={css.repositorySearch}
          leftIconProps={{ name: 'search', size: 18, padding: 'xsmall' }}
          onChange={e => {
            const queryText = (e.currentTarget as HTMLInputElement).value?.trim()
            Eif (queryText) {
              debouncedRepositorySearch(queryText)
            }
          }}
        />
        <RepositorySelectionTable repositories={repos} onRowClick={setRepository} />
        {showValidationErrorForRepositoryNotSelected ? (
          <Container padding={{ top: 'xsmall' }}>
            <FormError
              name={'repository'}
              errorMessage={getString('fieldRequired', {
                field: getString('repository')
              })}
            />
          </Container>
        ) : null}
      </Container>
    </Layout.Vertical>
  )
}
 
interface Repository {
  name: string
}
 
interface RepositorySelectionTableProps {
  repositories: Repository[]
  onRowClick: (repo: Repository) => void
}
 
function RepositorySelectionTable({ repositories, onRowClick }: RepositorySelectionTableProps): React.ReactElement {
  const { getString } = useStrings()
  const [selectedRow, setSelectedRow] = useState<Repository | undefined>(undefined)
 
  useEffect(() => {
    if (selectedRow) {
      onRowClick(selectedRow)
    }
  }, [selectedRow])
 
  const columns: Column<Repository>[] = React.useMemo(
    () => [
      {
        accessor: 'name',
        width: '100%',
        Cell: ({ row }: CellProps<Repository>) => {
          const { name: repositoryName } = row.original
          const isRowSelected = repositoryName === selectedRow?.name
          return (
            <Layout.Horizontal
              data-testid={repositoryName}
              className={css.repositoryRow}
              flex={{ justifyContent: 'flex-start' }}
              spacing="small"
            >
              <RadioButton checked={isRowSelected} />
              <Text
                lineClamp={1}
                font={{ variation: FontVariation.BODY2 }}
                color={isRowSelected ? Color.PRIMARY_7 : Color.GREY_900}
              >
                {repositoryName}
              </Text>
            </Layout.Horizontal>
          )
        },
        disableSortBy: true
      }
    ],
    [getString]
  )
 
  return (
    <TableV2<Repository>
      columns={columns}
      data={repositories || []}
      hideHeaders={true}
      minimal={true}
      resizable={false}
      sortable={false}
      className={css.repositoryTable}
      onRowClick={data => setSelectedRow(data)}
    />
  )
}
 
export const SelectRepository = React.forwardRef(SelectRepositoryRef)