30 lines
968 B
TypeScript
30 lines
968 B
TypeScript
import {FC, HTMLProps, useMemo} from "react"
|
|
import {Entity} from "../../../src/types"
|
|
import {useFactories} from "../../../src/hooks/useFactories"
|
|
import styles from './EntityIcon.module.css'
|
|
|
|
interface Props extends Omit<HTMLProps<HTMLSpanElement>, 'value'> {
|
|
value: Entity|string
|
|
amount?: number
|
|
}
|
|
|
|
export const EntityIcon: FC<Props> = ({value, amount, ...rest}) => {
|
|
const {findFactory} = useFactories()
|
|
const entity = useMemo<Entity>(() => {
|
|
return typeof value === "object"
|
|
? value
|
|
: findFactory(value) ?? {
|
|
href: value,
|
|
name: value,
|
|
image: value,
|
|
recipe: undefined
|
|
}
|
|
}, [findFactory, value])
|
|
|
|
return <span className={styles.span} {...rest}>
|
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|
<img className={styles.img} src={`https://wiki.factorio.com${entity.image}`} alt={entity.name}/>
|
|
{amount !== undefined && <span className={styles.amount}>{amount}</span>}
|
|
</span>
|
|
}
|