|
|
import { useEffect } from 'react'; |
|
|
import { useDebugUMAPStore } from '../store'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function useKeyboardNavigation() { |
|
|
const configs = useDebugUMAPStore((state) => state.configs); |
|
|
const currentConfigIndex = useDebugUMAPStore((state) => state.currentConfigIndex); |
|
|
const setCurrentConfigIndex = useDebugUMAPStore((state) => state.setCurrentConfigIndex); |
|
|
const saveToLocalStorage = useDebugUMAPStore((state) => state.saveToLocalStorage); |
|
|
|
|
|
useEffect(() => { |
|
|
const handleKeyDown = (event) => { |
|
|
|
|
|
if (event.target.tagName === 'INPUT' || event.target.tagName === 'TEXTAREA') { |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
if (configs.length === 0) { |
|
|
return; |
|
|
} |
|
|
|
|
|
let newIndex = currentConfigIndex; |
|
|
|
|
|
switch (event.key) { |
|
|
case 'ArrowLeft': |
|
|
case '-': |
|
|
|
|
|
newIndex = currentConfigIndex > 0 |
|
|
? currentConfigIndex - 1 |
|
|
: configs.length - 1; |
|
|
break; |
|
|
|
|
|
case 'ArrowRight': |
|
|
case '+': |
|
|
case '=': |
|
|
|
|
|
newIndex = currentConfigIndex < configs.length - 1 |
|
|
? currentConfigIndex + 1 |
|
|
: 0; |
|
|
break; |
|
|
|
|
|
default: |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
event.preventDefault(); |
|
|
|
|
|
|
|
|
if (newIndex !== currentConfigIndex) { |
|
|
console.log(`Navigation clavier: ${event.key} -> config ${newIndex}`); |
|
|
setCurrentConfigIndex(newIndex); |
|
|
saveToLocalStorage(); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
document.addEventListener('keydown', handleKeyDown); |
|
|
|
|
|
|
|
|
return () => { |
|
|
document.removeEventListener('keydown', handleKeyDown); |
|
|
}; |
|
|
}, [configs.length, currentConfigIndex, setCurrentConfigIndex, saveToLocalStorage]); |
|
|
|
|
|
return { |
|
|
currentConfigIndex, |
|
|
totalConfigs: configs.length |
|
|
}; |
|
|
} |