TorbatYar/frontend/modules/crm/hooks/useCrmKeyboardShortcuts.ts
Mortezakoohjani e140908034 Ship enterprise CRM frontend with real API wiring and thin app routes.
Connect all CRM pages to the BFF and CRM service, add module docs, and mark CRM available in the SuperApp catalog.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-26 20:58:17 +03:30

44 lines
1.2 KiB
TypeScript

"use client";
import { useEffect } from "react";
import { useRouter } from "next/navigation";
import { CRM_SHORTCUTS } from "@/modules/crm/constants/portals";
/** Global keyboard shortcuts for CRM (g + key chord). */
export function useCrmKeyboardShortcuts() {
const router = useRouter();
useEffect(() => {
let pending: string | null = null;
let timer: ReturnType<typeof setTimeout>;
const onKeyDown = (e: KeyboardEvent) => {
if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement) return;
const key = e.key.toLowerCase();
if (pending === "g") {
const chord = `g ${key}`;
const href = CRM_SHORTCUTS[chord];
if (href) {
e.preventDefault();
router.push(href);
}
pending = null;
return;
}
if (key === "g") {
pending = "g";
clearTimeout(timer);
timer = setTimeout(() => {
pending = null;
}, 800);
}
};
window.addEventListener("keydown", onKeyDown);
return () => {
window.removeEventListener("keydown", onKeyDown);
clearTimeout(timer);
};
}, [router]);
}