From 66016dedf023b1d6988c0cc6e1aa5c32b125f1fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Mon, 15 Jan 2024 15:02:04 -0600 Subject: [PATCH] feat: simplified duel state --- package.json | 1 + .../[id]/components/AntagonistBar.tsx | 100 ++++++----- .../[id]/components/Model/TankDisplay.tsx | 31 ++-- .../components/Model/components/TankArmor.tsx | 48 +++--- .../components/Model/components/TankModel.tsx | 13 +- .../tankopedia/[id]/components/Modules.tsx | 67 +++++++ .../[id]/components/RotationInputs.tsx | 13 +- .../tankopedia/[id]/components/Title.tsx | 29 ++++ src/app/tools/tankopedia/[id]/page.tsx | 163 ++++++------------ src/components/ArmorMesh/index.tsx | 11 +- src/core/hooks/useModelDefinitions.ts | 6 + src/stores/tankopedia.ts | 28 +-- yarn.lock | 12 ++ 13 files changed, 276 insertions(+), 246 deletions(-) create mode 100644 src/app/tools/tankopedia/[id]/components/Modules.tsx create mode 100644 src/app/tools/tankopedia/[id]/components/Title.tsx create mode 100644 src/core/hooks/useModelDefinitions.ts diff --git a/package.json b/package.json index 3872efc9c..d7afcc632 100644 --- a/package.json +++ b/package.json @@ -59,6 +59,7 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "react-fuzzysort": "^1.0.1", + "react-promise-suspense": "^0.3.4", "react-spinners": "^0.13.8", "satori": "^0.10.8", "sharp": "^0.33.2", diff --git a/src/app/tools/tankopedia/[id]/components/AntagonistBar.tsx b/src/app/tools/tankopedia/[id]/components/AntagonistBar.tsx index 819856db7..d643b7535 100644 --- a/src/app/tools/tankopedia/[id]/components/AntagonistBar.tsx +++ b/src/app/tools/tankopedia/[id]/components/AntagonistBar.tsx @@ -16,8 +16,9 @@ import { Tooltip, } from '@radix-ui/themes'; import { go } from 'fuzzysort'; +import { produce } from 'immer'; import { debounce } from 'lodash'; -import { use, useRef, useState } from 'react'; +import { Dispatch, SetStateAction, use, useRef, useState } from 'react'; import { ModuleButton } from '../../../../../components/ModuleButton'; import { SmallTankIcon } from '../../../../../components/SmallTankIcon'; import { @@ -25,23 +26,26 @@ import { tankDefinitions, tankNamesDiacritics, } from '../../../../../core/blitzkrieg/tankDefinitions'; -import mutateTankopedia, { - useTankopedia, -} from '../../../../../stores/tankopedia'; +import { useTankopedia } from '../../../../../stores/tankopedia'; +import { Duel } from '../page'; -export function AntagonistBar() { +interface AntagonistBarProps { + duel: Duel; + setDuel: Dispatch>; +} + +export function AntagonistBar({ + duel: { antagonist }, + setDuel, +}: AntagonistBarProps) { const awaitedTankDefinitions = use(tankDefinitions); const mode = useTankopedia((state) => state.mode); const awaitedTankNamesDiacritics = use(tankNamesDiacritics); const [tab, setTab] = useState('search'); const searchInput = useRef(null); - const antagonist = useTankopedia((state) => { - if (!state.areTanksAssigned) return; - return state.antagonist; - }); const [searchResults, setSearchResults] = useState([]); - if (!antagonist || mode !== 'armor') return null; + if (mode !== 'armor') return null; return ( @@ -58,10 +62,11 @@ export function AntagonistBar() { key={shell.id} last={index === antagonist.gun.shells.length - 1} onClick={() => { - mutateTankopedia((draft) => { - if (!draft.areTanksAssigned) return; - draft.antagonist.shell = shell; - }); + setDuel( + produce((draft) => { + draft.antagonist.shell = shell; + }), + ); }} /> ); @@ -147,17 +152,17 @@ export function AntagonistBar() { key={tank.id} variant="ghost" onClick={() => { - mutateTankopedia((draft) => { - if (!draft.areTanksAssigned) return; - - draft.antagonist.tank = tank; - draft.antagonist.turret = - tank.turrets.at(-1)!; - draft.antagonist.gun = - draft.antagonist.turret.guns.at(-1)!; - draft.antagonist.shell = - draft.antagonist.gun.shells[0]; - }); + setDuel( + produce((draft) => { + draft.antagonist.tank = tank; + draft.antagonist.turret = + tank.turrets.at(-1)!; + draft.antagonist.gun = + draft.antagonist.turret.guns.at(-1)!; + draft.antagonist.shell = + draft.antagonist.gun.shells[0]; + }), + ); setSearchResults([]); searchInput.current!.value = ''; setTab('configure'); @@ -200,15 +205,15 @@ export function AntagonistBar() { } key={turret.id} onClick={() => { - mutateTankopedia((draft) => { - if (!draft.areTanksAssigned) return; - - draft.antagonist.turret = turret; - draft.antagonist.gun = - turret.guns.at(-1)!; - draft.antagonist.shell = - draft.antagonist.gun.shells[0]; - }); + setDuel( + produce((draft) => { + draft.antagonist.turret = turret; + draft.antagonist.gun = + turret.guns.at(-1)!; + draft.antagonist.shell = + draft.antagonist.gun.shells[0]; + }), + ); }} selected={antagonist.turret.id === turret.id} tier={turret.tier} @@ -228,11 +233,12 @@ export function AntagonistBar() { index === antagonist.turret.guns.length - 1 } onClick={() => { - mutateTankopedia((draft) => { - if (!draft.areTanksAssigned) return; - draft.antagonist.gun = gun; - draft.antagonist.shell = gun.shells[0]; - }); + setDuel( + produce((draft) => { + draft.antagonist.gun = gun; + draft.antagonist.shell = gun.shells[0]; + }), + ); }} selected={antagonist.gun.id === gun.id} tier={gun.tier} @@ -277,14 +283,14 @@ export function AntagonistBar() {