Skip to content

Commit

Permalink
feat(helper): image validator
Browse files Browse the repository at this point in the history
Signed-off-by: sudhanshu dasgupta <[email protected]>
  • Loading branch information
sudhanshutech committed Jan 30, 2024
1 parent 2e5995f commit e5c042a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type DispatchImageFunction = (url: string) => void;

const handleImageValidation = async (url: string, dispatchImage: DispatchImageFunction): Promise<boolean> => {
try {
const img = new Image();
img.src = url;

await new Promise<void>((resolve, reject) => {
img.onload = () => resolve();
img.onerror = () => reject();
});

dispatchImage(url);
return true; // Image loaded successfully
} catch (error) {
return false; // Image failed to load
}
};

export default handleImageValidation;
3 changes: 3 additions & 0 deletions packages/components/src/custom/Helpers/UrlValidator/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import handleImageValidation from './image-url-validation';

export { handleImageValidation };
7 changes: 7 additions & 0 deletions packages/components/src/custom/Helpers/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ To use these helper components in your project, follow these steps:
</button>
);
```
3. **ImageUrlValidator**: A custom React hook for validating image URLs.
- **File**: `ImageUrlValidator`
- **Usage**: Provides the `useImageUrlValidator` hook, which allows you to validate image URLs.
- **Returns**: function that accepts an image URL and a dispatch function, and returns a Promise<boolean> indicating whether the image is valid or not.
4 changes: 3 additions & 1 deletion packages/components/src/custom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FlipCard } from './FlipCard';
import { useWindowDimensions } from './Helpers/Dimension';
import { useNotificationHandler } from './Helpers/Notification';
import { StyledTooltip } from './Tooltip';
import { handleImageValidation } from './Helpers/UrlValidator';

export { StyledChartDialog } from './ChartDialog';
export { StyledSearchBar } from './StyledSearchBar';
Expand All @@ -17,5 +18,6 @@ export {
WithErrorBoundary,
useNotificationHandler,
useWindowDimensions,
withSuppressedErrorBoundary
withSuppressedErrorBoundary,
handleImageValidation
};

0 comments on commit e5c042a

Please sign in to comment.