Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

5.3 KB
Raw Blame
import { createSignal, onCleanup, type Accessor } from 'solid-js';
import { useNavigate } from '@solidjs/router';
import {
  buildDockerPath,
  buildKubernetesPath,
  buildProxmoxPath,
  buildStandalonePath,
  buildTrueNASPath,
  buildVmwarePath,
} from '@/routing/resourceLinks';
import {
  createEmptyPlatformNavigationVisibility,
  filterPlatformNavigationShortcuts,
  type PlatformNavigationVisibility,
} from '@/features/platformNavigation/platformNavigationModel';
import { focusActiveTypeToSearch } from '@/hooks/useTypeToSearch';

type KeyboardShortcutsOptions = {
  enabled?: Accessor<boolean>;
  isShortcutsOpen?: Accessor<boolean>;
  isCommandPaletteOpen?: Accessor<boolean>;
  onOpenShortcuts?: () => void;
  onCloseShortcuts?: () => void;
  onToggleShortcuts?: () => void;
  onOpenCommandPalette?: () => void;
  onCloseCommandPalette?: () => void;
  onToggleCommandPalette?: () => void;
  onFocusSearch?: () => boolean | void;
  platformVisibility?: Accessor<PlatformNavigationVisibility>;
  // Patrol navigation exists only while AI is enabled and configured; an
  // absent accessor keeps the shortcut for callers that do not gate it.
  patrolVisible?: Accessor<boolean>;
};

const isEditableTarget = (target: EventTarget | null): boolean => {
  if (!target || !(target instanceof HTMLElement)) return false;
  const tag = target.tagName.toLowerCase();
  if (tag === 'input' || tag === 'textarea' || tag === 'select') return true;
  if (target.isContentEditable) return true;
  if (target.getAttribute('role') === 'textbox') return true;
  return false;
};

export function useKeyboardShortcuts(options: KeyboardShortcutsOptions = {}) {
  const navigate = useNavigate();
  const [awaitingSecondKey, setAwaitingSecondKey] = createSignal(false);
  let awaitingTimeout: number | undefined;

  const clearAwaiting = () => {
    if (awaitingTimeout !== undefined) {
      window.clearTimeout(awaitingTimeout);
      awaitingTimeout = undefined;
    }
    setAwaitingSecondKey(false);
  };

  const startAwaiting = () => {
    clearAwaiting();
    setAwaitingSecondKey(true);
    awaitingTimeout = window.setTimeout(() => {
      setAwaitingSecondKey(false);
      awaitingTimeout = undefined;
    }, 1000);
  };

  const focusSearch = () => {
    const handled = options.onFocusSearch?.();
    if (handled) return;
    focusActiveTypeToSearch({ selectText: true });
  };

  const openShortcuts = () => {
    if (options.onToggleShortcuts) {
      options.onToggleShortcuts();
      return;
    }
    options.onOpenShortcuts?.();
  };

  const openCommandPalette = () => {
    if (options.onToggleCommandPalette) {
      options.onToggleCommandPalette();
      return;
    }
    options.onOpenCommandPalette?.();
  };

  const getRoutes = (): Record<string, string> => {
    const platformVisibility =
      options.platformVisibility?.() ?? createEmptyPlatformNavigationVisibility();
    const infrastructureRoutes = filterPlatformNavigationShortcuts(
      {
        proxmox: { key: 'p', route: buildProxmoxPath() },
        docker: { key: 'd', route: buildDockerPath() },
        kubernetes: { key: 'k', route: buildKubernetesPath() },
        truenas: { key: 'n', route: buildTrueNASPath() },
        vmware: { key: 'v', route: buildVmwarePath() },
        standalone: { key: 's', route: buildStandalonePath() },
      },
      platformVisibility,
    );
    return {
      ...infrastructureRoutes,
      a: '/alerts',
      ...(options.patrolVisible?.() === false ? {} : { r: '/patrol' }),
      t: '/settings',
    };
  };

  const handleKeyDown = (e: KeyboardEvent) => {
    if (options.enabled && !options.enabled()) {
      return;
    }

    const shortcutsOpen = options.isShortcutsOpen?.() ?? false;
    const paletteOpen = options.isCommandPaletteOpen?.() ?? false;

    if (e.key === 'Escape') {
      if (awaitingSecondKey()) {
        clearAwaiting();
      }
      if (shortcutsOpen) {
        options.onCloseShortcuts?.();
      }
      if (paletteOpen) {
        options.onCloseCommandPalette?.();
      }
      return;
    }

    if (shortcutsOpen || paletteOpen) {
      return;
    }

    const key = e.key.toLowerCase();

    if ((e.metaKey || e.ctrlKey) && key === 'k') {
      e.preventDefault();
      openCommandPalette();
      return;
    }

    if (isEditableTarget(e.target)) {
      return;
    }

    if (key === 'g' && !awaitingSecondKey() && !e.metaKey && !e.ctrlKey && !e.altKey) {
      if (!e.repeat) {
        startAwaiting();
      }
      return;
    }

    if (awaitingSecondKey()) {
      clearAwaiting();
      const route = getRoutes()[key];
      if (route) {
        e.preventDefault();
        navigate(route);
      }
      return;
    }

    if (key === '/' && !e.metaKey && !e.ctrlKey && !e.altKey) {
      e.preventDefault();
      focusSearch();
      return;
    }

    if ((e.metaKey || e.ctrlKey) && key === 'k') {
      e.preventDefault();
      openCommandPalette();
      return;
    }

    if (e.key === '?') {
      e.preventDefault();
      openShortcuts();
    }
  };

  if (typeof document !== 'undefined') {
    document.addEventListener('keydown', handleKeyDown);
    onCleanup(() => {
      document.removeEventListener('keydown', handleKeyDown);
      if (awaitingTimeout !== undefined) {
        window.clearTimeout(awaitingTimeout);
      }
    });
  }

  return {
    awaitingSecondKey,
  };
}

export default useKeyboardShortcuts;