11import React from "react" ;
2- import { Box } from "lucide-react" ;
2+ import { File } from "lucide-react" ;
33
44import IconWrapper from "./IconWrapper" ;
55import ThemedIcon from "./ThemedIcon" ;
66import FontIcon from "./FontIcon" ;
77import { useAppStore } from "@/stores/appStore" ;
8+ import platformAdapter from "@/utils/platformAdapter" ;
89
910interface UniversalIconProps {
10- icon ?: string ; // Icon source
11- defaultIcon ?: React . FC ; // Default icon component
12- className ?: string ; // Style class name
11+ icon ?: string ; // Icon source
12+ defaultIcon ?: React . FC | string ; // Default icon component
13+ appIcon ?: boolean ; // Whether the icon is local
14+ className ?: string ; // Style class name
1315 onClick ?: React . MouseEventHandler < HTMLDivElement > ;
1416 wrapWithIconWrapper ?: boolean ; // Whether to wrap with IconWrapper
1517}
1618
17- type IconType = 'url' | 'base64' | 'font' | 'local' | 'splice' | 'default' ;
19+ type IconType =
20+ | "url"
21+ | "base64"
22+ | "font"
23+ | "local"
24+ | "app"
25+ | "splice"
26+ | "default" ;
27+
28+ // Determine icon type
29+ export const getIconType = ( icon ?: string , appIcon ?: boolean ) : IconType => {
30+ if ( ! icon ) return "default" ;
31+ if ( appIcon ) return "app" ;
32+ if ( icon . startsWith ( "http://" ) || icon . startsWith ( "https://" ) ) return "url" ;
33+ if ( icon . startsWith ( "data:image/" ) ) return "base64" ;
34+ if ( icon . startsWith ( "font_" ) ) return "font" ;
35+ if ( icon . startsWith ( "/assets" ) ) return "local" ;
36+ if ( icon . startsWith ( "/" ) ) return "splice" ;
37+ return "default" ;
38+ } ;
1839
1940function UniversalIcon ( {
2041 icon,
21- defaultIcon = Box ,
42+ defaultIcon = File ,
43+ appIcon = false ,
2244 className = "w-5 h-5 flex-shrink-0" ,
2345 onClick = ( ) => { } ,
2446 wrapWithIconWrapper = true ,
2547} : UniversalIconProps ) {
2648 const endpoint_http = useAppStore ( ( state ) => state . endpoint_http ) ;
2749
28- // Determine icon type
29- const getIconType = ( icon ?: string ) : IconType => {
30- if ( ! icon ) return 'default' ;
31- if ( icon . startsWith ( 'http://' ) || icon . startsWith ( 'https://' ) ) return 'url' ;
32- if ( icon . startsWith ( 'data:image/' ) ) return 'base64' ;
33- if ( icon . startsWith ( 'font_' ) ) return 'font' ;
34- if ( icon . startsWith ( '/assets' ) ) return 'local' ;
35- if ( icon . startsWith ( '/' ) ) return 'splice' ;
36- return 'default' ;
37- } ;
38-
3950 // Render image type icon
4051 const renderImageIcon = ( src : string ) => {
4152 const img = < img className = { className } src = { src } alt = "icon" /> ;
4253 return wrapWithIconWrapper ? (
4354 < IconWrapper className = { className } onClick = { onClick } >
4455 { img }
4556 </ IconWrapper >
46- ) : img ;
57+ ) : (
58+ img
59+ ) ;
60+ } ;
61+
62+ // Render app type icon
63+ const renderAppIcon = ( src : string ) => {
64+ const img = (
65+ < img
66+ className = { className }
67+ src = { platformAdapter . convertFileSrc ( src ) }
68+ alt = "icon"
69+ />
70+ ) ;
71+ return wrapWithIconWrapper ? (
72+ < IconWrapper className = { className } onClick = { onClick } >
73+ { img }
74+ </ IconWrapper >
75+ ) : (
76+ img
77+ ) ;
4778 } ;
4879
4980 // Render default icon
5081 const renderDefaultIcon = ( ) => {
51- const defaultComponent = < ThemedIcon component = { defaultIcon } className = { className } /> ;
82+ if ( typeof defaultIcon === "string" ) {
83+ return renderImageIcon ( defaultIcon ) ;
84+ }
85+ const defaultComponent = (
86+ < ThemedIcon component = { defaultIcon } className = { className } />
87+ ) ;
5288 return wrapWithIconWrapper ? (
5389 < IconWrapper className = { className } onClick = { onClick } >
5490 { defaultComponent }
5591 </ IconWrapper >
56- ) : defaultComponent ;
92+ ) : (
93+ defaultComponent
94+ ) ;
5795 } ;
5896
5997 // Render component based on icon type
60- const iconType = getIconType ( icon ) ;
98+ const iconType = getIconType ( icon , appIcon ) ;
6199 switch ( iconType ) {
62- case 'url' :
63- case 'base64' :
64- case 'local' :
65- return renderImageIcon ( icon ! ) ;
66- case 'splice' :
67- const url = `${ endpoint_http } ${ icon ! } `
100+ case "url" :
101+ case "base64" :
102+ case "local" :
103+ return renderImageIcon ( icon ! ) ;
104+ case "app" :
105+ return renderAppIcon ( icon ! ) ;
106+ case "splice" :
107+ const url = `${ endpoint_http } ${ icon ! } ` ;
68108 return renderImageIcon ( url ) ;
69- case ' font' :
109+ case " font" :
70110 return < FontIcon name = { icon ! } className = { className } /> ;
71111 default :
72112 return renderDefaultIcon ( ) ;
73113 }
74114}
75115
76- export default UniversalIcon ;
116+ export default UniversalIcon ;
0 commit comments