1- import { PathHelper } from '@sensenet/client-utils'
21import React , { memo , useEffect , useState } from 'react'
32import { IconOptions } from './Icon'
43
@@ -10,56 +9,56 @@ type IconFromPathProps = {
109}
1110
1211const IconFromPathComponent = ( { path, options, contentPath, contentType } : IconFromPathProps ) => {
13- const [ icon , setIcon ] = useState < string | null > ( null )
12+ const [ iconUrlOrSvg , setIconUrlOrSvg ] = useState < string | null > ( null )
13+ const [ isInlineSvg , setIsInlineSvg ] = useState ( false )
1414
1515 useEffect ( ( ) => {
1616 const loadIcon = async ( ) => {
17- let svgPath = path
17+ const fileName = path . split ( '/' ) . pop ( ) || ''
18+ let svgPath = `/icons/${ fileName } `
1819
1920 if ( contentType . toLowerCase ( ) . endsWith ( 'file' ) ) {
2021 if ( contentPath . toLowerCase ( ) . endsWith ( '.csv' ) ) {
2122 svgPath = '/icons/csv.svg'
2223 } else if ( contentPath . toLowerCase ( ) . endsWith ( '.svg' ) ) {
2324 svgPath = '/icons/file_img.svg'
2425 }
25- } else {
26- const fileName = path . split ( '/' ) . pop ( )
27- if ( fileName ) {
28- svgPath = `/icons/${ fileName } `
29- }
3026 }
3127
28+ // For SVGs, fetch and inline them
3229 if ( svgPath . endsWith ( '.svg' ) ) {
3330 try {
34- const response = await options . repo . fetch ( svgPath , { cache : 'force-cache' } )
31+ const response = await fetch ( svgPath , {
32+ cache : 'no-store' ,
33+ } )
3534 if ( ! response . ok ) return
36-
3735 const svgText = await response . text ( )
3836 const resizedSvg = svgText
3937 . replace ( 'width=' , 'width="24px" oldwidth=' )
4038 . replace ( 'height=' , 'height="24px" oldheight=' )
4139
42- options . repo . iconCache . set ( path , resizedSvg )
43- setIcon ( resizedSvg )
40+ setIsInlineSvg ( true )
41+ setIconUrlOrSvg ( resizedSvg )
4442 } catch ( e ) {
45- console . warn ( 'Failed to load SVG icon :' , e )
43+ console . warn ( 'Failed to load SVG:' , e )
4644 }
4745 return
4846 }
4947
50- setIcon ( svgPath )
51- options . repo . iconCache . set ( path , svgPath )
48+ // For non-SVG fallback
49+ setIconUrlOrSvg ( svgPath )
50+ setIsInlineSvg ( false )
5251 }
5352
5453 loadIcon ( )
55- } , [ path , contentPath , contentType , options . repo ] )
54+ } , [ path , contentPath , contentType ] )
5655
57- if ( ! icon ) return null
56+ if ( ! iconUrlOrSvg ) return null
5857
59- return path . endsWith ( '.svg' ) ? (
60- < span dangerouslySetInnerHTML = { { __html : icon } } style = { options . style } className = "svgicon" />
58+ return isInlineSvg ? (
59+ < span dangerouslySetInnerHTML = { { __html : iconUrlOrSvg } } style = { options . style } className = "svgicon" />
6160 ) : (
62- < img src = { icon } alt = "icon" style = { options . style } />
61+ < img src = { iconUrlOrSvg } alt = "icon" style = { options . style } />
6362 )
6463}
6564
0 commit comments