useOperationQueueShortcuts.ts65 lines · main
| 1 | // @ts-nocheck |
| 2 | import { useQueryClient } from '@tanstack/react-query' |
| 3 | |
| 4 | import { useOperationQueueActions } from './useOperationQueueActions' |
| 5 | import { useIsQueueOperationsEnabled } from '@/components/interfaces/Account/Preferences/useDashboardSettings' |
| 6 | import { tableRowKeys } from '@/data/table-rows/keys' |
| 7 | import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' |
| 8 | import { SHORTCUT_IDS } from '@/state/shortcuts/registry' |
| 9 | import { useShortcut } from '@/state/shortcuts/useShortcut' |
| 10 | import { useTableEditorStateSnapshot } from '@/state/table-editor' |
| 11 | |
| 12 | /** |
| 13 | * Hook that provides keyboard shortcuts for the operation queue. |
| 14 | * |
| 15 | * Shortcuts: |
| 16 | * - Cmd/Ctrl + S: Save all pending changes |
| 17 | * - Cmd/Ctrl + .: Toggle the operation queue side panel |
| 18 | * |
| 19 | * These shortcuts are registered on the capture phase to ensure they fire |
| 20 | * before the data grid handles the keyboard event. |
| 21 | */ |
| 22 | export function useOperationQueueShortcuts() { |
| 23 | const queryClient = useQueryClient() |
| 24 | const { data: project } = useSelectedProjectQuery() |
| 25 | const isQueueOperationsEnabled = useIsQueueOperationsEnabled() |
| 26 | const snap = useTableEditorStateSnapshot() |
| 27 | const { handleSave } = useOperationQueueActions() |
| 28 | |
| 29 | const isSaving = snap.operationQueue.status === 'saving' |
| 30 | const hasOperations = snap.hasPendingOperations |
| 31 | const isEnabled = isQueueOperationsEnabled && hasOperations |
| 32 | |
| 33 | useShortcut( |
| 34 | SHORTCUT_IDS.OPERATION_QUEUE_SAVE, |
| 35 | () => { |
| 36 | if (!isSaving && hasOperations) { |
| 37 | handleSave() |
| 38 | } |
| 39 | }, |
| 40 | { enabled: isEnabled } |
| 41 | ) |
| 42 | |
| 43 | useShortcut( |
| 44 | SHORTCUT_IDS.OPERATION_QUEUE_TOGGLE, |
| 45 | () => { |
| 46 | snap.toggleViewOperationQueue() |
| 47 | }, |
| 48 | { enabled: isEnabled } |
| 49 | ) |
| 50 | |
| 51 | useShortcut( |
| 52 | SHORTCUT_IDS.OPERATION_QUEUE_UNDO, |
| 53 | () => { |
| 54 | const tableIdLatestOperation = snap.operationQueue.operations.at(-1)?.tableId |
| 55 | snap.undoLatestOperation() |
| 56 | |
| 57 | if (project && tableIdLatestOperation) { |
| 58 | queryClient.invalidateQueries({ |
| 59 | queryKey: tableRowKeys.tableRowsAndCount(project.ref, tableIdLatestOperation), |
| 60 | }) |
| 61 | } |
| 62 | }, |
| 63 | { enabled: isEnabled } |
| 64 | ) |
| 65 | } |