All files / modules/10-common/components/SvgInline SvgInline.tsx

100% Statements 24/24
90% Branches 9/10
100% Functions 6/6
100% Lines 24/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              2x 2x 2x 2x             2x 66x 66x 66x   66x   66x 18x 18x 18x 18x   18x 18x 18x 18x       66x 33x     33x 15x                   18x     2x  
/*
 * 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, useState } from 'react'
import { Text, Icon, Layout } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
 
interface SvgInlineProps {
  url: string
  className?: string
}
 
const SvgInline: React.FC<SvgInlineProps> = ({ url, className }) => {
  const [svg, setSvg] = useState<string>('')
  const [loading, setLoading] = useState<boolean>(false)
  const [error, setError] = useState<any>(undefined)
 
  const { getString } = useStrings()
 
  useEffect(() => {
    setLoading(true)
    let isMounted = true
    fetch(url)
      .then(res => res.text())
      .then(setSvg)
      .catch(err => isMounted && setError(err))
      .finally(() => isMounted && setLoading(false))
    return () => {
      isMounted = false
    }
  }, [url])
 
  if (loading) {
    return <Icon name="steps-spinner" size={32} color={Color.GREY_600} />
  }
 
  if (error) {
    return (
      <Layout.Horizontal spacing={'small'}>
        <Icon name="error" size={32} color={Color.RED_500} />
        <Text font={{ align: 'center' }} color={Color.RED_500}>
          {error.message || getString('somethingWentWrong')}
        </Text>
      </Layout.Horizontal>
    )
  }
 
  return <div className={className} dangerouslySetInnerHTML={{ __html: svg }} />
}
 
export default SvgInline