Files
supabase/apps/studio/components/grid/hooks/useOperationQueueShortcuts.ts
Ali Waseem ea1b95d29b feature: batch and save operations for cell content updates (#42120)
* added initial queue operations and feature flag

* updated types

* added dirty state tracking on columns

* updated queue operations

* updated operation types and queue

* updated spacing

* removed on cancel

* updated to support saving

* updated to include eye details

* updated spacing for orders

* updated to support shortcuts

* added feature preview

* updated to unify queue methods

* added key generation

* used unique keys rather than random uuid

* updated based on code review

* operation key

* updated handle cancel

* updated remove operation button

* updated views for toast

* updated logic to support optimistic updates

* updated types

* code cleanup: remove LLM slop

* updated PR bug

* updated preview for logout

* updated based on code review

* removed use effect as it was causing problems

* fixed toast mounting away from sql editor

* removed toast for dedicated action bar

* cleaned up logic

* updated queue operations

* renamed method

* updated name for types

* updated comment

* fixed code rabbit solution

* added check for changed column

* added tests
2026-01-28 06:54:30 -07:00

69 lines
1.7 KiB
TypeScript

import { useCallback, useEffect } from 'react'
import { detectOS } from 'lib/helpers'
export function getModKey() {
const os = detectOS()
return os === 'macos' ? '⌘' : 'Ctrl+'
}
interface UseOperationQueueShortcutsOptions {
enabled: boolean
onSave: () => void
onTogglePanel: () => void
isSaving?: boolean
hasOperations?: boolean
}
/**
* Hook that provides keyboard shortcuts for the operation queue.
*
* Shortcuts:
* - Cmd/Ctrl + S: Save all pending changes
* - Cmd/Ctrl + .: Toggle the operation queue side panel
*
* These shortcuts are registered on the capture phase to ensure they fire
* before the data grid handles the keyboard event.
*/
export function useOperationQueueShortcuts({
enabled,
onSave,
onTogglePanel,
isSaving = false,
hasOperations = true,
}: UseOperationQueueShortcutsOptions) {
const os = detectOS()
const modKey = os === 'macos' ? '⌘' : 'Ctrl+'
const handleKeyDown = useCallback(
(event: KeyboardEvent) => {
const isMod = os === 'macos' ? event.metaKey : event.ctrlKey
if (isMod && event.key === 's') {
event.preventDefault()
event.stopPropagation()
if (!isSaving && hasOperations) {
onSave()
}
} else if (isMod && event.key === '.') {
event.preventDefault()
event.stopPropagation()
onTogglePanel()
}
},
[os, isSaving, hasOperations, onSave, onTogglePanel]
)
// Use capture phase to intercept events before the grid handles them
useEffect(() => {
if (enabled) {
window.addEventListener('keydown', handleKeyDown, true)
return () => {
window.removeEventListener('keydown', handleKeyDown, true)
}
}
}, [enabled, handleKeyDown])
return { modKey }
}