Files
supabase/apps/studio/hooks/ui/useHotKey.ts
T
Joshen Lim f90f3b4d14 Revert "Revert "Fix queue operations stale state + refactor to reuse existing…" (#42291)
TBD

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* OS-aware modifier-key label shown in UI (⌘ on macOS, Ctrl+ on others)
  * Tooltips added to operation queue actions for improved usability

* **Bug Fixes**
  * Warns before applying non-primary-key sorts on large tables
  * Clarified access error messages for organizations and projects

* **Refactor**
  * Redesigned hotkey handling and operation-queue shortcuts
* Operation queue view toggle and save flow refined (panel auto-closes
after save)

<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-01-30 10:44:42 +08:00

50 lines
1.5 KiB
TypeScript

import { useEffect } from 'react'
import { useLatest } from 'react-use'
// [Joshen] Refactor: Remove dependencies, and just make this into a single definition
function useHotKey(
callback: (e: KeyboardEvent) => void,
key: string,
options?: { enabled?: boolean }
): void
/**
* @deprecated The `dependencies` parameter is deprecated. Use the overload without dependencies instead.
*/
function useHotKey(
callback: (e: KeyboardEvent) => void,
key: string,
dependencies: unknown[],
options?: { enabled?: boolean }
): void
function useHotKey(
callback: (e: KeyboardEvent) => void,
key: string,
dependenciesOrOptions?: unknown[] | { enabled?: boolean },
options?: { enabled?: boolean }
): void {
// Determine which overload was called
const isDepsArray = Array.isArray(dependenciesOrOptions)
const resolvedOptions = isDepsArray ? options : dependenciesOrOptions
const enabled = resolvedOptions?.enabled ?? true
const enabledRef = useLatest(enabled)
const callbackRef = useLatest(callback)
const keyRef = useLatest(key)
useEffect(() => {
function handler(e: KeyboardEvent) {
if (!enabledRef.current) return
if ((e.metaKey || e.ctrlKey) && e.key === keyRef.current && !e.altKey && !e.shiftKey) {
callbackRef.current(e)
}
}
window.addEventListener('keydown', handler, true)
return () => {
window.removeEventListener('keydown', handler, true)
}
}, [callbackRef, enabledRef, keyRef])
}
export { useHotKey }