All files / modules/40-gitsync/pages/config GitSyncConfigTab.tsx

86.27% Statements 44/51
91.67% Branches 22/24
53.85% Functions 7/13
86% Lines 43/50

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              1x 1x                           1x 1x 1x 1x 1x   1x           1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x     1x 3x 3x 3x 3x 3x 3x 3x 3x   3x                                     3x                     3x                     3x 1x               3x       1x 1x         3x 2x       3x 1x     3x                                                                                                                             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 React, { useCallback, useEffect, useState } from 'react'
import {
  Container,
  Layout,
  Page,
  Button,
  ButtonVariation,
  PageSpinner,
  ExpandingSearchInput,
  Checkbox,
  MultiSelectDropDown,
  SelectOption,
  MultiSelectOption,
  Dialog
} from '@wings-software/uicore'
import { FontVariation, Color } from '@harness/design-system'
import { useParams } from 'react-router-dom'
import { defaultTo } from 'lodash-es'
import debounce from 'p-debounce'
import { useModalHook } from '@harness/use-modal'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import {
  GitFullSyncEntityInfoFilterKeys,
  PageGitFullSyncEntityInfoDTO,
  ResponsePageGitFullSyncEntityInfoDTO,
  useListFullSyncFiles
} from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import { setPageNumber } from '@common/utils/utils'
import { Entities } from '@common/interfaces/GitSyncInterface'
import FullSyncForm from '@gitsync/components/FullSyncForm/FullSyncForm'
import { GitSyncStoreProvider } from 'framework/GitRepoStore/GitSyncStoreContext'
import GitFullSyncEntityList from './GitFullSyncEntityList'
import css from './GitSyncConfigTab.module.scss'
 
const enum SyncStatus {
  QUEUED = 'QUEUED',
  SUCCESS = 'SUCCESS',
  FAILED = 'FAILED',
  OVERRIDDEN = 'OVERRIDDEN'
}
 
const GitSyncConfigTab: React.FC = () => {
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
  const { getString } = useStrings()
  const [fullSyncEntities, setFullSyncEntities] = useState<PageGitFullSyncEntityInfoDTO | undefined>()
  const [searchTerm, setSearchTerm] = useState('')
  const [page, setPage] = useState(0)
  const [showOnlyFailed, setShowOnlyFailed] = useState(false)
  const [debounceWait, setDebounceWait] = useState<number>(0)
  const [selectedEntity, setSelectedEntity] = useState<MultiSelectOption[]>()
 
  const supportedGitEntities: SelectOption[] = [
    {
      label: getString('connectorsLabel'),
      value: defaultTo(Entities.CONNECTORS, '')
    },
    {
      label: getString('pipelines'),
      value: defaultTo(Entities.PIPELINES, '')
    },
    {
      label: getString('inputSetsText'),
      value: defaultTo(Entities.INPUT_SETS, '')
    },
    {
      label: getString('common.templates'),
      value: defaultTo(Entities.TEMPLATE, '')
    }
  ]
 
  const { mutate: getFullSyncFiles, loading } = useListFullSyncFiles({
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier,
      pageIndex: page,
      pageSize: 10,
      searchTerm
    }
  })
 
  const [showModal, hideModal] = useModalHook(
    () => (
      <Dialog isOpen={true} enforceFocus={false} onClose={hideModal} className={css.dialog}>
        <GitSyncStoreProvider>
          <FullSyncForm isNewUser={false} onClose={hideModal} onSuccess={hideModal} />
        </GitSyncStoreProvider>
      </Dialog>
    ),
    []
  )
 
  const fetchFullSyncFiles = (): Promise<ResponsePageGitFullSyncEntityInfoDTO> => {
    return getFullSyncFiles({
      syncStatus: showOnlyFailed ? SyncStatus.FAILED : undefined,
      entityTypes: selectedEntity?.map(
        (option: MultiSelectOption) => option.value
      ) as GitFullSyncEntityInfoFilterKeys['entityTypes']
    })
  }
 
  const debouncedGetFullSyncFiles = useCallback(
    debounceWait
      ? debounce(fetchFullSyncFiles, debounceWait)
      : () => {
          setDebounceWait(500)
          return fetchFullSyncFiles()
        },
    [getFullSyncFiles, showOnlyFailed, selectedEntity]
  )
 
  useEffect(() => {
    setPageNumber({ setPage, page, pageItemsCount: fullSyncEntities?.pageItemCount })
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [fullSyncEntities])
 
  useEffect(() => {
    debouncedGetFullSyncFiles().then(response => setFullSyncEntities(response.data))
  }, [searchTerm, page, showOnlyFailed, selectedEntity, debouncedGetFullSyncFiles])
 
  return (
    <>
      <Page.SubHeader>
        <Layout.Horizontal flex={{ justifyContent: 'space-between' }} width={'100%'}>
          <Layout.Horizontal spacing="medium">
            <Container border padding={{ top: 'xsmall', right: 'small', bottom: 'xsmall', left: 'small' }}>
              <Checkbox
                disabled={loading}
                name="syncFailedFilter"
                label={getString('gitsync.syncFailed')}
                font={{ variation: FontVariation.FORM_LABEL }}
                color={Color.GREY_900}
                onChange={e => {
                  setShowOnlyFailed(!!e.currentTarget.checked)
                }}
              />
            </Container>
            <MultiSelectDropDown
              buttonTestId={'entityTypeFilter'}
              value={selectedEntity}
              width={200}
              items={supportedGitEntities}
              usePortal={true}
              placeholder={getString('common.entityType')}
              className={css.entityTypeFilter}
              onChange={setSelectedEntity}
            />
          </Layout.Horizontal>
          <Layout.Horizontal spacing="medium">
            <ExpandingSearchInput
              alwaysExpanded
              width={250}
              placeholder={getString('search')}
              throttle={200}
              onChange={text => {
                setSearchTerm(text.trim())
                setPage(0)
              }}
            />
            <Button
              variation={ButtonVariation.SECONDARY}
              text={getString('gitsync.resyncButtonText')}
              disabled={loading}
              onClick={() => showModal()}
            />
          </Layout.Horizontal>
        </Layout.Horizontal>
      </Page.SubHeader>
      <Page.Body>
        {loading || debounceWait === 0 ? (
          <PageSpinner />
        ) : fullSyncEntities?.totalItems ? (
          <Container padding="xlarge" height={'100%'}>
            <GitFullSyncEntityList data={fullSyncEntities} gotoPage={(pageNumber: number) => setPage(pageNumber)} />
          </Container>
        ) : (
          <Page.NoDataCard message={getString('noData')} />
        )}
      </Page.Body>
    </>
  )
}
 
export default GitSyncConfigTab