Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added error handler prop to components #277

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/components/basic/Icon/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ declare module '@mui/material/SvgIcon' {
}
export interface IconProps extends SvgIconProps {
iconName: keyof typeof MUIIcons
onError?: () => void
}

// Use a switch case for scalable integration of additional icon libraries.
Expand All @@ -47,11 +48,22 @@ const getIconComponent = (iconName: string) => {
] as React.ComponentType<SvgIconProps>
}

export const Icon: React.FC<IconProps> = ({ iconName, ...props }) => {
// defining default error handler
const defaultOnError = (iconName: string) => {
console.warn(`Icon ${iconName} does not exist in @mui/icons-material`)
}

export const Icon: React.FC<IconProps> = ({
iconName,
onError = () => {
defaultOnError(iconName)
},
...props
}) => {
const IconComponent = iconName ? getIconComponent(iconName) : null

if (!IconComponent) {
console.warn(`Icon ${iconName} does not exist in @mui/icons-material`)
onError()
return null
}

Expand Down
15 changes: 11 additions & 4 deletions src/components/basic/Image/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,16 @@ interface ImageProps {
alt?: string
style?: React.CSSProperties
loader?: (src: string) => Promise<ArrayBuffer>
onError?: (e: Error) => void
}

export const Image = ({ src, alt, style, loader }: ImageProps): JSX.Element => {
export const Image = ({
src,
alt,
style,
loader,
onError,
}: ImageProps): JSX.Element => {
const [data, setData] = useState(LogoGrayData)
const [error, setError] = useState(false)

Expand All @@ -62,14 +69,14 @@ export const Image = ({ src, alt, style, loader }: ImageProps): JSX.Element => {
IMAGE_TYPES[firstByte] ?? IMAGE_TYPES[first3Bytes] ?? 'image/*'
setData(URL.createObjectURL(new Blob([buffer], { type: imageType })))
} catch (e) {
// defining default error handler
onError ? onError(e as Error) : console.error(e)
setData(LogoGrayData)
}
}, [src, loader])

useEffect(() => {
getData().catch((e) => {
console.error(e)
})
void getData()
}, [getData])

return (
Expand Down