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 { accountState } from '~utils/accountState' import { formatTimeAgo } from '~utils/timeAgo' import Button from '~components/Button' import { ButtonType } from '~utils/enums' import './index.scss' interface Props { shortcode: string id: string name: string raid: Raid grid: GridWeapon[] user?: User favorited: boolean createdAt: Date displayUser?: boolean | false onClick: (shortcode: string) => void onSave?: (partyId: string, favorited: boolean) => void } const GridRep = (props: Props) => { const numWeapons: number = 9 const { account } = useSnapshot(accountState) const router = useRouter() const { t } = useTranslation('common') const locale = router.locale && ['en', 'ja'].includes(router.locale) ? router.locale : 'en' const [mainhand, setMainhand] = useState() const [weapons, setWeapons] = useState>({}) const [grid, setGrid] = useState>({}) const titleClass = classNames({ empty: !props.name, }) const raidClass = classNames({ raid: true, empty: !props.raid, }) const userClass = classNames({ user: true, empty: !props.user, }) useEffect(() => { const newWeapons = Array(numWeapons) const gridWeapons = Array(numWeapons) for (const [key, value] of Object.entries(props.grid)) { if (value.position == -1) setMainhand(value.object) else if (!value.mainhand && value.position != null) { newWeapons[value.position] = value.object gridWeapons[value.position] = value } } setWeapons(newWeapons) setGrid(gridWeapons) }, [props.grid]) function navigate() { props.onClick(props.shortcode) } function generateMainhandImage() { let url = '' if (mainhand) { if (mainhand.element == 0 && props.grid[0].element) { url = `${process.env.NEXT_PUBLIC_SIERO_IMG_URL}/weapon-main/${mainhand.granblue_id}_${props.grid[0].element}.jpg` } else { url = `${process.env.NEXT_PUBLIC_SIERO_IMG_URL}/weapon-main/${mainhand.granblue_id}.jpg` } } return mainhand && props.grid[0] ? ( {mainhand.name[locale]} ) : ( '' ) } function generateGridImage(position: number) { let url = '' const weapon = weapons[position] const gridWeapon = grid[position] if (weapon && gridWeapon) { if (weapon.element == 0 && gridWeapon.element) { url = `${process.env.NEXT_PUBLIC_SIERO_IMG_URL}/weapon-grid/${weapon.granblue_id}_${gridWeapon.element}.jpg` } else { url = `${process.env.NEXT_PUBLIC_SIERO_IMG_URL}/weapon-grid/${weapon.granblue_id}.jpg` } } return weapons[position] ? ( {weapons[position]?.name[locale]} ) : ( '' ) } function sendSaveData() { if (props.onSave) props.onSave(props.id, props.favorited) } const userImage = () => { if (props.user && props.user.picture) { return ( {props.user.picture.picture} ) } else return
} const details = (

{props.name ? props.name : t('no_title')}

{props.raid ? props.raid.name[locale] : t('no_raid')}
) const detailsWithUsername = (

{props.name ? props.name : t('no_title')}

{props.raid ? props.raid.name[locale] : t('no_raid')}
{account.authorized && ((props.user && account.user && account.user.id !== props.user.id) || !props.user) ? (
{userImage()} {props.user ? props.user.username : t('no_user')}
) return (
{props.displayUser ? detailsWithUsername : details}
{generateMainhandImage()}
    {Array.from(Array(numWeapons)).map((x, i) => { return (
  • {generateGridImage(i)}
  • ) })}
) } export default GridRep