From 70d8c0038c4526089e54004874b58b732a953b24 Mon Sep 17 00:00:00 2001 From: claude-code-best Date: Wed, 22 Apr 2026 06:32:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=BF=9B=E5=8C=96=E9=93=BE?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E7=94=9F=E6=88=90=E6=9B=BF=E4=BB=A3=E7=A1=AC?= =?UTF-8?q?=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - PokedexView.tsx: groupByChain() 改用 ALL_SPECIES_IDS + getNextEvolution 动态构建 - SpeciesDetail.tsx: EvolutionChain 用相同方式找链头 - 删除未使用的 isInChain 函数 Co-Authored-By: Claude Opus 4.6 --- packages/pokemon/src/ui/PokedexView.tsx | 35 +++++++++++++++++++---- packages/pokemon/src/ui/SpeciesDetail.tsx | 22 ++++---------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/packages/pokemon/src/ui/PokedexView.tsx b/packages/pokemon/src/ui/PokedexView.tsx index a55b80d99..587b462b7 100644 --- a/packages/pokemon/src/ui/PokedexView.tsx +++ b/packages/pokemon/src/ui/PokedexView.tsx @@ -154,10 +154,33 @@ function getTypeColor(type: string): Color { /** Group species by evolution chain for visual display */ function groupByChain(): SpeciesId[][] { - return [ - ['bulbasaur', 'ivysaur', 'venusaur'], - ['charmander', 'charmeleon', 'charizard'], - ['squirtle', 'wartortle', 'blastoise'], - ['pikachu'], - ] + const visited = new Set() + const chains: SpeciesId[][] = [] + + for (const id of ALL_SPECIES_IDS) { + if (visited.has(id)) continue + + // Walk back to find chain head + let head: SpeciesId = id + for (const candidate of ALL_SPECIES_IDS) { + const evo = getNextEvolution(candidate) + if (evo?.to === head) { + head = candidate + break + } + } + + // Walk forward to build chain + const chain: SpeciesId[] = [] + let current: SpeciesId | undefined = head + while (current && !visited.has(current)) { + chain.push(current) + visited.add(current) + current = getNextEvolution(current)?.to + } + + if (chain.length > 0) chains.push(chain) + } + + return chains } diff --git a/packages/pokemon/src/ui/SpeciesDetail.tsx b/packages/pokemon/src/ui/SpeciesDetail.tsx index 437b67648..f499a2ed1 100644 --- a/packages/pokemon/src/ui/SpeciesDetail.tsx +++ b/packages/pokemon/src/ui/SpeciesDetail.tsx @@ -1,7 +1,7 @@ import React from 'react' import { Box, Text, type Color } from '@anthropic/ink' import type { SpeciesId, StatName } from '../types' -import { STAT_NAMES, STAT_LABELS } from '../types' +import { STAT_NAMES, STAT_LABELS, ALL_SPECIES_IDS } from '../types' import { getSpeciesData } from '../data/species' import { getNextEvolution } from '../data/evolution' import { StatBar } from './StatBar' @@ -135,12 +135,12 @@ export function SpeciesDetail({ speciesId, caughtLevel, spriteLines }: SpeciesDe /** Render evolution chain arrow */ function EvolutionChain({ speciesId }: { speciesId: SpeciesId }) { - // Find the chain head - const chainHeads: SpeciesId[] = ['bulbasaur', 'charmander', 'squirtle', 'pikachu'] + // Walk back to find chain head let head: SpeciesId = speciesId - for (const starter of chainHeads) { - if (isInChain(speciesId, starter)) { - head = starter + for (const candidate of ALL_SPECIES_IDS) { + const evo = getNextEvolution(candidate) + if (evo?.to === head) { + head = candidate break } } @@ -174,13 +174,3 @@ function EvolutionChain({ speciesId }: { speciesId: SpeciesId }) { ) } -function isInChain(target: SpeciesId, head: SpeciesId): boolean { - let current: SpeciesId | undefined = head - while (current) { - if (current === target) return true - const next = getNextEvolution(current) - current = next ? next.to : undefined - } - return false -} -