All files / modules/35-connectors/pages/connectors ConnectorView.tsx

78.95% Statements 45/57
65.79% Branches 50/76
66.67% Functions 6/9
78.95% Lines 45/57

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              10x 10x 10x                               10x   10x   10x 10x 10x 10x 10x 10x 10x 10x 10x                                                       10x 29x   10x           3x   3x     3x                 3x   3x       10x 15x 15x           13x 13x 13x     13x 13x   13x 13x   13x                               13x                 13x                       13x 9x 3x 3x       13x                         13x                 3x                                           3x 3x                                                                                                                 10x  
/*
 * 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, useEffect } from 'react'
import { useParams } from 'react-router-dom'
import {
  Layout,
  Container,
  ButtonVariation,
  VisualYamlSelectedView as SelectedView,
  VisualYamlToggle,
  useToaster
} from '@wings-software/uicore'
import type { ToasterProps } from '@wings-software/uicore/dist/hooks/useToaster/useToaster'
import type {
  ConnectorInfoDTO,
  ConnectorResponse,
  ResponseJsonNode,
  ResponseYamlSnippets,
  ResponsePageSecretResponseWrapper
} from 'services/cd-ng'
import YamlBuilder from '@common/components/YAMLBuilder/YamlBuilder'
import type { YamlBuilderProps } from '@common/interfaces/YAMLBuilderProps'
import useCreateConnectorModal from '@connectors/modals/ConnectorModal/useCreateConnectorModal'
import type { UseGetMockData } from '@common/utils/testUtils'
import { useStrings } from 'framework/strings'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import RbacButton from '@rbac/components/Button/Button'
import { usePermission } from '@rbac/hooks/usePermission'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import ConnectorActivityDetails from '@connectors/components/ConnectorActivityDetails/ConnectorActivityDetails'
import ConnectorYAMLEditor from '@connectors/components/ConnectorYAMLEditor/ConnectorYAMLEditor'
import SavedConnectorDetails from './views/savedDetailsView/SavedConnectorDetails'
import css from './ConnectorView.module.scss'
 
export interface ConnectorViewProps {
  type: ConnectorInfoDTO['type']
  response: ConnectorResponse
  refetchConnector: () => Promise<void>
  mockMetaData?: UseGetMockData<ResponseYamlSnippets>
  mockSnippetData?: UseGetMockData<ResponseJsonNode>
  mockSchemaData?: UseGetMockData<ResponseJsonNode>
  mockSecretData?: UseGetMockData<ResponsePageSecretResponseWrapper>
}
 
interface ConnectorViewState {
  enableEdit: boolean
  setEnableEdit: (val: boolean) => void
  connector: ConnectorInfoDTO
  setConnector: (object: ConnectorInfoDTO) => void
  selectedView: string
  setSelectedView: (selection: SelectedView) => void
}
 
interface ModeSwitchHandlers {
  setSelectedView: (selection: SelectedView) => void
  setConnector: (object: ConnectorInfoDTO) => void
  setConnectorForYaml: (object: ConnectorInfoDTO) => void
  showError: ToasterProps['showError']
}
 
const getInitialConnectorData = (response: ConnectorResponse): ConnectorInfoDTO =>
  response?.connector || ({} as ConnectorInfoDTO)
 
const handleModeSwitch = (
  newView: SelectedView,
  selectedView: SelectedView,
  connector: ConnectorInfoDTO,
  handlers: ModeSwitchHandlers
): boolean => {
  const { setSelectedView, setConnector, setConnectorForYaml, showError } = handlers
 
  Iif (newView === selectedView) {
    return false
  } else {
    Iif (newView === SelectedView.VISUAL) {
      try {
        setSelectedView(newView)
        setConnector(connector)
        setConnectorForYaml(connector)
      } /* istanbul ignore next */ catch (err) {
        showError(err.name ? `${err.name}: ${err.message}` : err)
      }
    } else {
      setSelectedView(SelectedView.YAML)
    }
    return true
  }
}
 
const ConnectorView: React.FC<ConnectorViewProps> = props => {
  const { showError } = useToaster()
  const { accountId, projectIdentifier, orgIdentifier } = useParams<{
    accountId: string
    projectIdentifier: string
    orgIdentifier: string
  }>()
 
  const isEntityInvalid = props.response.entityValidityDetails?.valid === false
  const [enableEdit, setEnableEdit] = useState(false)
  const [selectedView, setSelectedView] = useState<SelectedView>(
    isEntityInvalid ? SelectedView.YAML : SelectedView.VISUAL
  )
  const [connector, setConnector] = useState<ConnectorInfoDTO>(getInitialConnectorData(props.response))
  const [connectorForYaml, setConnectorForYaml] = useState<ConnectorInfoDTO>(getInitialConnectorData(props.response))
 
  const { getString } = useStrings()
  const isHarnessManaged = props.response?.harnessManaged
 
  const [canEditConnector] = usePermission(
    {
      resourceScope: {
        accountIdentifier: accountId,
        orgIdentifier,
        projectIdentifier
      },
      resource: {
        resourceType: ResourceType.CONNECTOR,
        resourceIdentifier: connector.identifier
      },
      permissions: [PermissionIdentifier.UPDATE_CONNECTOR]
    },
    []
  )
 
  const state: ConnectorViewState = {
    enableEdit,
    setEnableEdit,
    connector,
    setConnector,
    selectedView,
    setSelectedView
  }
 
  const { openConnectorModal } = useCreateConnectorModal({
    onSuccess: data => {
      setConnector(data?.connector as ConnectorInfoDTO)
      setConnectorForYaml(data?.connector as ConnectorInfoDTO)
      state.setEnableEdit(false)
    },
    onClose: () => {
      state.setEnableEdit(false)
      props.refetchConnector()
    }
  })
 
  useEffect(() => {
    if (props.response?.connector) {
      setConnector(props.response.connector)
      setConnectorForYaml(props.response.connector)
    }
  }, [props.response])
 
  const yamlBuilderReadOnlyModeProps: YamlBuilderProps = {
    fileName: `${connectorForYaml?.name ?? 'Connector'}.yaml`,
    entityType: 'Connectors',
    ...(!isEntityInvalid
      ? { existingJSON: { connector: connectorForYaml } }
      : { existingYaml: props.response.entityValidityDetails?.invalidYaml }),
    isReadOnlyMode: true,
    height: 'calc(100vh - 350px)',
    yamlSanityConfig: {
      removeEmptyString: false
    }
  }
 
  return (
    <Layout.Horizontal padding="xlarge" height="inherit">
      <Layout.Vertical width={enableEdit && selectedView === SelectedView.YAML ? '100%' : '67%'}>
        <Container className={css.buttonContainer}>
          {state.enableEdit ? null : (
            <VisualYamlToggle
              selectedView={isEntityInvalid ? SelectedView.YAML : selectedView}
              disableToggle={isEntityInvalid}
              onChange={nextMode => {
                handleModeSwitch(nextMode, selectedView, getInitialConnectorData(props.response), {
                  setSelectedView,
                  setConnector,
                  setConnectorForYaml,
                  showError
                })
              }}
            />
          )}
          {state.enableEdit || isHarnessManaged ? null : (
            <RbacButton
              id="editDetailsBtn"
              text={getString('editDetails')}
              icon="edit"
              permission={{
                permission: PermissionIdentifier.UPDATE_CONNECTOR,
                resource: {
                  resourceType: ResourceType.CONNECTOR,
                  resourceIdentifier: connector.identifier
                }
              }}
              onClick={() => {
                state.setEnableEdit(true)
                selectedView === SelectedView.VISUAL
                  ? openConnectorModal(true, props.type, {
                      connectorInfo: connector,
                      gitDetails: { ...props.response?.gitDetails, getDefaultFromOtherRepo: false }
                    })
                  : undefined
              }}
              variation={ButtonVariation.SECONDARY}
            />
          )}
        </Container>
        <Layout.Horizontal height="100%">
          {/* Edit mode */}
          {enableEdit ? (
            selectedView === SelectedView.YAML ? (
              <ConnectorYAMLEditor
                responsedata={props.response}
                connector={connector}
                setConnector={setConnector}
                yamlBuilderProps={yamlBuilderReadOnlyModeProps}
                enableEdit={enableEdit}
                setEnableEdit={setEnableEdit}
                selectedView={selectedView}
                setSelectedView={setSelectedView}
                setConnectorForYaml={setConnectorForYaml}
                refetchConnector={props.refetchConnector}
              ></ConnectorYAMLEditor>
            ) : null
          ) : /* View-only mode */
          selectedView === SelectedView.VISUAL ? (
            <Layout.Horizontal spacing="medium" height="100%" width="100%">
              <SavedConnectorDetails connector={connector}></SavedConnectorDetails>
            </Layout.Horizontal>
          ) : (
            <Layout.Horizontal spacing="medium" className={css.fullWidth}>
              <div className={css.yamlView} data-test="yamlBuilderContainer">
                <YamlBuilder
                  {...yamlBuilderReadOnlyModeProps}
                  showSnippetSection={false}
                  onEnableEditMode={() => state.setEnableEdit(true)}
                  isEditModeSupported={canEditConnector}
                />
              </div>
            </Layout.Horizontal>
          )}
        </Layout.Horizontal>
      </Layout.Vertical>
      {selectedView === SelectedView.VISUAL ? (
        <ConnectorActivityDetails
          responsedata={props.response}
          refetchConnector={props.refetchConnector}
        ></ConnectorActivityDetails>
      ) : null}
    </Layout.Horizontal>
  )
}
 
export default ConnectorView