File size: 2,302 Bytes
6bda4a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { useEffect } from 'react';
import { useDebugUMAPStore } from '../store';

/**
 * Hook pour gérer la navigation au clavier
 * Flèches gauche/droite pour changer de configuration de manière rotative
 */
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) => {
      // Ignorer si on est dans un input ou textarea
      if (event.target.tagName === 'INPUT' || event.target.tagName === 'TEXTAREA') {
        return;
      }

      // Ignorer si on n'a pas de configurations
      if (configs.length === 0) {
        return;
      }

      let newIndex = currentConfigIndex;

      switch (event.key) {
        case 'ArrowLeft':
        case '-':
          // Configuration précédente (rotatif)
          newIndex = currentConfigIndex > 0 
            ? currentConfigIndex - 1 
            : configs.length - 1;
          break;
        
        case 'ArrowRight':
        case '+':
        case '=':
          // Configuration suivante (rotatif)
          newIndex = currentConfigIndex < configs.length - 1 
            ? currentConfigIndex + 1 
            : 0;
          break;
        
        default:
          return; // Pas une touche qui nous intéresse
      }

      // Empêcher le comportement par défaut
      event.preventDefault();
      
      // Changer de configuration
      if (newIndex !== currentConfigIndex) {
        console.log(`Navigation clavier: ${event.key} -> config ${newIndex}`);
        setCurrentConfigIndex(newIndex);
        saveToLocalStorage(); // Sauvegarder automatiquement
      }
    };

    // Ajouter l'écouteur d'événements
    document.addEventListener('keydown', handleKeyDown);

    // Nettoyer l'écouteur d'événements
    return () => {
      document.removeEventListener('keydown', handleKeyDown);
    };
  }, [configs.length, currentConfigIndex, setCurrentConfigIndex, saveToLocalStorage]);

  return {
    currentConfigIndex,
    totalConfigs: configs.length
  };
}