Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Revert "Fix queue operations stale state + refactor to reuse existing… (#42290)

Ali Waseem8b2dcaa3525c
diff --git a/apps/studio/components/grid/components/footer/operations/SaveQueueActionBar.tsx b/apps/studio/components/grid/components/footer/operations/SaveQueueActionBar.tsx
index f2467268475..65be243d127 100644
--- a/apps/studio/components/grid/components/footer/operations/SaveQueueActionBar.tsx
+++ b/apps/studio/components/grid/components/footer/operations/SaveQueueActionBar.tsx
@@ -1,23 +1,21 @@
-import { useOperationQueueActions } from 'components/grid/hooks/useOperationQueueActions'
-import { useOperationQueueShortcuts } from 'components/grid/hooks/useOperationQueueShortcuts'
-import { useIsQueueOperationsEnabled } from 'components/interfaces/App/FeaturePreview/FeaturePreviewContext'
-import { AnimatePresence, motion } from 'framer-motion'
import { Eye } from 'lucide-react'
+import { AnimatePresence, motion } from 'framer-motion'
import { createPortal } from 'react-dom'
-import { useTableEditorStateSnapshot } from 'state/table-editor'
import { Button } from 'ui'
-import { getModKeyLabel } from '@/lib/helpers'
-
-const modKey = getModKeyLabel()
+import {
+ useOperationQueueShortcuts,
+ getModKey,
+} from 'components/grid/hooks/useOperationQueueShortcuts'
+import { useIsQueueOperationsEnabled } from 'components/interfaces/App/FeaturePreview/FeaturePreviewContext'
+import { useTableEditorStateSnapshot } from 'state/table-editor'
+import { useOperationQueueActions } from 'components/grid/hooks/useOperationQueueActions'
export const SaveQueueActionBar = () => {
const snap = useTableEditorStateSnapshot()
const isQueueOperationsEnabled = useIsQueueOperationsEnabled()
const { handleSave } = useOperationQueueActions()
- useOperationQueueShortcuts()
-
const operationCount = snap.operationQueue.operations.length
const isSaving = snap.operationQueue.status === 'saving'
const isOperationQueuePanelOpen = snap.sidePanel?.type === 'operation-queue'
@@ -25,6 +23,16 @@ export const SaveQueueActionBar = () => {
const isVisible =
isQueueOperationsEnabled && snap.hasPendingOperations && !isOperationQueuePanelOpen
+ useOperationQueueShortcuts({
+ enabled: isQueueOperationsEnabled && snap.hasPendingOperations,
+ onSave: handleSave,
+ onTogglePanel: () => snap.onViewOperationQueue(),
+ isSaving,
+ hasOperations: operationCount > 0,
+ })
+
+ const modKey = getModKey()
+
const content = (
<AnimatePresence>
{isVisible && (
@@ -41,7 +49,7 @@ export const SaveQueueActionBar = () => {
</span>
<div className="flex items-center gap-3">
<button
- onClick={() => snap.toggleViewOperationQueue()}
+ onClick={() => snap.onViewOperationQueue()}
className="text-foreground-light hover:text-foreground transition-colors flex items-center"
aria-label="View Details"
>
diff --git a/apps/studio/components/grid/components/header/sort/SortPopoverPrimitive.tsx b/apps/studio/components/grid/components/header/sort/SortPopoverPrimitive.tsx
index bbb8df4e78b..8a48e3cf10d 100644
--- a/apps/studio/components/grid/components/header/sort/SortPopoverPrimitive.tsx
+++ b/apps/studio/components/grid/components/header/sort/SortPopoverPrimitive.tsx
@@ -1,14 +1,15 @@
import { THRESHOLD_COUNT } from '@supabase/pg-meta/src/query/table-row-query'
import { keepPreviousData } from '@tanstack/react-query'
+import { isEqual } from 'lodash'
+import { ChevronDown, List } from 'lucide-react'
+import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
+
import { useParams } from 'common'
import { useTableFilter } from 'components/grid/hooks/useTableFilter'
import type { Sort } from 'components/grid/types'
import { InlineLink } from 'components/ui/InlineLink'
import { useTableRowsCountQuery } from 'data/table-rows/table-rows-count-query'
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
-import { isEqual } from 'lodash'
-import { ChevronDown, List } from 'lucide-react'
-import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import {
type RoleImpersonationState,
useRoleImpersonationStateSnapshot,
@@ -22,7 +23,6 @@ import {
Popover_Shadcn_,
} from 'ui'
import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
-
import { DropdownControl } from '../../common/DropdownControl'
import SortRow from './SortRow'
@@ -278,10 +278,8 @@ export const SortPopoverPrimitive = ({
const hasSortNotPK = localSorts.some(
(x) => !snap.table.columns.find((y) => x.column === y.name)?.isPrimaryKey
)
- if (hasSortNotPK) return setShowWarning(true)
- }
-
- onSelectApplySorts()
+ if (hasSortNotPK) setShowWarning(true)
+ } else onSelectApplySorts()
}}
>
Apply sorting
diff --git a/apps/studio/components/grid/hooks/useOperationQueueActions.ts b/apps/studio/components/grid/hooks/useOperationQueueActions.ts
index c7ba7c17a8e..a55f45bd52e 100644
--- a/apps/studio/components/grid/hooks/useOperationQueueActions.ts
+++ b/apps/studio/components/grid/hooks/useOperationQueueActions.ts
@@ -1,9 +1,10 @@
import { useQueryClient } from '@tanstack/react-query'
+import { useCallback } from 'react'
+import { toast } from 'sonner'
+
import { tableRowKeys } from 'data/table-rows/keys'
import { useOperationQueueSaveMutation } from 'data/table-rows/operation-queue-save-mutation'
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
-import { useCallback } from 'react'
-import { toast } from 'sonner'
import { useGetImpersonatedRoleState } from 'state/role-impersonation-state'
import { useTableEditorStateSnapshot } from 'state/table-editor'
import { QueuedOperation } from 'state/table-editor-operation-queue.types'
@@ -29,7 +30,6 @@ export function useOperationQueueActions(options: UseOperationQueueActionsOption
useOperationQueueSaveMutation({
onSuccess: () => {
snap.clearQueue()
- snap.closeSidePanel()
toast.success('Changes saved successfully')
onSaveSuccess?.()
},
@@ -40,10 +40,12 @@ export function useOperationQueueActions(options: UseOperationQueueActionsOption
})
const isSaving = snap.operationQueue.status === 'saving' || isMutationPending
- const operations = snap.operationQueue.operations as readonly QueuedOperation[]
const handleSave = useCallback(() => {
- if (!project || operations.length === 0) return
+ if (!project) return
+
+ const operations = snap.operationQueue.operations as readonly QueuedOperation[]
+ if (operations.length === 0) return
snap.setQueueStatus('saving')
@@ -53,7 +55,7 @@ export function useOperationQueueActions(options: UseOperationQueueActionsOption
operations,
roleImpersonationState: getImpersonatedRoleState(),
})
- }, [snap, project, operations, saveOperationQueue, getImpersonatedRoleState])
+ }, [snap, project, saveOperationQueue, getImpersonatedRoleState])
const handleCancel = useCallback(() => {
// Get unique table IDs from the queue before clearing
diff --git a/apps/studio/components/grid/hooks/useOperationQueueShortcuts.ts b/apps/studio/components/grid/hooks/useOperationQueueShortcuts.ts
index d1503ea6750..74dc8d606f8 100644
--- a/apps/studio/components/grid/hooks/useOperationQueueShortcuts.ts
+++ b/apps/studio/components/grid/hooks/useOperationQueueShortcuts.ts
@@ -1,7 +1,19 @@
-import { useOperationQueueActions } from './useOperationQueueActions'
-import { useIsQueueOperationsEnabled } from '@/components/interfaces/App/FeaturePreview/FeaturePreviewContext'
-import { useHotKey } from '@/hooks/ui/useHotKey'
-import { useTableEditorStateSnapshot } from '@/state/table-editor'
+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.
@@ -13,34 +25,44 @@ import { useTableEditorStateSnapshot } from '@/state/table-editor'
* These shortcuts are registered on the capture phase to ensure they fire
* before the data grid handles the keyboard event.
*/
-export function useOperationQueueShortcuts() {
- const isQueueOperationsEnabled = useIsQueueOperationsEnabled()
- const snap = useTableEditorStateSnapshot()
- const { handleSave } = useOperationQueueActions()
-
- const isSaving = snap.operationQueue.status === 'saving'
- const hasOperations = snap.hasPendingOperations
- const isEnabled = isQueueOperationsEnabled && hasOperations
-
- useHotKey(
- (event) => {
- event.preventDefault()
- event.stopPropagation()
- if (!isSaving && hasOperations) {
- handleSave()
+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()
}
},
- 's',
- { enabled: isEnabled }
+ [os, isSaving, hasOperations, onSave, onTogglePanel]
)
- useHotKey(
- (event) => {
- event.preventDefault()
- event.stopPropagation()
- snap.toggleViewOperationQueue()
- },
- '.',
- { enabled: isEnabled }
- )
+ // 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 }
}
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/OperationQueueSidePanel/OperationItem.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/OperationQueueSidePanel/OperationItem.tsx
index c9476493de6..3ee11ebb1bc 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/OperationQueueSidePanel/OperationItem.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/OperationQueueSidePanel/OperationItem.tsx
@@ -1,12 +1,12 @@
import { useQueryClient } from '@tanstack/react-query'
+import { X } from 'lucide-react'
+import { Button } from 'ui'
+
import { tableRowKeys } from 'data/table-rows/keys'
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
-import { X } from 'lucide-react'
import { useTableEditorStateSnapshot } from 'state/table-editor'
-
-import { formatOperationItemValue } from './OperationQueueSidePanel.utils'
-import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { EditCellContentPayload } from '@/state/table-editor-operation-queue.types'
+import { formatOperationItemValue } from './OperationQueueSidePanel.utils'
interface OperationItemProps {
operationId: string
@@ -50,18 +50,17 @@ export const OperationItem = ({ operationId, tableId, content }: OperationItemPr
<div className="text-xs text-foreground font-mono">{fullTableName}</div>
<div className="text-sm text-foreground-muted mt-0.5">
<span className="font-medium text-foreground">{columnName}</span>
- <span className="text-foreground-muted mx-2">•</span>
+ <span className="text-foreground-muted mx-2">·</span>
<span className="text-foreground text-xs">where {whereClause}</span>
</div>
</div>
- <ButtonTooltip
+ <Button
type="text"
size="tiny"
icon={<X size={14} />}
onClick={handleDelete}
- className="shrink-0 w-7"
+ className="shrink-0"
aria-label="Remove operation"
- tooltip={{ content: { side: 'bottom', text: 'Remove operation' } }}
/>
</div>
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/OperationQueueSidePanel/OperationQueueSidePanel.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/OperationQueueSidePanel/OperationQueueSidePanel.tsx
index 9bfa1251e06..5a5754ad2cb 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/OperationQueueSidePanel/OperationQueueSidePanel.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/OperationQueueSidePanel/OperationQueueSidePanel.tsx
@@ -1,9 +1,9 @@
import { useOperationQueueActions } from 'components/grid/hooks/useOperationQueueActions'
+import { useOperationQueueShortcuts } from 'components/grid/hooks/useOperationQueueShortcuts'
import { useTableEditorStateSnapshot } from 'state/table-editor'
import { Button, SidePanel } from 'ui'
import { OperationList } from './OperationList'
-import { getModKeyLabel } from '@/lib/helpers'
import { QueuedOperation } from '@/state/table-editor-operation-queue.types'
interface OperationQueueSidePanelProps {
@@ -11,8 +11,6 @@ interface OperationQueueSidePanelProps {
closePanel: () => void
}
-const modKey = getModKeyLabel()
-
export const OperationQueueSidePanel = ({ visible, closePanel }: OperationQueueSidePanelProps) => {
const snap = useTableEditorStateSnapshot()
@@ -23,6 +21,14 @@ export const OperationQueueSidePanel = ({ visible, closePanel }: OperationQueueS
onCancelSuccess: closePanel,
})
+ const { modKey } = useOperationQueueShortcuts({
+ enabled: visible,
+ onSave: handleSave,
+ onTogglePanel: closePanel,
+ isSaving,
+ hasOperations: operations.length > 0,
+ })
+
return (
<SidePanel
size="large"
@@ -31,7 +37,7 @@ export const OperationQueueSidePanel = ({ visible, closePanel }: OperationQueueS
header={
<div className="flex items-center justify-between w-full">
<div className="flex flex-col gap-1">
- <span>Pending changes</span>
+ <span>Pending Changes</span>
<span className="text-xs text-foreground-light">
{operations.length} operation{operations.length !== 1 ? 's' : ''}
</span>
diff --git a/apps/studio/hooks/ui/useHotKey.ts b/apps/studio/hooks/ui/useHotKey.ts
index 41e470ca11c..d8daff563c3 100644
--- a/apps/studio/hooks/ui/useHotKey.ts
+++ b/apps/studio/hooks/ui/useHotKey.ts
@@ -1,49 +1,25 @@
import { useEffect } from 'react'
-import { useLatest } from 'react-use'
-// [Joshen] Refactor: Remove dependencies, and just make this into a single definition
-function useHotKey(
+export function useHotKey(
callback: (e: KeyboardEvent) => void,
key: string,
- options?: { enabled?: 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 }
-): void
-function useHotKey(
- callback: (e: KeyboardEvent) => void,
- key: string,
- dependenciesOrOptions?: unknown[] | { enabled?: boolean },
+ dependencies: any[] = [],
options?: { enabled?: boolean }
): void {
- // Determine which overload was called
- const isDepsArray = Array.isArray(dependenciesOrOptions)
- const resolvedOptions = isDepsArray ? options : dependenciesOrOptions
- const enabled = resolvedOptions?.enabled ?? true
-
- const enabledRef = useLatest(enabled)
- const callbackRef = useLatest(callback)
- const keyRef = useLatest(key)
+ const enabled = options?.enabled ?? true
useEffect(() => {
+ if (!enabled) return
+
function handler(e: KeyboardEvent) {
- if (!enabledRef.current) return
- if ((e.metaKey || e.ctrlKey) && e.key === keyRef.current && !e.altKey && !e.shiftKey) {
- callbackRef.current(e)
+ if ((e.metaKey || e.ctrlKey) && e.key === key && !e.altKey && !e.shiftKey) {
+ callback(e)
}
}
- window.addEventListener('keydown', handler, true)
+ window.addEventListener('keydown', handler)
return () => {
- window.removeEventListener('keydown', handler, true)
+ window.removeEventListener('keydown', handler)
}
- }, [callbackRef, enabledRef, keyRef])
+ }, [key, enabled, ...dependencies])
}
-
-export { useHotKey }
diff --git a/apps/studio/lib/helpers.ts b/apps/studio/lib/helpers.ts
index 58ffb5c303d..a82e0c00dc5 100644
--- a/apps/studio/lib/helpers.ts
+++ b/apps/studio/lib/helpers.ts
@@ -182,11 +182,6 @@ export const detectOS = () => {
}
}
-export const getModKeyLabel = () => {
- const os = detectOS()
- return os === 'macos' ? '⌘' : 'Ctrl+'
-}
-
/**
* Convert a list of tables to SQL
* @param t - The list of tables
diff --git a/apps/studio/state/table-editor.tsx b/apps/studio/state/table-editor.tsx
index 0bd5be50c42..a45f9672c44 100644
--- a/apps/studio/state/table-editor.tsx
+++ b/apps/studio/state/table-editor.tsx
@@ -1,4 +1,7 @@
import type { PostgresColumn } from '@supabase/postgres-meta'
+import { PropsWithChildren, createContext, useContext } from 'react'
+import { proxy, useSnapshot } from 'valtio'
+
import { useConstant } from 'common'
import type { SupaRow } from 'components/grid/types'
import {
@@ -8,16 +11,15 @@ import {
import { ForeignKey } from 'components/interfaces/TableGridEditor/SidePanelEditor/ForeignKeySelector/ForeignKeySelector.types'
import type { EditValue } from 'components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.types'
import type { TableField } from 'components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.types'
-import { PropsWithChildren, createContext, useContext } from 'react'
import type { Dictionary } from 'types'
-import { proxy, useSnapshot } from 'valtio'
import {
NewQueuedOperation,
+ QueuedOperationType,
+ type EditCellContentPayload,
type OperationQueueState,
- type QueueStatus,
type QueuedOperation,
- QueuedOperationType,
+ type QueueStatus,
} from './table-editor-operation-queue.types'
export const TABLE_EDITOR_DEFAULT_ROWS_PER_PAGE = 100
@@ -201,14 +203,10 @@ export const createTableEditorState = () => {
sidePanel: { type: 'csv-import', file },
}
},
- toggleViewOperationQueue: () => {
- if (state.ui.open === 'side-panel' && state.ui.sidePanel.type === 'operation-queue') {
- state.closeSidePanel()
- } else {
- state.ui = {
- open: 'side-panel',
- sidePanel: { type: 'operation-queue' },
- }
+ onViewOperationQueue: () => {
+ state.ui = {
+ open: 'side-panel',
+ sidePanel: { type: 'operation-queue' },
}
},