useOperationQueueShortcuts.ts 2.1 KB

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