All files / modules/75-ce/components/CreateConnector CreateConnector.tsx

40.74% Statements 33/81
0% Branches 0/48
9.09% Functions 2/22
40.51% Lines 32/79

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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296              3x 3x 3x 3x 3x 3x 3x   3x 3x 3x 3x 3x 3x 3x       3x                                         3x                                                                                   3x 1x 1x 1x           1x                                                       1x                                           1x     3x                                                                                                               3x 2x 2x   2x                 2x                                                         2x                                                                                 2x         3x  
/*
 * 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, useMemo } from 'react'
import { Dialog, IconName, IDialogProps } from '@blueprintjs/core'
import { Button, CardSelect, Carousel, Container, Heading, Icon, Layout, Text } from '@wings-software/uicore'
import { useModalHook } from '@harness/use-modal'
import { Color } from '@harness/design-system'
import useCreateConnectorModal from '@connectors/modals/ConnectorModal/useCreateConnectorModal'
import { Connectors } from '@connectors/constants'
import type { ConnectorInfoDTO } from 'services/cd-ng'
import { useTelemetry } from '@common/hooks/useTelemetry'
import { PAGE_NAMES } from '@ce/TrackingEventsConstants'
import { CE_CONNECTOR_CLICK } from '@connectors/trackingConstants'
import AutoStoppingImage from './images/autoStopping.svg'
import BudgetsImage from './images/budgets-anomalies.svg'
import PerspectiveImage from './images/Perspectives.svg'
import css from './CreateConnector.module.scss'
 
// interface useCreateConnectorProps {}
 
const modalProps: IDialogProps = {
  isOpen: true,
  enforceFocus: false,
  style: {
    width: 860,
    position: 'relative',
    height: 500
  }
}
 
interface CloudProviderListProps {
  onChange?: (selectedProvider: string) => void
  selected?: string
}
 
interface UseCreateConnectorProps {
  portalClassName?: string
  onSuccess?: () => void
  onClose?: () => void
}
 
const CloudProviderList: React.FC<CloudProviderListProps> = ({ onChange, selected }) => {
  const providers = [
    {
      icon: 'service-aws',
      title: 'AWS'
    },
    {
      icon: 'gcp',
      title: 'GCP'
    },
    {
      icon: 'service-azure',
      title: 'Azure'
    },
    {
      icon: 'service-kubernetes',
      title: 'Kubernetes'
    }
  ]
  return (
    <div className={css.cloudProviderListContainer}>
      <CardSelect
        data={providers}
        cornerSelected={true}
        renderItem={item => (
          <div>
            <Icon name={item.icon as IconName} size={26} />
          </div>
        )}
        onChange={value => onChange?.(value.title)}
        selected={providers.find(_p => _p.title === selected)}
        className={css.listContainer}
      ></CardSelect>
      <div className={css.textList}>
        {providers.map(provider => (
          <Text key={provider.title}>{provider.title}</Text>
        ))}
      </div>
    </div>
  )
}
 
export const useCreateConnectorMinimal = (props: UseCreateConnectorProps) => {
  const { portalClassName, onSuccess } = props
  const { trackEvent } = useTelemetry()
  const { openConnectorModal } = useCreateConnectorModal({
    onSuccess: () => {
      onSuccess?.()
    }
  })
 
  const handleConnectorCreation = (selectedProvider: string) => {
    let connectorType
    switch (selectedProvider) {
      case 'AWS':
        connectorType = Connectors.CEAWS
        break
      case 'GCP':
        connectorType = Connectors.CE_GCP
        break
      case 'Azure':
        connectorType = Connectors.CE_AZURE
        break
      case 'Kubernetes':
        connectorType = Connectors.CE_KUBERNETES
        break
    }
 
    if (connectorType) {
      trackEvent(CE_CONNECTOR_CLICK, {
        connectorType: connectorType,
        page: PAGE_NAMES.NO_CONNECTOR_MODAL
      })
      openConnectorModal(false, connectorType, {
        connectorInfo: { orgIdentifier: '', projectIdentifier: '' } as unknown as ConnectorInfoDTO
      })
    }
  }
 
  const [showModal, hideModal] = useModalHook(() => {
    return (
      <Dialog
        isOpen={true}
        style={{ width: 450, padding: 40 }}
        enforceFocus={false}
        {...(portalClassName && { portalClassName, usePortal: true })}
      >
        <Text color={Color.GREY_700} font={{ weight: 'bold', size: 'normal' }} style={{ marginBottom: 10 }}>
          You have not added any connectors yet.
        </Text>
        <Text color={Color.GREY_700} font={{ weight: 'bold', size: 'normal' }} style={{ marginBottom: 20 }}>
          Create one to plug in your data and start exploring Cloud cost Management and everything it has to offer!
        </Text>
        <Text font={{ size: 'normal' }} style={{ marginBottom: 10 }}>
          Choose your cloud Provider
        </Text>
        <CloudProviderList onChange={handleConnectorCreation} />
      </Dialog>
    )
  }, [])
 
  return { openModal: showModal, closeModal: hideModal }
}
 
const FeaturesCarousel = () => {
  const data = useMemo(
    () => [
      {
        title: 'Create Cost perspectives',
        description:
          'Create visualisations of relevant data to specific teams, groups, departments, BUs, LOBs cost-centers etc. This provides relevant data to specific teams for decentralized cost management.',
        ctaLink: '',
        imgSrc: PerspectiveImage
      },
      {
        title: 'Set Budgets and receive Alerts on anomalies and overspend',
        description:
          'Once a perspective is created you can schedule <b>reports</b>, create budgets, configure <b>anomaly alerts</b>, get <b>recommendations</b> to improve save costs for a decentralised cost management.',
        ctaLink: '',
        imgSrc: BudgetsImage
      },
      {
        title: 'Create AutoStopping rules',
        description:
          'AutoStopping Rules dynamically make sure that your non-production workloads are running (and costing you) only when you’re using them, and never when they are idle.',
        ctaLink: '',
        imgSrc: AutoStoppingImage
      }
    ],
    []
  )
 
  const [activeSlide, setActiveSlide] = useState<number>(1)
 
  useEffect(() => {
    const id = setTimeout(() => {
      setActiveSlide(prevActiveSlide => (prevActiveSlide === data.length ? 1 : prevActiveSlide + 1))
    }, 20000)
    return () => {
      clearTimeout(id)
    }
  }, [activeSlide])
 
  return (
    <Carousel defaultSlide={activeSlide} onChange={setActiveSlide} className={css.featuresCarousel}>
      {data.map(item => {
        return (
          <div key={item.title} className={css.featureSlide}>
            <div className={css.title}>{item.title}</div>
            <div className={css.imgContainer}>
              <img src={item.imgSrc} alt={item.title} />
            </div>
            <p dangerouslySetInnerHTML={{ __html: item.description }} />
          </div>
        )
      })}
    </Carousel>
  )
}
 
const useCreateConnector = (props: UseCreateConnectorProps) => {
  const [selectedProvider, setSelectedProvider] = useState<string>()
  const { trackEvent } = useTelemetry()
 
  const { openConnectorModal } = useCreateConnectorModal({
    onSuccess: () => {
      props?.onSuccess?.()
    },
    onClose: () => {
      props?.onClose?.()
    }
  })
 
  const handleConnectorCreation = () => {
    let connectorType
    switch (selectedProvider) {
      case 'AWS':
        connectorType = Connectors.CEAWS
        break
      case 'GCP':
        connectorType = Connectors.CE_GCP
        break
      case 'Azure':
        connectorType = Connectors.CE_AZURE
        break
      case 'Kubernetes':
        connectorType = Connectors.CE_KUBERNETES
        break
    }
 
    if (connectorType) {
      trackEvent(CE_CONNECTOR_CLICK, {
        connectorType: connectorType,
        page: PAGE_NAMES.START_TRIAL_MODAL
      })
 
      openConnectorModal(false, connectorType, {
        connectorInfo: { orgIdentifier: '', projectIdentifier: '' } as unknown as ConnectorInfoDTO
      })
    }
  }
 
  const [showModal, hideModal] = useModalHook(() => {
    return (
      <Dialog {...modalProps} className={css.createConnectorDialog}>
        <Layout.Horizontal style={{ height: '100%' }}>
          <Container className={css.connectorsSection}>
            <Heading>{'Welcome!'}</Heading>
            <Text>Let’s get you started with Cloud Cost Management</Text>
            <Text>
              To begin with, you need to create a Connector that will pull in data from your Cloud provider into CCM
            </Text>
            <section style={{ paddingTop: 15 }}>
              <Text className={css.selectProviderLabel}>Select your Cloud provider</Text>
              <CloudProviderList onChange={setSelectedProvider} selected={selectedProvider} />
            </section>
            <Button
              text={'Next'}
              disabled={!selectedProvider}
              intent="primary"
              onClick={handleConnectorCreation}
              className={css.nextButton}
            />
          </Container>
          <Container className={css.carouselSection}>
            <FeaturesCarousel />
          </Container>
        </Layout.Horizontal>
        <Button
          minimal
          icon="cross"
          iconProps={{ size: 18 }}
          onClick={() => {
            props?.onClose?.()
            hideModal()
          }}
          style={{ position: 'absolute', right: 'var(--spacing-large)', top: 'var(--spacing-large)' }}
          data-testid={'close-instance-modal'}
        />
      </Dialog>
    )
  }, [selectedProvider])
 
  return {
    openModal: showModal
  }
}
 
export default useCreateConnector