import React, { useEffect, useState } from "react"; import { useRouter } from "next/router"; import { useSnapshot } from "valtio"; import { useTranslation } from "next-i18next"; import classnames from "classnames"; import { appState } from "~utils/appState"; import CharacterHovercard from "~components/CharacterHovercard"; import SearchModal from "~components/SearchModal"; import UncapIndicator from "~components/UncapIndicator"; import PlusIcon from "~public/icons/Add.svg"; import type { SearchableObject } from "~types"; import "./index.scss"; interface Props { gridCharacter?: GridCharacter; position: number; editable: boolean; updateObject: (object: SearchableObject, position: number) => void; updateUncap: (id: string, position: number, uncap: number) => void; } const CharacterUnit = (props: Props) => { const { t } = useTranslation("common"); const { party, grid } = useSnapshot(appState); const router = useRouter(); const locale = router.locale && ["en", "ja"].includes(router.locale) ? router.locale : "en"; const [imageUrl, setImageUrl] = useState(""); const classes = classnames({ CharacterUnit: true, editable: props.editable, filled: props.gridCharacter !== undefined, }); const gridCharacter = props.gridCharacter; const character = gridCharacter?.object; useEffect(() => { generateImageUrl(); }); function generateImageUrl() { let imgSrc = ""; if (props.gridCharacter) { const character = props.gridCharacter.object!; // Change the image based on the uncap level let suffix = "01"; if (props.gridCharacter.uncap_level == 6) suffix = "04"; else if (props.gridCharacter.uncap_level == 5) suffix = "03"; else if (props.gridCharacter.uncap_level > 2) suffix = "02"; // Special casing for Lyria (and Young Cat eventually) if (props.gridCharacter.object.granblue_id === "3030182000") { let element = 1; if (grid.weapons.mainWeapon && grid.weapons.mainWeapon.element) { element = grid.weapons.mainWeapon.element; } else if (party.element != 0) { element = party.element; } suffix = `${suffix}_0${element}`; } imgSrc = `${process.env.NEXT_PUBLIC_SIERO_IMG_URL}/chara-main/${character.granblue_id}_${suffix}.jpg`; } setImageUrl(imgSrc); } function passUncapData(uncap: number) { if (props.gridCharacter) props.updateUncap(props.gridCharacter.id, props.position, uncap); } const image = (
{character?.name.en} {props.editable ? ( ) : ( "" )}
); const editableImage = ( {image} ); const unitContent = (
{props.editable ? editableImage : image} {gridCharacter && character ? ( ) : ( "" )}

{character?.name[locale]}

); const withHovercard = ( {unitContent} ); return gridCharacter && !props.editable ? withHovercard : unitContent; }; export default CharacterUnit;