All files / modules/40-gitsync/modals useCreateGitSyncModal.tsx

82.5% Statements 33/40
57.14% Branches 8/14
50% Functions 4/8
82.5% Lines 33/40

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              5x 5x 5x 5x 5x   5x   5x 5x 5x 5x 5x                                 5x 17x 17x 17x 17x 17x 17x                       17x   17x       17x 5x 1x     1x 1x     5x                                                                                                                                           17x             5x 5x 5x 5x 5x           5x  
/*
 * 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, { useState } from 'react'
import { Button, ButtonVariation, StepWizard } from '@wings-software/uicore'
import { useModalHook } from '@harness/use-modal'
import { Dialog, IDialogProps } from '@blueprintjs/core'
import { useParams } from 'react-router-dom'
import type { GitSyncConfig } from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import GitSyncRepoFormStep from '@gitsync/pages/steps/GitSyncRepoFormStep'
import GitConnection from '@gitsync/components/GitConnection/GitConnection'
import { GitFullSyncStep } from '@gitsync/pages/steps/GitFullSyncStep/GitFullSyncStep'
import GitSyncRepoForm from '../components/gitSyncRepoForm/GitSyncRepoForm'
import css from './useCreateGitSyncModal.module.scss'
 
export interface UseCreateGitSyncModalProps {
  onSuccess?: (data?: GitSyncConfig) => void
  onClose?: () => void
}
 
export interface UseCreateGitSyncModalReturn {
  openGitSyncModal: (
    creatingFirstRepo: boolean,
    isEditMode: boolean,
    syncRepo: GitSyncConfig | undefined,
    modalProps?: IDialogProps
  ) => void
  hideGitSyncModal: () => void
}
 
const useCreateGitSyncModal = (props: UseCreateGitSyncModalProps): UseCreateGitSyncModalReturn => {
  let shouldRefreshOnClose = false
  const { getString } = useStrings()
  const [isEditMode, setIsEditMode] = useState(false)
  const [isNewUser, setIsNewUser] = useState(false)
  const [syncRepo, setSyncRepo] = useState<GitSyncConfig | void>()
  const [modalProps, setModalProps] = useState<IDialogProps>({
    isOpen: true,
    enforceFocus: false,
    style: {
      width: 1200,
      minHeight: 720,
      borderLeft: 0,
      paddingBottom: 0,
      position: 'relative',
      overflow: 'hidden'
    }
  })
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
 
  const handleSuccess = (data?: GitSyncConfig): void => {
    props.onSuccess?.(data)
  }
 
  const [showModal, hideModal] = useModalHook(() => {
    const closeHandler = (): void => {
      Iif (shouldRefreshOnClose) {
        handleSuccess()
      }
      props.onClose?.()
      hideModal()
    }
 
    return isNewUser ? (
      <Dialog {...modalProps}>
        <StepWizard
          stepClassName={css.noPadding}
          title={getString('enableGitExperience')}
          icon="git-landing-page"
          iconProps={{ size: 80, className: css.icon }}
        >
          <GitSyncRepoFormStep
            name={getString('gitsync.configureHarnessFolder')}
            accountId={accountId}
            isEditMode={isEditMode}
            isNewUser={isNewUser}
            gitSyncRepoInfo={undefined}
            orgIdentifier={orgIdentifier}
            projectIdentifier={projectIdentifier}
            onClose={closeHandler}
          />
          <GitConnection
            name={getString('connectors.selectConnectivityMode')}
            onSuccess={() => {
              shouldRefreshOnClose = true
            }}
            isLastStep={false}
          />
          <GitFullSyncStep
            name={getString('gitsync.branchToSync')}
            onClose={closeHandler}
            onSuccess={(data?: GitSyncConfig) => {
              shouldRefreshOnClose = false
              handleSuccess(data)
            }}
          />
        </StepWizard>
        <Button
          minimal
          variation={ButtonVariation.ICON}
          icon="cross"
          iconProps={{ size: 18 }}
          className={css.crossIcon}
          onClick={closeHandler}
        />
      </Dialog>
    ) : (
      <Dialog {...modalProps}>
        <GitSyncRepoForm
          accountId={accountId}
          orgIdentifier={orgIdentifier}
          projectIdentifier={projectIdentifier}
          isEditMode={isEditMode}
          isNewUser={isNewUser}
          gitSyncRepoInfo={undefined}
          onSuccess={(data?: GitSyncConfig) => {
            handleSuccess(data)
            hideModal()
          }}
          onClose={closeHandler}
        />
        <Button
          minimal
          variation={ButtonVariation.ICON}
          icon="cross"
          iconProps={{ size: 18 }}
          className={css.crossIcon}
          onClick={closeHandler}
        />
      </Dialog>
    )
  }, [isEditMode, syncRepo, isNewUser])
 
  return {
    openGitSyncModal: (
      creatingFirstRepo: boolean,
      isEditing: boolean,
      gitSyncRepo: GitSyncConfig | undefined,
      _modalProps?: IDialogProps
    ) => {
      setSyncRepo(gitSyncRepo)
      setIsNewUser(creatingFirstRepo)
      setIsEditMode(isEditing)
      setModalProps(_modalProps || modalProps)
      showModal()
    },
    hideGitSyncModal: hideModal
  }
}
 
export default useCreateGitSyncModal