All files / modules/75-cf/pages/home CFHomePage.tsx

87.32% Statements 62/71
70.89% Branches 56/79
62.5% Functions 5/8
89.71% Lines 61/68

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              1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x   1x 1x 1x 1x 1x 1x 1x 1x   1x   1x 6x 6x 6x 6x 6x 6x 6x   6x 6x             6x                     6x           6x   6x                             6x               6x   6x   6x 6x           6x             6x   6x 6x       6x 6x       6x 6x       6x   6x       6x 1x     5x       5x   5x                 5x 1x                 4x                     4x                       1x  
/*
 * 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, { useEffect } from 'react'
import { useParams, useHistory } from 'react-router-dom'
import { pick } from 'lodash-es'
import { PageError } from '@wings-software/uicore'
import { ContainerSpinner } from '@common/components/ContainerSpinner/ContainerSpinner'
import { useStrings } from 'framework/strings'
import { HomePageTemplate } from '@projects-orgs/pages/HomePageTemplate/HomePageTemplate'
import { TrialInProgressTemplate } from '@rbac/components/TrialHomePageTemplate/TrialInProgressTemplate'
import { ModuleName } from 'framework/types/ModuleName'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import { useLicenseStore, handleUpdateLicenseStore } from 'framework/LicenseStore/LicenseStoreContext'
import { useProjectModal } from '@projects-orgs/modals/ProjectModal/useProjectModal'
import type { Project } from 'services/cd-ng'
import routes from '@common/RouteDefinitions'
import type { AccountPathProps, Module } from '@common/interfaces/RouteInterfaces'
import { useQueryParams } from '@common/hooks'
import { useGetLicensesAndSummary, useGetProjectList } from 'services/cd-ng'
import { useFeatureFlags } from '@common/hooks/useFeatureFlag'
import { ModuleLicenseType } from '@common/constants/SubscriptionTypes'
import { useFFTrialModal } from '@cf/modals/FFTrial/useFFTrialModal'
import { TrialType } from '@pipeline/components/TrialModalTemplate/trialModalUtils'
import { useGetProjectCreateSuccessHandler, useGetSignUpHandler } from './cfHomeUtils'
import bgImageURL from './ff.svg'
 
import css from './CFHomePage.module.scss'
 
const CFHomePage: React.FC = () => {
  const { getString } = useStrings()
  const { accountId: accountIdentifier } = useParams<AccountPathProps>()
  const { currentUserInfo, selectedProject } = useAppStore()
  const { NG_LICENSES_ENABLED } = useFeatureFlags()
  const { licenseInformation, updateLicenseStore } = useLicenseStore()
  const moduleType = ModuleName.CF
  const module = moduleType.toLowerCase() as Module
 
  const { accounts } = currentUserInfo
  const createdFromNG = accounts?.find(account => account.uuid === accountIdentifier)?.createdFromNG
 
  const {
    data: licenseData,
    error: licenseError,
    refetch: refetchLicense,
    loading: gettingLicense
  } = useGetLicensesAndSummary({
    queryParams: { moduleType },
    accountIdentifier
  })
 
  // get project lists via accountId
  const {
    data: projectListData,
    loading: gettingProjects,
    error: projectsErr,
    refetch: refetchProject
  } = useGetProjectList({
    queryParams: {
      accountIdentifier,
      pageSize: 1
    }
  })
  const projectsExist = !!projectListData?.data?.content?.length
 
  const { openProjectModal, closeProjectModal } = useProjectModal({
    onWizardComplete: (projectData?: Project) => {
      closeProjectModal()
      if (projectData) {
        history.push({
          pathname: routes.toCFOnboarding({
            orgIdentifier: projectData?.orgIdentifier || '',
            projectIdentifier: projectData.identifier,
            accountId: accountIdentifier
          })
        })
      }
    }
  })
 
  const trialInProgressProps = {
    description: getString('common.trialInProgressDescription'),
    startBtn: {
      description: getString('createProject'),
      onClick: openProjectModal
    }
  }
 
  const { experience, modal } = useQueryParams<{ experience?: ModuleLicenseType; modal?: ModuleLicenseType }>()
 
  const history = useHistory()
 
  const expiryTime = licenseData?.data?.maxExpiryTime
  const updatedLicenseInfo = licenseData?.data && {
    ...licenseInformation?.[moduleType],
    ...pick(licenseData?.data, ['licenseType', 'edition']),
    expiryTime
  }
 
  const { openTrialModal } = useFFTrialModal({
    actionProps: {
      onCreateProject: openProjectModal
    },
    trialType: TrialType.CREATE_OR_SELECT_PROJECT
  })
 
  const signUpHandler = useGetSignUpHandler({ gettingProjects, projectsExist, openProjectModal, openTrialModal })
 
  useEffect(() => {
    signUpHandler()
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [modal, gettingProjects, selectedProject, projectsExist])
 
  useEffect(() => {
    handleUpdateLicenseStore({ ...licenseInformation }, updateLicenseStore, module, updatedLicenseInfo)
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [licenseData])
 
  useEffect(() => {
    refetchLicense()
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [experience])
 
  const projectCreateSuccessHandler = useGetProjectCreateSuccessHandler()
 
  Iif (gettingLicense || gettingProjects) {
    return <ContainerSpinner className={css.homepageSpinner} />
  }
 
  if (licenseError) {
    return <PageError message={licenseError.message} onClick={() => refetchLicense()} />
  }
 
  Iif (projectsErr) {
    return <PageError message={projectsErr.message} onClick={() => refetchProject()} />
  }
 
  const showTrialPages = createdFromNG || NG_LICENSES_ENABLED
 
  Iif (showTrialPages && licenseData?.status === 'SUCCESS' && !licenseData.data) {
    history.push(
      routes.toModuleTrialHome({
        accountId: accountIdentifier,
        module
      })
    )
  }
 
  if (showTrialPages && experience === ModuleLicenseType.TRIAL) {
    return (
      <TrialInProgressTemplate
        title={getString('cf.continuous')}
        bgImageUrl={bgImageURL}
        trialInProgressProps={trialInProgressProps}
      />
    )
  }
 
  Iif (selectedProject && !experience) {
    history.push(
      routes.toCFFeatureFlags({
        projectIdentifier: selectedProject.identifier,
        orgIdentifier: selectedProject.orgIdentifier || '',
        accountId: accountIdentifier
      })
    )
  }
 
  // by default will return Home Page
  return (
    <HomePageTemplate
      title={getString('cf.continuous')}
      bgImageUrl={bgImageURL}
      projectCreateSuccessHandler={projectCreateSuccessHandler}
      subTitle={getString('cf.homepage.slogan')}
      documentText={getString('cf.homepage.learnMore')}
      documentURL="https://ngdocs.harness.io/article/0a2u2ppp8s-getting-started-with-continuous-features"
    />
  )
}
 
export default CFHomePage