fix: Battle 快捷键通过 inputRef 转发至主 Ink 上下文

BattleFlow 不再直接调用 useInput(外部包的 Ink 上下文可能不同),
改为暴露 handleInput 方法,由 BuddyPanel 中的 BattleTab 通过
useInput + ref 转发键盘事件,确保在正确的 StdinContext 中工作。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
claude-code-best
2026-04-22 08:44:33 +08:00
parent d8e33935db
commit bd70971632
3 changed files with 125 additions and 112 deletions

View File

@@ -1,5 +1,5 @@
import * as React from 'react';
import { useState } from 'react';
import { useState, useRef } from 'react';
import { Box, Text, Pane, Tab, Tabs, useInput, type Color } from '@anthropic/ink';
import { useSetAppState } from '../../state/AppState.js';
import { useKeybinding } from '../../keybindings/useKeybinding.js';
@@ -22,6 +22,7 @@ import { getXpProgress } from '@claude-code-best/pokemon';
import { getGenderSymbol } from '@claude-code-best/pokemon';
import { StatBar, SpriteAnimator, getFallbackSprite, loadSprite } from '@claude-code-best/pokemon';
import { BattleFlow, loadBuddyData } from '@claude-code-best/pokemon';
import type { BattleFlowHandle } from '@claude-code-best/pokemon';
import type { LocalJSXCommandOnDone } from '../../types/command.js';
const CYAN: Color = 'ansi:cyan';
@@ -633,6 +634,13 @@ function BattleTab({
onUpdate: (data: BuddyData) => void;
}) {
const [battleKey, setBattleKey] = useState(0);
const inputRef = useRef<BattleFlowHandle | null>(null);
// Handle input here (in main app's Ink context) and forward to BattleFlow via ref
useInput((input, key) => {
if (!isActive) return;
inputRef.current?.handleInput(input, key);
});
const handleClose = async () => {
const updated = await loadBuddyData();
@@ -646,6 +654,7 @@ function BattleTab({
buddyData={buddyData}
onClose={handleClose}
isActive={isActive}
inputRef={inputRef}
/>
);
}