All files / modules/72-templates-library/components/TemplateStudio/TemplateStudioSubHeader TemplateStudioSubHeader.tsx

92% Statements 23/25
86.67% Branches 13/15
75% Functions 3/4
91.67% Lines 22/24

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              9x 9x 9x                   9x 9x   9x   9x       9x 9x 9x               9x         18x 18x 18x 18x 18x 18x   18x 13x     13x       18x                             5x                                                                                          
/*
 * 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 from 'react'
import { useParams } from 'react-router-dom'
import {
  Button,
  ButtonVariation,
  Container,
  Icon,
  Layout,
  Text,
  VisualYamlSelectedView as SelectedView,
  VisualYamlToggle
} from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import type { TemplateStudioPathProps } from '@common/interfaces/RouteInterfaces'
import { TemplateContext } from '@templates-library/components/TemplateStudio/TemplateContext/TemplateContext'
import type { GitFilterScope } from '@common/components/GitFilters/GitFilters'
import {
  GetErrorResponse,
  SaveTemplatePopover
} from '@templates-library/components/TemplateStudio/SaveTemplatePopover/SaveTemplatePopover'
import { DefaultNewTemplateId } from 'framework/Templates/templates'
import { TemplateStudioSubHeaderLeftView } from './views/TemplateStudioSubHeaderLeftView/TemplateStudioSubHeaderLeftView'
import css from './TemplateStudioSubHeader.module.scss'
 
export interface TemplateStudioSubHeaderProps {
  onViewChange: (view: SelectedView) => boolean
  getErrors?: () => Promise<GetErrorResponse>
  onGitBranchChange: (selectedFilter: GitFilterScope) => void
}
 
export const TemplateStudioSubHeader: (props: TemplateStudioSubHeaderProps) => JSX.Element = ({
  onViewChange,
  getErrors,
  onGitBranchChange
}) => {
  const { state, fetchTemplate, view, isReadonly } = React.useContext(TemplateContext)
  const { isUpdated, entityValidityDetails } = state
  const { getString } = useStrings()
  const { templateIdentifier } = useParams<TemplateStudioPathProps>()
  const [disableVisualView, setDisableVisualView] = React.useState(entityValidityDetails.valid === false)
  const isYaml = view === SelectedView.YAML
 
  React.useEffect(() => {
    Iif (entityValidityDetails.valid === false) {
      setDisableVisualView(true)
    } else {
      setDisableVisualView(false)
    }
  }, [entityValidityDetails.valid])
 
  return (
    <Container
      className={css.subHeader}
      height={52}
      padding={{ right: 'xlarge', left: 'xlarge' }}
      border={{ bottom: true, color: Color.GREY_200 }}
      background={Color.WHITE}
    >
      <Layout.Horizontal height={'100%'} flex={{ alignItems: 'center', justifyContent: 'space-between' }}>
        <TemplateStudioSubHeaderLeftView onGitBranchChange={onGitBranchChange} />
        <Container>
          <VisualYamlToggle
            className={css.visualYamlToggle}
            selectedView={isYaml || disableVisualView ? SelectedView.YAML : SelectedView.VISUAL}
            onChange={nextMode => {
              onViewChange(nextMode)
            }}
            disableToggle={disableVisualView}
          />
        </Container>
        <Container>
          <Layout.Horizontal spacing={'medium'} flex={{ alignItems: 'center' }}>
            {isReadonly && (
              <Container>
                <Layout.Horizontal spacing={'small'}>
                  <Icon name="eye-open" size={16} color={Color.ORANGE_800} />
                  <Text color={Color.ORANGE_800} font={{ size: 'small' }}>
                    {getString('common.readonlyPermissions')}
                  </Text>
                </Layout.Horizontal>
              </Container>
            )}
            {isUpdated && !isReadonly && (
              <Text color={Color.ORANGE_600} font={{ size: 'small' }} className={css.tagRender}>
                {getString('unsavedChanges')}
              </Text>
            )}
            {!isReadonly && (
              <Container>
                <Layout.Horizontal spacing={'small'} flex={{ alignItems: 'center' }}>
                  <SaveTemplatePopover getErrors={getErrors} />
                  {templateIdentifier !== DefaultNewTemplateId && (
                    <Button
                      disabled={!isUpdated}
                      onClick={() => {
                        fetchTemplate({ forceFetch: true, forceUpdate: true })
                      }}
                      variation={ButtonVariation.SECONDARY}
                      text={getString('common.discard')}
                    />
                  )}
                </Layout.Horizontal>
              </Container>
            )}
          </Layout.Horizontal>
        </Container>
      </Layout.Horizontal>
    </Container>
  )
}