Use snapshot for segment reps

Using snapshots lets that data be reactive.

Also removed extra dependencies and fixed a bug in how SummonRep displayed sub-summons
This commit is contained in:
Justin Edmund 2023-06-18 15:36:24 -07:00
parent a90aa02f88
commit aee641f27e
3 changed files with 22 additions and 23 deletions

View file

@ -1,20 +1,25 @@
import React from 'react' import React from 'react'
import { useSnapshot } from 'valtio' import { useSnapshot } from 'valtio'
import { useTranslation } from 'next-i18next' import { useTranslation } from 'next-i18next'
import classNames from 'classnames'
import { appState } from '~utils/appState' import { appState } from '~utils/appState'
import { accountState } from '~utils/accountState'
import SegmentedControl from '~components/common/SegmentedControl' import SegmentedControl from '~components/common/SegmentedControl'
import RepSegment from '~components/reps/RepSegment'
import CharacterRep from '~components/reps/CharacterRep'
import WeaponRep from '~components/reps/WeaponRep'
import SummonRep from '~components/reps/SummonRep'
import { GridType } from '~utils/enums' import { GridType } from '~utils/enums'
import './index.scss' import './index.scss'
import classNames from 'classnames'
import RepSegment from '~components/reps/RepSegment' // Fix for valtio readonly array
import CharacterRep from '~components/reps/CharacterRep' declare module 'valtio' {
import { accountState } from '~utils/accountState' function useSnapshot<T extends object>(p: T): T
import WeaponRep from '~components/reps/WeaponRep' }
import SummonRep from '~components/reps/SummonRep'
interface Props { interface Props {
selectedTab: GridType selectedTab: GridType
@ -27,7 +32,7 @@ const PartySegmentedControl = (props: Props) => {
const { party, grid } = useSnapshot(appState) const { party, grid } = useSnapshot(appState)
function getElement() { const getElement = () => {
let element: number = 0 let element: number = 0
if (party.element == 0 && grid.weapons.mainWeapon) if (party.element == 0 && grid.weapons.mainWeapon)
element = grid.weapons.mainWeapon.element element = grid.weapons.mainWeapon.element
@ -55,16 +60,16 @@ const PartySegmentedControl = (props: Props) => {
controlGroup="grid" controlGroup="grid"
inputName="characters" inputName="characters"
name={t('party.segmented_control.characters')} name={t('party.segmented_control.characters')}
selected={props.selectedTab == GridType.Character} selected={props.selectedTab === GridType.Character}
onClick={props.onClick} onClick={props.onClick}
> >
<CharacterRep <CharacterRep
job={appState.party?.job} job={party.job}
element={appState.party?.element} element={party.element}
gender={ gender={
accountState.account.user ? accountState.account.user.gender : 0 accountState.account.user ? accountState.account.user.gender : 0
} }
grid={appState.grid.characters} grid={grid.characters}
/> />
</RepSegment> </RepSegment>
) )
@ -77,10 +82,10 @@ const PartySegmentedControl = (props: Props) => {
controlGroup="grid" controlGroup="grid"
inputName="weapons" inputName="weapons"
name="Weapons" name="Weapons"
selected={props.selectedTab == GridType.Weapon} selected={props.selectedTab === GridType.Weapon}
onClick={props.onClick} onClick={props.onClick}
> >
<WeaponRep grid={appState.grid.weapons} /> <WeaponRep grid={grid.weapons} />
</RepSegment> </RepSegment>
) )
} }
@ -92,10 +97,10 @@ const PartySegmentedControl = (props: Props) => {
controlGroup="grid" controlGroup="grid"
inputName="summons" inputName="summons"
name="Summons" name="Summons"
selected={props.selectedTab == GridType.Summon} selected={props.selectedTab === GridType.Summon}
onClick={props.onClick} onClick={props.onClick}
> >
<SummonRep grid={appState.grid.summons} /> <SummonRep grid={grid.summons} />
</RepSegment> </RepSegment>
) )
} }

View file

@ -1,7 +1,5 @@
import React, { useEffect, useState } from 'react' import React, { useEffect, useState } from 'react'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import { useTranslation } from 'next-i18next'
import 'fix-date'
import './index.scss' import './index.scss'
@ -18,7 +16,6 @@ const SUMMONS_COUNT = 4
const SummonRep = (props: Props) => { const SummonRep = (props: Props) => {
// Localization for alt tags // Localization for alt tags
const router = useRouter() const router = useRouter()
const { t } = useTranslation('common')
const locale = const locale =
router.locale && ['en', 'ja'].includes(router.locale) ? router.locale : 'en' router.locale && ['en', 'ja'].includes(router.locale) ? router.locale : 'en'
@ -159,8 +156,8 @@ const SummonRep = (props: Props) => {
<ul className="GridSummons"> <ul className="GridSummons">
{Array.from(Array(SUMMONS_COUNT)).map((x, i) => { {Array.from(Array(SUMMONS_COUNT)).map((x, i) => {
return ( return (
<li key={`summons-${i + 1}`} className="Grid Summon"> <li key={`summons-${i}`} className="Grid Summon">
{generateGridImage(i + 1)} {generateGridImage(i)}
</li> </li>
) )
})} })}

View file

@ -1,7 +1,5 @@
import React, { useEffect, useState } from 'react' import React, { useEffect, useState } from 'react'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import { useTranslation } from 'next-i18next'
import 'fix-date'
import './index.scss' import './index.scss'
@ -17,7 +15,6 @@ const WEAPONS_COUNT = 9
const WeaponRep = (props: Props) => { const WeaponRep = (props: Props) => {
// Localization for alt tags // Localization for alt tags
const router = useRouter() const router = useRouter()
const { t } = useTranslation('common')
const locale = const locale =
router.locale && ['en', 'ja'].includes(router.locale) ? router.locale : 'en' router.locale && ['en', 'ja'].includes(router.locale) ? router.locale : 'en'