mirror of
https://github.com/supabase/supabase.git
synced 2026-10-11 12:25:05 +03:00
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES ## What kind of change does this PR introduce? - Add copy markdown as a keyboard extension - Add copy json as keyboard extension - Add download CSV as keyboard extension All extensions can be toggled off ## Demo https://github.com/user-attachments/assets/c3390b0a-8350-4708-9aeb-7b73aa3b423c --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
57 lines
1.7 KiB
TypeScript
57 lines
1.7 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; shift?: 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; shift?: boolean }
|
|
): void
|
|
function useHotKey(
|
|
callback: (e: KeyboardEvent) => void,
|
|
key: string,
|
|
dependenciesOrOptions?: unknown[] | { enabled?: boolean; shift?: boolean },
|
|
options?: { enabled?: boolean; shift?: boolean }
|
|
): void {
|
|
// Determine which overload was called
|
|
const isDepsArray = Array.isArray(dependenciesOrOptions)
|
|
const resolvedOptions = isDepsArray ? options : dependenciesOrOptions
|
|
const enabled = resolvedOptions?.enabled ?? true
|
|
const shift = resolvedOptions?.shift ?? false
|
|
|
|
const enabledRef = useLatest(enabled)
|
|
const callbackRef = useLatest(callback)
|
|
const keyRef = useLatest(key)
|
|
const shiftRef = useLatest(shift)
|
|
|
|
useEffect(() => {
|
|
function handler(e: KeyboardEvent) {
|
|
if (!enabledRef.current) return
|
|
if (
|
|
(e.metaKey || e.ctrlKey) &&
|
|
e.key.toLowerCase() === keyRef.current.toLowerCase() &&
|
|
!e.altKey &&
|
|
(shiftRef.current ? e.shiftKey : !e.shiftKey)
|
|
) {
|
|
callbackRef.current(e)
|
|
}
|
|
}
|
|
|
|
window.addEventListener('keydown', handler, true)
|
|
return () => {
|
|
window.removeEventListener('keydown', handler, true)
|
|
}
|
|
}, [callbackRef, enabledRef, keyRef, shiftRef])
|
|
}
|
|
|
|
export { useHotKey }
|