Skip to content

Commit

Permalink
chore: iconmapper error fix (#3669)
Browse files Browse the repository at this point in the history
* chore: iconmapper error fix

* chore: fix import with idealImage

* chore: fix

* chore: added comments
  • Loading branch information
karl-cardenas-coding authored Aug 20, 2024
1 parent aef60a7 commit b316ad4
Showing 1 changed file with 23 additions and 29 deletions.
52 changes: 23 additions & 29 deletions src/components/Technologies/PackCardIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,49 +1,43 @@
/* eslint-disable */
import React, { useState, useEffect, ReactElement } from "react";
import styles from "./PackCardIcon.module.scss";
import IconMapper from "@site/src/components/IconMapper/IconMapper";
import Image from "@theme/IdealImage";

interface PackCardIconProps {
appType?: string;
logoUrl?: string;
type?: any;
className?: any;
}

type ImageModule = {
default: string;
};

export default function PackCardIcon({ appType, logoUrl, type, className }: PackCardIconProps) {
const [icon, setIcon] = useState<ReactElement | null>(null);
// Declare a state variable 'icon' with an initial value of null,
// which will store the React element to be rendered as the icon.

useEffect(() => {
const loadIcon = async () => {
if (logoUrl && appType === "app") {
setIcon(<img src={logoUrl} />);
return;
}

if (logoUrl) {
try {
const module: ImageModule = await import(`/static/img/packs/${logoUrl}`);
setIcon(<Image img={module.default} />);
return;
} catch (e) {
console.error(e);
if (logoUrl) {
// Check if 'logoUrl' is provided.
try {
if (appType === "app") {
// If 'appType' is "app", set the icon to an img element with the 'logoUrl' as the source.
setIcon(<img src={logoUrl} />);
} else {
// Otherwise, set the icon to an Image component, requiring the image from a specific path.
setIcon(<Image img={require(`/static/img/packs/${logoUrl}`)} />);
}
} catch (e) {
// If an error occurs (e.g., the image cannot be found), fall back to the IconMapper component.
type ? setIcon(<IconMapper type={type} />) : setIcon(null);
}

if (type) {
setIcon(<IconMapper type={type} />);
} else {
setIcon(null);
}
};

// Explicitly mark the promise as intentionally unhandled
void loadIcon();
}, [logoUrl, appType, type]);
} else {
// If no 'logoUrl' is provided, fall back to rendering an icon based on the 'type' prop.
type ? setIcon(<IconMapper type={type} />) : setIcon(null);
}
}, [logoUrl]);
// The useEffect hook is triggered whenever 'logoUrl' changes.

return <div className={`${className} ${styles.imageWrapper}`}>{icon}</div>;
// Return a div containing the icon, with a combination of the provided 'className' and
// a CSS class from the imported styles.
}

0 comments on commit b316ad4

Please sign in to comment.