Skip to content

Commit b0e4b70

Browse files
committed
refactor: update breed context to improve error handling and logic in computeBreedingUpdates
1 parent dceaa5e commit b0e4b70

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/components/pokemon/PokemonNodeNickname.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const PokemonNodeNickname: React.FC<PokemonNodeNicknameProps> = ({
2020
function saveNicknameIfChanged() {
2121
if (nickName !== currentNode.species?.name) {
2222
currentNode.nickname = nickName
23-
ctx.updateBreedTree({ runLogic: false })
23+
ctx.updateBreedTree({ compute: false })
2424
}
2525
}
2626

src/contexts/breed-context/index.tsx

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,19 @@ export function BreedContextProvider({
127127
_setBreedMap({ ...breedMap })
128128
}
129129

130-
function computeBreedingUpdates(map: PokemonBreedMap) {
131-
if (!breedTarget) return { updates: {}, errors: {} }
130+
function computeBreedingUpdates(
131+
map: PokemonBreedMap,
132+
target: PokemonBreedTarget | undefined = breedTarget,
133+
) {
134+
if (!target) {
135+
debugger
136+
return { updates: {}, errors: {} }
137+
}
132138

133139
const breeder = PokemonBreeder.getInstance()
134140
const updates: Record<PokemonBreedMapPositionKey, PokemonNode> = {}
135141
const errors: BreedErrors = {}
136-
const lastRow = breedTarget.nature
137-
? breedTarget.ivCount
138-
: breedTarget.ivCount - 1
142+
const lastRow = target.nature ? target.ivCount : target.ivCount - 1
139143
const rowLength = Math.pow(2, lastRow)
140144

141145
for (let col = 0; col < rowLength; col += 2) {
@@ -262,6 +266,8 @@ export function BreedContextProvider({
262266
)
263267
setBreedTarget(target)
264268
initializeBreedMap(target)
269+
const { errors } = computeBreedingUpdates(breedMap, target)
270+
setBreedErrors(errors)
265271
}
266272

267273
function save() {
@@ -271,7 +277,7 @@ export function BreedContextProvider({
271277
function setBreedErrors(errors: BreedErrors) {
272278
_setBreedErrors(errors)
273279
// Show toast notifications for errors
274-
Object.entries(breedErrors).forEach(([key, errorKind]) => {
280+
Object.entries(errors).forEach(([key, errorKind]) => {
275281
if (!errorKind) {
276282
return
277283
}
@@ -300,15 +306,20 @@ export function BreedContextProvider({
300306
}
301307

302308
if (errorMsg) {
303-
toast.error(
304-
`${node.species.name} cannot breed with ${partner.species.name}.`,
305-
{
306-
description: `Error codes: ${errorMsg}`,
307-
action: {
308-
label: "Dismiss",
309-
onClick: () => {},
310-
},
311-
},
309+
// bullshit-ass hack to make this toast show up when deserializing
310+
setTimeout(
311+
() =>
312+
toast.error(
313+
`${node!.species!.name} cannot breed with ${partner!.species!.name}.`,
314+
{
315+
description: `Error codes: ${errorMsg}`,
316+
action: {
317+
label: "Dismiss",
318+
onClick: () => {},
319+
},
320+
},
321+
),
322+
0,
312323
)
313324
}
314325
})
@@ -323,7 +334,6 @@ export function BreedContextProvider({
323334
persist?: boolean
324335
map?: PokemonBreedMap
325336
} = {}) {
326-
console.log("Updating breed tree...")
327337
if (compute) {
328338
const { updates, errors } = computeBreedingUpdates(breedMap)
329339
_setBreedMap({ ...map, ...updates })

0 commit comments

Comments
 (0)