mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 17:35:10 +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? - Brand new hook APIs for registering shortcuts using tanstack hotkeys - Support for command menu injection when shortcut is added <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Centralized keyboard shortcuts system with per‑shortcut registration and per‑user enable/disable preferences stored locally * Added a "Copy results as Markdown" shortcut (Mod+Shift+M) * Shortcuts can be surfaced in the Command Menu with a visual shortcut badge for discoverability * **Documentation** * Legacy keyboard shortcut hooks marked as deprecated and documentation updated to point to the new shortcut API <!-- end of auto-generated comment: release notes by coderabbit.ai -->
52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
import { LOCAL_STORAGE_KEYS } from 'common'
|
|
import { useCallback } from 'react'
|
|
|
|
import type { ShortcutId } from './registry'
|
|
import { DisabledShortcuts } from './types'
|
|
import { useLocalStorageQuery } from '@/hooks/misc/useLocalStorage'
|
|
|
|
const STORAGE_KEY = LOCAL_STORAGE_KEYS.SHORTCUT_STORAGE_KEY
|
|
|
|
const DEFAULT_DISABLED: DisabledShortcuts = {}
|
|
|
|
export function useShortcutPreferences() {
|
|
const [disabled, setDisabled] = useLocalStorageQuery<DisabledShortcuts>(
|
|
STORAGE_KEY,
|
|
DEFAULT_DISABLED
|
|
)
|
|
|
|
const setShortcutEnabled = useCallback(
|
|
(id: ShortcutId, enabled: boolean) => {
|
|
setDisabled((prev) => {
|
|
if (enabled) {
|
|
const { [id]: _removed, ...rest } = prev
|
|
return rest
|
|
}
|
|
return { ...prev, [id]: true }
|
|
})
|
|
},
|
|
[setDisabled]
|
|
)
|
|
|
|
const resetShortcut = useCallback(
|
|
(id: ShortcutId) => {
|
|
setDisabled((prev) => {
|
|
const { [id]: _removed, ...rest } = prev
|
|
return rest
|
|
})
|
|
},
|
|
[setDisabled]
|
|
)
|
|
|
|
const resetAllShortcuts = useCallback(() => {
|
|
setDisabled(DEFAULT_DISABLED)
|
|
}, [setDisabled])
|
|
|
|
return {
|
|
disabled,
|
|
setShortcutEnabled,
|
|
resetShortcut,
|
|
resetAllShortcuts,
|
|
}
|
|
}
|