All files / modules/75-cf/pages/feature-flags-detail FeatureFlagsDetailPage.tsx

88.89% Statements 32/36
85.71% Branches 24/28
40% Functions 2/5
88.57% Lines 31/35

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              1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 6x 5x           5x 5x   5x   5x                       5x         5x   5x 1x     4x 1x                   3x                       3x                       3x                                                         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, { ReactElement, useState } from 'react'
import { Layout, PageError } from '@wings-software/uicore'
import { useParams } from 'react-router-dom'
import { GetFeatureFlagQueryParams, useGetFeatureFlag } from 'services/cf'
import { getErrorMessage } from '@cf/utils/CFUtils'
import { ContainerSpinner } from '@common/components/ContainerSpinner/ContainerSpinner'
import { useStrings } from 'framework/strings'
import { useDocumentTitle } from '@common/hooks/useDocumentTitle'
import FlagActivation from '@cf/components/FlagActivation/FlagActivation'
import FlagActivationDetails from '@cf/components/FlagActivation/FlagActivationDetails'
import useActiveEnvironment from '@cf/hooks/useActiveEnvironment'
import GitSyncActions from '@cf/components/GitSyncActions/GitSyncActions'
import { useGitSync } from '@cf/hooks/useGitSync'
import css from './FeatureFlagsDetailPage.module.scss'
 
const FeatureFlagsDetailPage: React.FC = () => {
  const { getString } = useStrings()
  const [skipLoading, setSkipLoading] = useState(false)
  const {
    orgIdentifier,
    projectIdentifier,
    featureFlagIdentifier,
    accountId: accountIdentifier
  } = useParams<Record<string, string>>()
  const { activeEnvironment: environmentIdentifier } = useActiveEnvironment()
 
  useDocumentTitle(getString('featureFlagsText'))
 
  const queryParams = {
    projectIdentifier,
    environmentIdentifier,
    accountIdentifier,
    orgIdentifier
  } as GetFeatureFlagQueryParams
 
  const {
    data: featureFlag,
    loading,
    error,
    refetch
  } = useGetFeatureFlag({
    identifier: featureFlagIdentifier as string,
    queryParams
  })
 
  const gitSync = useGitSync()
 
  if (loading && !skipLoading) {
    return <ContainerSpinner className={css.spinner} />
  }
 
  if (error) {
    return (
      <PageError
        message={getErrorMessage(error)}
        onClick={() => {
          refetch()
        }}
      />
    )
  }
 
  const refetchFlag = async (): Promise<void> => {
    setSkipLoading(true)
    await refetch({
      queryParams: {
        ...queryParams,
        environmentIdentifier
      }
    }).finally(() => {
      setSkipLoading(false)
    })
  }
 
  const GitSyncActionsComponent = (): ReactElement => (
    <GitSyncActions
      isLoading={gitSync.gitSyncLoading}
      branch={gitSync.gitRepoDetails?.branch || ''}
      repository={gitSync.gitRepoDetails?.repoIdentifier || ''}
      isAutoCommitEnabled={gitSync.isAutoCommitEnabled}
      isGitSyncPaused={gitSync.isGitSyncPaused}
      handleToggleAutoCommit={gitSync.handleAutoCommit}
      handleGitPause={gitSync.handleGitPause}
    />
  )
 
  return (
    <div className={css.pageLayout}>
      <section>
        <Layout.Vertical className={css.flagActivationDetailsLayout}>
          {featureFlag && (
            <FlagActivationDetails
              featureFlag={featureFlag}
              refetchFlag={refetch}
              gitSyncActionsComponent={gitSync?.isGitSyncActionsEnabled ? <GitSyncActionsComponent /> : undefined}
              gitSync={gitSync}
            />
          )}
        </Layout.Vertical>
      </section>
      <section>
        {featureFlag && (
          <FlagActivation
            refetchFlag={refetchFlag}
            refetchFlagLoading={loading}
            gitSync={gitSync}
            projectIdentifier={projectIdentifier as string}
            flagData={featureFlag}
          />
        )}
      </section>
    </div>
  )
}
 
export default FeatureFlagsDetailPage