import { useEffect } from "react"; export type ShipActions = { onUndock: () => void; onStartApproach: () => void; onDock: () => void; onStartMining: () => void; onSelectTarget?: (index: number) => void; }; export function useKeyboardShortcuts( ship: { dockedStationId: string; flightMode: string; selectedPoiId: string; currentPoiId: string; } | undefined, pois: Array<{ poiId: string; poiType: string }>, operation: { operationType: string } | undefined, actions: ShipActions, ) { useEffect(() => { function handleKeyDown(event: KeyboardEvent) { if (event.target instanceof HTMLInputElement || event.target instanceof HTMLTextAreaElement) return; const key = event.key.toLowerCase(); if (key === "u" && !operation) { if (ship && ship.dockedStationId.length > 0) { event.preventDefault(); actions.onUndock(); } } if (key === "d" && !operation) { if (ship && ship.dockedStationId.length === 0 && ship.flightMode === "flight") { event.preventDefault(); actions.onDock(); } } if (key === "a" && !operation) { if (ship && ship.dockedStationId.length === 0 && ship.selectedPoiId.length > 0 && ship.selectedPoiId !== ship.currentPoiId) { event.preventDefault(); actions.onStartApproach(); } } if (key === "m" && !operation) { if (ship && ship.dockedStationId.length === 0 && ship.flightMode === "flight") { event.preventDefault(); actions.onStartMining(); } } if (key >= "1" && key <= "9") { const index = parseInt(key) - 1; if (index < pois.length && actions.onSelectTarget) { event.preventDefault(); actions.onSelectTarget(index); } } } window.addEventListener("keydown", handleKeyDown); return () => window.removeEventListener("keydown", handleKeyDown); }, [ship, pois, operation, actions]); }