import clsx from "clsx"; import { forwardRef, memo, useEffect, useMemo, useState, type ComponentType, type MouseEventHandler, type SVGProps } from "react"; import "./rds-comp-ai-icon.scss"; import PersonOutlineIcon from "@mui/icons-material/PersonOutline"; import GroupOutlinedIcon from "@mui/icons-material/GroupOutlined"; /** Accepts MUI SvgIcon components and plain SVG React components. */ // eslint-disable-next-line @typescript-eslint/no-explicit-any type RdsIconComponent = ComponentType; const defaultMaterialIcons: Record = { users: GroupOutlinedIcon, "person-outline": PersonOutlineIcon, }; const materialIconsRegistry: Record = { ...defaultMaterialIcons, }; export const registerMaterialIcon = ( name: string, iconComponent: RdsIconComponent ) => { materialIconsRegistry[name.toLowerCase()] = iconComponent; try { window.dispatchEvent(new CustomEvent("rds-icons-updated")); } catch { /* handled */ } }; export const registerMaterialIcons = (icons: Record) => { Object.entries(icons).forEach(([name, component]) => { materialIconsRegistry[name.toLowerCase()] = component; }); try { window.dispatchEvent(new CustomEvent("rds-icons-updated")); } catch { /* handled */ } }; const createMuiIconWrapper = ( MuiIcon: RdsIconComponent ): ComponentType> => forwardRef>( ({ color, fontSize, className, style, ...restProps }, ref) => { const combinedStyle = { ...style, ...(color && { color }), ...(fontSize && { fontSize }), }; return ; } ); const resolveIconComponent = ( iconName: string, SvgIcon?: RdsIconComponent ): ComponentType> | null => { try { if (SvgIcon) return SvgIcon; if (iconName && materialIconsRegistry[iconName]) { return createMuiIconWrapper(materialIconsRegistry[iconName]); } return null; } catch { return null; } }; export interface RdsCompAiIconProps { width?: string; height?: string; colorVariant?: string; name?: string; fill?: boolean; stroke?: boolean; strokeWidth?: string; borderRadius?: string; onClick?: MouseEventHandler | null; opacity?: string; isAnimate?: boolean; classes?: string; dataTestId?: string; databsdismiss?: string; databstarget?: string; databstoggle?: string; ariacontrols?: string; imageUrl?: string; id?: string; iconPath?: string; type?: "icon" | "lottie"; isHover?: boolean; isContinueAnimate?: boolean; hovered?: boolean; isHovered?: boolean; isCursorPointer?: boolean; strokeColor?: string; SvgIcon?: ComponentType>; position?: "center" | "top-left" | "none"; } const RdsCompAiIconComponent = ({ width, height, colorVariant, name, fill, stroke, strokeWidth, onClick, classes, dataTestId, databsdismiss, databstarget, databstoggle, ariacontrols, imageUrl, id, isCursorPointer, strokeColor, SvgIcon, position, }: RdsCompAiIconProps) => { const normalizedName = name?.toLowerCase() ?? ""; const IconComponent = useMemo( () => resolveIconComponent(normalizedName, SvgIcon), [normalizedName, SvgIcon] ); const style = useMemo( () => ({ height: height ?? "22px", width: width ?? "22px", "--rds-ai-icon-size": "var(--rds-ai-icon-size)", strokeWidth: strokeWidth ?? "inherit", ...(position === "center" && { margin: "auto" }), ...(position === "top-left" && { margin: "0" }), ...(position === "none" && {}), ...(!position && { margin: "auto" }), }), [height, width, strokeWidth, position] ); const className = clsx( "rds-comp-ai-icon", classes, isCursorPointer && "rds-comp-ai-icon--cursor", colorVariant && `rds-comp-ai-icon--${colorVariant}` ); if (IconComponent) { try { const Icon = IconComponent; return ( ); } catch { return null; } } if (imageUrl) { const imageElement = ( ); if (onClick) { return ( ); } return ( imageElement ); } return null; }; const RdsCompAiIcon = memo(RdsCompAiIconComponent); RdsCompAiIcon.displayName = "RdsCompAiIcon"; export default RdsCompAiIcon;