All files / modules/75-cf/pages/onboarding/views TestYourFlagView.tsx

72.34% Statements 34/47
32.14% Branches 9/28
62.5% Functions 5/8
71.11% Lines 32/45

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              1x 1x 1x 1x 1x 1x   1x 1x 1x 1x   1x                     1x 2x 1x 1x 1x           1x 1x                   1x       1x 1x   1x           1x   1x 1x 1x                     1x 1x   1x       1x 1x               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, useMemo, useState, useRef } from 'react'
import { Container, Text, Heading, Layout, Icon } from '@wings-software/uicore'
import { useParams } from 'react-router-dom'
import { Color } from '@harness/design-system'
import { Classes, Switch } from '@blueprintjs/core'
import { String, useStrings } from 'framework/strings'
import type { PlatformEntry } from '@cf/components/LanguageSelection/LanguageSelection'
import routes from '@common/RouteDefinitions'
import { ApiKey, FeatureFlagRequestRequestBody, useGetAllFeatures } from 'services/cf'
import { useToggleFeatureFlag } from '@cf/hooks/useToggleFeatureFlag'
import { TestFlagInfoView } from './TestFlagInfoView'
 
const POLLING_INTERVAL_IN_MS = 3000
 
export interface TestYourFlagViewProps {
  flagInfo: FeatureFlagRequestRequestBody
  language: PlatformEntry
  apiKey: ApiKey
  environmentIdentifier: string | undefined
  testDone: boolean
  setTestDone: React.Dispatch<React.SetStateAction<boolean>>
}
 
export const TestYourFlagView: React.FC<TestYourFlagViewProps> = props => {
  const { flagInfo, environmentIdentifier } = props
  const { projectIdentifier, orgIdentifier, accountId: accountIdentifier } = useParams<Record<string, string>>()
  const { getString } = useStrings()
  const toggleFeatureFlag = useToggleFeatureFlag({
    accountIdentifier,
    orgIdentifier,
    projectIdentifier,
    environmentIdentifier: environmentIdentifier as string
  })
  const queryParams = useMemo(
    () => ({
      accountIdentifier,
      orgIdentifier,
      projectIdentifier,
      environmentIdentifier,
      identifier: flagInfo.identifier,
      metrics: true
    }),
    [projectIdentifier, environmentIdentifier, accountIdentifier, orgIdentifier, flagInfo.identifier]
  )
  const { data, loading, refetch } = useGetAllFeatures({
    lazy: true,
    queryParams
  })
  const [checked, setChecked] = useState(false)
  const timeoutIdRef = useRef<number>()
 
  let link = routes.toCFFeatureFlagsDetail({
    orgIdentifier: orgIdentifier as string,
    projectIdentifier: projectIdentifier as string,
    featureFlagIdentifier: flagInfo.identifier,
    accountId: accountIdentifier
  })
  link = location.hash.startsWith('#/account/') ? '/#' + link : link
 
  useEffect(() => {
    Eif (!props.testDone) {
      const pollingFn = (): void => {
        if (!loading) {
          refetch().finally(() => {
            clearTimeout(timeoutIdRef.current)
            timeoutIdRef.current = window.setTimeout(pollingFn, POLLING_INTERVAL_IN_MS)
          })
        } else {
          clearTimeout(timeoutIdRef.current)
          timeoutIdRef.current = window.setTimeout(pollingFn, POLLING_INTERVAL_IN_MS)
        }
      }
      clearTimeout(timeoutIdRef.current)
      timeoutIdRef.current = window.setTimeout(pollingFn, POLLING_INTERVAL_IN_MS)
 
      return () => clearTimeout(timeoutIdRef.current)
    }
  }, [loading, refetch, props.testDone])
 
  useEffect(() => {
    Iif (!props.testDone && data) {
      if (data.features?.[0].status?.status === 'active') {
        clearTimeout(timeoutIdRef.current)
        props.setTestDone(true)
      }
    }
  }, [props.testDone, data, props])
 
  return (
    <Container height="100%">
      <Container padding="xlarge" width="calc(100% - 765px)" height="calc(100vh - 140px)" style={{ overflow: 'auto' }}>
        <Heading
          level={2}
          style={{
            fontWeight: 600,
            fontSize: '20px',
            lineHeight: '28px',
            color: '#22222A'
          }}
          padding={{ bottom: 'medium' }}
        >
          {getString('cf.onboarding.listenToEvent')}
        </Heading>
        <Text style={{ color: '#22222A' }}>{getString('cf.onboarding.toggleLabel')}</Text>
        <Container
          margin={{ top: 'xlarge', bottom: 'xlarge' }}
          padding={{ top: 'xlarge' }}
          style={{
            border: '1px solid #D9DAE5',
            borderRadius: '8px'
          }}
        >
          <Layout.Horizontal padding={{ left: 'small', bottom: 'large' }}>
            <Switch
              onChange={() => {
                if (checked) {
                  toggleFeatureFlag.off(flagInfo.identifier)
                } else {
                  toggleFeatureFlag.on(flagInfo.identifier)
                }
 
                setChecked(!checked)
              }}
              alignIndicator="right"
              className={Classes.LARGE}
              checked={checked}
            />
            <Container padding={{ left: 'large' }}>
              <Text
                style={{
                  fontWeight: 600,
                  fontSize: '13px',
                  lineHeight: '20px',
                  color: '#0B0B0D',
                  alignSelf: 'baseline'
                }}
              >
                {flagInfo.name}
              </Text>
            </Container>
          </Layout.Horizontal>
          {!props.testDone && (
            <Layout.Horizontal
              padding={{ top: 'large', left: 'large', bottom: 'large' }}
              style={{ alignItems: 'center', background: '#F3F3FA', borderTop: '1px solid #D9DAE5' }}
            >
              <Icon name="steps-spinner" size={24} color={Color.BLUE_500} />
              <Text padding={{ left: 'medium' }} style={{ fontSize: '14px' }}>
                {getString('cf.onboarding.listeningToEvent')}
              </Text>
            </Layout.Horizontal>
          )}
        </Container>
        {props.testDone && (
          <>
            <Text style={{ fontWeight: 600, fontSize: '16px', lineHeight: '24px', color: '#0B0B0D' }}>
              {getString('cf.onboarding.allSet')}
            </Text>
            <Text margin={{ top: 'small' }}>
              <String stringID="cf.onboarding.tryTarget" vars={{ link }} useRichText />
            </Text>
          </>
        )}
      </Container>
      <Container
        padding="xxlarge"
        style={{
          boxShadow: '-8px 0 10px -5px rgb(96 97 112 / 16%)',
          position: 'fixed',
          top: '90px',
          right: '400px',
          bottom: '60px',
          zIndex: 0
        }}
      >
        <Container
          padding="large"
          style={{ borderRadius: '8px', border: '1px solid #D9DAE6' }}
          background={Color.BLACK}
          width={300}
          height={150}
        >
          <Text font={{ mono: true }} color={Color.WHITE}>
            <pre style={{ margin: 0 }}>
              {getString('cf.onboarding.waitForConnect', {
                message: props.testDone ? getString('cf.onboarding.connected') : ''
              })}
            </pre>
          </Text>
        </Container>
        <Text width={300} margin={{ top: 'large' }}>
          {getString('cf.onboarding.behindTheSenes')}
        </Text>
      </Container>
      <TestFlagInfoView />
    </Container>
  )
}