Skip to content

Commit

Permalink
fix: added errorHandler prop to components
Browse files Browse the repository at this point in the history
  • Loading branch information
charmi-v committed Aug 29, 2024
1 parent 61a9839 commit 35fd310
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 283 deletions.
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
83 changes: 0 additions & 83 deletions src/components/basic/Notifications/Snackbar.mdx

This file was deleted.

This file was deleted.

72 changes: 0 additions & 72 deletions src/components/basic/Notifications/SnackbarManager/index.tsx

This file was deleted.

4 changes: 0 additions & 4 deletions src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ export { LoadMoreButton } from './basic/Button/LoadMoreButton'
export { PageNotifications } from './basic/Notifications/PageNotification'
export { PageSnackbar } from './basic/Notifications/Snackbar'
export { PageSnackbarStack } from './basic/Notifications/Snackbar/PageSnackbarStack'
export {
GlobalSnackbarContainer,
useSnackbar,
} from './basic/Notifications/SnackbarManager'
export { ErrorPage } from './basic/ErrorPage'
export { MultiSelectList } from './basic/MultiSelectList'
export { ProcessList } from './basic/ProcessList'
Expand Down

0 comments on commit 35fd310

Please sign in to comment.