Skip to content

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

Feat/sort via column header (#35139)

Jonathan Summers-Muir62d00873c5bd
diff --git a/apps/studio/components/grid/components/menu/ColumnMenu.tsx b/apps/studio/components/grid/components/menu/ColumnMenu.tsx
index 5ad77c71d28..10e3bc140ab 100644
--- a/apps/studio/components/grid/components/menu/ColumnMenu.tsx
+++ b/apps/studio/components/grid/components/menu/ColumnMenu.tsx
@@ -1,15 +1,18 @@
-import { ChevronDown, Edit, Lock, Trash, Unlock } from 'lucide-react'
+import type { Sort } from 'components/grid/types'
+import { ArrowDown, ArrowUp, ChevronDown, Edit, Lock, Trash, Unlock } from 'lucide-react'
import type { CalculatedColumn } from 'react-data-grid'
+import { useTableSort } from 'components/grid/hooks/useTableSort'
import { useTableEditorStateSnapshot } from 'state/table-editor'
import { useTableEditorTableStateSnapshot } from 'state/table-editor-table'
import {
Button,
+ cn,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
+ DropdownMenuSeparator,
DropdownMenuTrigger,
- Separator,
Tooltip,
TooltipContent,
TooltipTrigger,
@@ -23,8 +26,10 @@ interface ColumnMenuProps {
const ColumnMenu = ({ column, isEncrypted }: ColumnMenuProps) => {
const tableEditorSnap = useTableEditorStateSnapshot()
const snap = useTableEditorTableStateSnapshot()
+ const { sorts, addOrUpdateSort, removeSort } = useTableSort()
const columnKey = column.key
+ const columnName = column.name as string
function onFreezeColumn() {
snap.freezeColumn(columnKey)
@@ -48,21 +53,65 @@ const ColumnMenu = ({ column, isEncrypted }: ColumnMenuProps) => {
}
}
+ function onSortColumn(ascending: boolean) {
+ if (!columnKey) return
+ const currentSort = sorts.find((s) => s.column === columnKey)
+
+ if (currentSort && currentSort.ascending === ascending) {
+ // Clicked the currently active sort: Remove it
+ removeSort(columnKey)
+ } else {
+ // Clicked the inactive sort or column wasn't sorted: Add or update it
+ addOrUpdateSort(columnKey, ascending)
+ }
+ }
+
function renderMenu() {
+ const currentSort: Sort | undefined = sorts.find((s) => s.column === columnKey)
+
return (
<>
+ <DropdownMenuItem
+ className={cn(
+ 'space-x-2',
+ currentSort?.ascending && 'bg-surface-200 dark:bg-surface-400 text-foreground'
+ )}
+ onClick={() => onSortColumn(true)}
+ >
+ <ArrowUp size={14} strokeWidth={currentSort?.ascending ? 3 : 1.5} />
+ <span>Sort Ascending</span>
+ </DropdownMenuItem>
+ <DropdownMenuItem
+ className={cn(
+ 'space-x-2',
+ currentSort &&
+ !currentSort.ascending &&
+ 'bg-surface-200 dark:bg-surface-400 text-foreground'
+ )}
+ onClick={() => onSortColumn(false)}
+ >
+ <ArrowDown size={14} strokeWidth={currentSort && !currentSort.ascending ? 3 : 1.5} />
+ <span>Sort Descending</span>
+ </DropdownMenuItem>
{snap.editable && (
- <Tooltip>
- <TooltipTrigger asChild className={`${isEncrypted ? 'opacity-50' : ''}`}>
- <DropdownMenuItem className="space-x-2" onClick={onEditColumn} disabled={isEncrypted}>
- <Edit size={14} />
- <p>Edit column</p>
- </DropdownMenuItem>
- </TooltipTrigger>
- {isEncrypted && (
- <TooltipContent side="bottom">Encrypted columns cannot be edited</TooltipContent>
- )}
- </Tooltip>
+ <>
+ <DropdownMenuSeparator />
+ <Tooltip>
+ <TooltipTrigger asChild className={`${isEncrypted ? 'opacity-50' : ''}`}>
+ <DropdownMenuItem
+ className="space-x-2"
+ onClick={onEditColumn}
+ disabled={isEncrypted}
+ >
+ <Edit size={14} strokeWidth={1.5} />
+ <span>Edit column</span>
+ </DropdownMenuItem>
+ </TooltipTrigger>
+ {isEncrypted && (
+ <TooltipContent side="bottom">Encrypted columns cannot be edited</TooltipContent>
+ )}
+ </Tooltip>
+ </>
)}
<DropdownMenuItem
className="space-x-2"
@@ -70,22 +119,22 @@ const ColumnMenu = ({ column, isEncrypted }: ColumnMenuProps) => {
>
{column.frozen ? (
<>
- <Unlock size={14} />
- <p>Unfreeze column</p>
+ <Unlock size={14} strokeWidth={1.5} />
+ <span>Unfreeze column</span>
</>
) : (
<>
- <Lock size={14} />
- <p>Freeze column</p>
+ <Lock size={14} strokeWidth={1.5} />
+ <span>Freeze column</span>
</>
)}
</DropdownMenuItem>
{snap.editable && (
<>
- <Separator />
+ <DropdownMenuSeparator />
<DropdownMenuItem className="space-x-2" onClick={onDeleteColumn}>
- <Trash size={14} stroke="red" />
- <p>Delete column</p>
+ <Trash size={14} className="text-destructive" />
+ <span>Delete column</span>
</DropdownMenuItem>
</>
)}
diff --git a/apps/studio/components/grid/hooks/useTableSort.ts b/apps/studio/components/grid/hooks/useTableSort.ts
index 9cbd1c70a7a..f114d9f1753 100644
--- a/apps/studio/components/grid/hooks/useTableSort.ts
+++ b/apps/studio/components/grid/hooks/useTableSort.ts
@@ -42,9 +42,58 @@ export function useTableSort() {
[snap, setParams, saveSortsAndTriggerSideEffects]
)
+ /**
+ * Adds a new sort for a column or updates the direction of an existing one.
+ * New sorts are added to the beginning of the array (highest precedence).
+ * If the column already exists, its `ascending` direction is updated.
+ * Calls `onApplySorts` to update URL parameters and trigger side effects.
+ *
+ * @param columnKey The key/name of the column to sort.
+ * @param ascending The sort direction (true for ascending, false for descending).
+ */
+ const addOrUpdateSort = useCallback(
+ (columnKey: string, ascending: boolean) => {
+ if (!tableName || !columnKey) return
+
+ // Use the derived 'sorts' state from the hook
+ const existingSortIndex = sorts.findIndex((s) => s.column === columnKey)
+ let newSorts = [...sorts] // Create a mutable copy
+
+ if (existingSortIndex !== -1) {
+ // Column already exists in sorts: Update the existing sort (toggle handled by removeSort)
+ newSorts[existingSortIndex] = { ...newSorts[existingSortIndex], ascending: ascending }
+ } else {
+ // Column doesn't exist in sorts: Add it to the beginning
+ newSorts.unshift({ table: tableName, column: columnKey, ascending: ascending })
+ }
+
+ onApplySorts(newSorts)
+ },
+ [tableName, sorts, onApplySorts] // Depend on derived sorts and callback
+ )
+
+ /**
+ * Removes a sort criterion for a specific column.
+ * Calls `onApplySorts` with the filtered array to update URL parameters and trigger side effects.
+ *
+ * @param columnKey The key/name of the column to remove from sorting.
+ */
+ const removeSort = useCallback(
+ (columnKey: string) => {
+ if (!tableName || !columnKey) return
+
+ // Use the derived 'sorts' state from the hook
+ const newSorts = sorts.filter((s) => s.column !== columnKey)
+ onApplySorts(newSorts)
+ },
+ [tableName, sorts, onApplySorts] // Depend on derived sorts and callback
+ )
+
return {
sorts,
urlSorts,
onApplySorts,
+ addOrUpdateSort,
+ removeSort,
}
}
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector.tsx
index 92eab845314..dae748ae714 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/ForeignRowSelector/ForeignRowSelector.tsx
@@ -7,8 +7,7 @@ import { useParams } from 'common'
import RefreshButton from 'components/grid/components/header/RefreshButton'
import { FilterPopoverPrimitive } from 'components/grid/components/header/filter/FilterPopoverPrimitive'
import { SortPopoverPrimitive } from 'components/grid/components/header/sort/SortPopoverPrimitive'
-import type { Filter } from 'components/grid/types'
-import { Sort } from 'components/grid/types'
+import type { Filter, Sort } from 'components/grid/types'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import { useTableEditorQuery } from 'data/table-editor/table-editor-query'
import { useTableRowsQuery } from 'data/table-rows/table-rows-query'
diff --git a/apps/studio/pages/project/[ref]/database/tables/index.tsx b/apps/studio/pages/project/[ref]/database/tables/index.tsx
index c56310f5d36..1b7075d19dc 100644
--- a/apps/studio/pages/project/[ref]/database/tables/index.tsx
+++ b/apps/studio/pages/project/[ref]/database/tables/index.tsx
@@ -1,6 +1,6 @@
import { useState } from 'react'
-
import { PostgresTable } from '@supabase/postgres-meta'
+
import { useParams } from 'common'
import { TableList } from 'components/interfaces/Database'
import { SidePanelEditor } from 'components/interfaces/TableGridEditor'