1+ import { PathHelper } from '@sensenet/client-utils'
12import React , { memo , useEffect , useState } from 'react'
23import { IconOptions } from './Icon'
34
@@ -9,56 +10,56 @@ type IconFromPathProps = {
910}
1011
1112const IconFromPathComponent = ( { path, options, contentPath, contentType } : IconFromPathProps ) => {
12- const [ iconUrlOrSvg , setIconUrlOrSvg ] = useState < string | null > ( null )
13- const [ isInlineSvg , setIsInlineSvg ] = useState ( false )
13+ const [ icon , setIcon ] = useState < string | null > ( null )
1414
1515 useEffect ( ( ) => {
1616 const loadIcon = async ( ) => {
17- const fileName = path . split ( '/' ) . pop ( ) || ''
18- let svgPath = `/icons/${ fileName } `
17+ let svgPath = path
1918
2019 if ( contentType . toLowerCase ( ) . endsWith ( 'file' ) ) {
2120 if ( contentPath . toLowerCase ( ) . endsWith ( '.csv' ) ) {
2221 svgPath = '/icons/csv.svg'
2322 } else if ( contentPath . toLowerCase ( ) . endsWith ( '.svg' ) ) {
2423 svgPath = '/icons/file_img.svg'
2524 }
25+ } else {
26+ const fileName = path . split ( '/' ) . pop ( )
27+ if ( fileName ) {
28+ svgPath = `/icons/${ fileName } `
29+ }
2630 }
2731
28- // For SVGs, fetch and inline them
2932 if ( svgPath . endsWith ( '.svg' ) ) {
3033 try {
31- const response = await fetch ( svgPath , {
32- cache : 'no-store' ,
33- } )
34+ const response = await options . repo . fetch ( svgPath , { cache : 'force-cache' } )
3435 if ( ! response . ok ) return
36+
3537 const svgText = await response . text ( )
3638 const resizedSvg = svgText
3739 . replace ( 'width=' , 'width="24px" oldwidth=' )
3840 . replace ( 'height=' , 'height="24px" oldheight=' )
3941
40- setIsInlineSvg ( true )
41- setIconUrlOrSvg ( resizedSvg )
42+ options . repo . iconCache . set ( path , resizedSvg )
43+ setIcon ( resizedSvg )
4244 } catch ( e ) {
43- console . warn ( 'Failed to load SVG:' , e )
45+ console . warn ( 'Failed to load SVG icon :' , e )
4446 }
4547 return
4648 }
4749
48- // For non-SVG fallback
49- setIconUrlOrSvg ( svgPath )
50- setIsInlineSvg ( false )
50+ setIcon ( svgPath )
51+ options . repo . iconCache . set ( path , svgPath )
5152 }
5253
5354 loadIcon ( )
54- } , [ path , contentPath , contentType ] )
55+ } , [ path , contentPath , contentType , options . repo ] )
5556
56- if ( ! iconUrlOrSvg ) return null
57+ if ( ! icon ) return null
5758
58- return isInlineSvg ? (
59- < span dangerouslySetInnerHTML = { { __html : iconUrlOrSvg } } style = { options . style } className = "svgicon" />
59+ return path . endsWith ( '.svg' ) ? (
60+ < span dangerouslySetInnerHTML = { { __html : icon } } style = { options . style } className = "svgicon" />
6061 ) : (
61- < img src = { iconUrlOrSvg } alt = "icon" style = { options . style } />
62+ < img src = { icon } alt = "icon" style = { options . style } />
6263 )
6364}
6465
0 commit comments