-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: iconmapper error fix * chore: fix import with idealImage * chore: fix * chore: added comments
- Loading branch information
1 parent
aef60a7
commit b316ad4
Showing
1 changed file
with
23 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} |