Skip to content

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

feat(design-system): add TableHeadSort to table (#40900)

Francesco Sansalvadore316d26cf992a
diff --git a/apps/design-system/content/docs/components/table.mdx b/apps/design-system/content/docs/components/table.mdx
index ab2cc6bf5dc..6dcafe2b601 100644
--- a/apps/design-system/content/docs/components/table.mdx
+++ b/apps/design-system/content/docs/components/table.mdx
@@ -48,6 +48,24 @@ import {
</Table>
```
+## Sortable Columns
+
+Use `TableHeadSort` inside `TableHead` to enable column sorting.
+
+| Prop | Type | Description |
+| -------------- | -------------------------- | ------------------------------------------------------------------ |
+| `column` | `string` | Unique identifier for the column |
+| `currentSort` | `string` | Current sort state in format `"column:order"` (e.g., `"name:asc"`) |
+| `onSortChange` | `(column: string) => void` | Callback fired when column header is clicked |
+
+```tsx
+<TableHead>
+ <TableHeadSort column="name" currentSort={sort} onSortChange={handleSortChange}>
+ Name
+ </TableHeadSort>
+</TableHead>
+```
+
## Data Table
You can use the `<Table />` component to build more complex data tables. Combine it with [@tanstack/react-table](https://tanstack.com/table/v8) to create tables with sorting, filtering and pagination.
diff --git a/apps/studio/components/interfaces/Auth/OAuthApps/OAuthAppsList.tsx b/apps/studio/components/interfaces/Auth/OAuthApps/OAuthAppsList.tsx
index 8e77b34ab23..9db1788627d 100644
--- a/apps/studio/components/interfaces/Auth/OAuthApps/OAuthAppsList.tsx
+++ b/apps/studio/components/interfaces/Auth/OAuthApps/OAuthAppsList.tsx
@@ -1,8 +1,8 @@
import type { OAuthClient } from '@supabase/supabase-js'
import { Edit, MoreVertical, Plus, RotateCw, Search, Trash, X } from 'lucide-react'
import Link from 'next/link'
-import { parseAsBoolean, useQueryState } from 'nuqs'
-import { useRef, useState } from 'react'
+import { parseAsBoolean, parseAsStringLiteral, useQueryState } from 'nuqs'
+import { useMemo, useRef, useState } from 'react'
import { toast } from 'sonner'
import { useParams } from 'common'
@@ -30,6 +30,7 @@ import {
TableCell,
TableHead,
TableHeader,
+ TableHeadSort,
TableRow,
} from 'ui'
import { Admonition } from 'ui-patterns/admonition'
@@ -44,6 +45,21 @@ import {
OAUTH_APP_REGISTRATION_TYPE_OPTIONS,
} from './oauthApps.utils'
+const OAUTH_APPS_SORT_VALUES = [
+ 'name:asc',
+ 'name:desc',
+ 'client_type:asc',
+ 'client_type:desc',
+ 'registration_type:asc',
+ 'registration_type:desc',
+ 'created_at:asc',
+ 'created_at:desc',
+] as const
+
+type OAuthAppsSort = (typeof OAUTH_APPS_SORT_VALUES)[number]
+type OAuthAppsSortColumn = OAuthAppsSort extends `${infer Column}:${string}` ? Column : unknown
+type OAuthAppsSortOrder = OAuthAppsSort extends `${string}:${infer Order}` ? Order : unknown
+
export const OAuthAppsList = () => {
const { ref: projectRef } = useParams()
const { data: authConfig, isLoading: isAuthConfigLoading } = useAuthConfigQuery({ projectRef })
@@ -108,12 +124,40 @@ export const OAuthAppsList = () => {
const [filterString, setFilterString] = useState<string>('')
- const filteredOAuthApps = filterOAuthApps({
- apps: oAuthApps,
- searchString: filterString,
- registrationTypes: filteredRegistrationTypes,
- clientTypes: filteredClientTypes,
- })
+ const [sort, setSort] = useQueryState(
+ 'sort',
+ parseAsStringLiteral<OAuthAppsSort>(OAUTH_APPS_SORT_VALUES).withDefault('name:asc')
+ )
+
+ const filteredAndSortedOAuthApps = useMemo(() => {
+ const filtered = filterOAuthApps({
+ apps: oAuthApps,
+ searchString: filterString,
+ registrationTypes: filteredRegistrationTypes,
+ clientTypes: filteredClientTypes,
+ })
+
+ const [sortCol, sortOrder] = sort.split(':') as [OAuthAppsSortColumn, OAuthAppsSortOrder]
+ const orderMultiplier = sortOrder === 'asc' ? 1 : -1
+
+ return filtered.sort((a, b) => {
+ if (sortCol === 'name') {
+ return a.client_name.localeCompare(b.client_name) * orderMultiplier
+ }
+ if (sortCol === 'client_type') {
+ return a.client_type.localeCompare(b.client_type) * orderMultiplier
+ }
+ if (sortCol === 'registration_type') {
+ return a.registration_type.localeCompare(b.registration_type) * orderMultiplier
+ }
+ if (sortCol === 'created_at') {
+ return (
+ (new Date(a.created_at).getTime() - new Date(b.created_at).getTime()) * orderMultiplier
+ )
+ }
+ return 0
+ })
+ }, [oAuthApps, filterString, filteredRegistrationTypes, filteredClientTypes, sort])
const hasActiveFilters =
filterString.length > 0 ||
@@ -126,6 +170,22 @@ export const OAuthAppsList = () => {
setFilteredClientTypes([])
}
+ const handleSortChange = (column: OAuthAppsSortColumn) => {
+ const [currentCol, currentOrder] = sort.split(':') as [OAuthAppsSortColumn, OAuthAppsSortOrder]
+ if (currentCol === column) {
+ // Cycle through: asc -> desc -> no sort (default)
+ if (currentOrder === 'asc') {
+ setSort(`${column}:desc` as OAuthAppsSort)
+ } else {
+ // Reset to default sort (name:asc)
+ setSort('name:asc')
+ }
+ } else {
+ // New column, start with asc
+ setSort(`${column}:asc` as OAuthAppsSort)
+ }
+ }
+
if (isAuthConfigLoading || (isOAuthServerEnabled && isLoading)) {
return <GenericSkeletonLoader />
}
@@ -223,26 +283,54 @@ export const OAuthAppsList = () => {
<Table containerProps={{ stickyLastColumn: true }}>
<TableHeader>
<TableRow>
- <TableHead>Name</TableHead>
+ <TableHead>
+ <TableHeadSort column="name" currentSort={sort} onSortChange={handleSortChange}>
+ Name
+ </TableHeadSort>
+ </TableHead>
<TableHead>Client ID</TableHead>
- <TableHead>Client Type</TableHead>
- <TableHead>Registration Type</TableHead>
- <TableHead>Created</TableHead>
+ <TableHead>
+ <TableHeadSort
+ column="client_type"
+ currentSort={sort}
+ onSortChange={handleSortChange}
+ >
+ Client Type
+ </TableHeadSort>
+ </TableHead>
+ <TableHead>
+ <TableHeadSort
+ column="registration_type"
+ currentSort={sort}
+ onSortChange={handleSortChange}
+ >
+ Registration Type
+ </TableHeadSort>
+ </TableHead>
+ <TableHead>
+ <TableHeadSort
+ column="created_at"
+ currentSort={sort}
+ onSortChange={handleSortChange}
+ >
+ Created
+ </TableHeadSort>
+ </TableHead>
<TableHead className="w-8 px-0">
<div className="!bg-200 px-4 w-full h-full flex items-center border-l @[944px]:border-l-0" />
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
- {filteredOAuthApps.length === 0 && (
+ {filteredAndSortedOAuthApps.length === 0 && (
<TableRow>
<TableCell colSpan={6}>
<p className="text-foreground-lighter">No OAuth apps found</p>
</TableCell>
</TableRow>
)}
- {filteredOAuthApps.length > 0 &&
- filteredOAuthApps.map((app) => (
+ {filteredAndSortedOAuthApps.length > 0 &&
+ filteredAndSortedOAuthApps.map((app) => (
<TableRow key={app.client_id} className="w-full">
<TableCell className="w-48 max-w-48 flex" title={app.client_name}>
<Button
diff --git a/packages/ui/src/components/shadcn/ui/table.tsx b/packages/ui/src/components/shadcn/ui/table.tsx
index fefe8967216..d56edc29c5a 100644
--- a/packages/ui/src/components/shadcn/ui/table.tsx
+++ b/packages/ui/src/components/shadcn/ui/table.tsx
@@ -1,5 +1,6 @@
import type { ComponentProps } from 'react'
import * as React from 'react'
+import { ArrowDown, ArrowUp, ChevronsUpDown } from 'lucide-react'
import { cn } from '../../../lib/utils/cn'
import { ShadowScrollArea } from '../../ShadowScrollArea'
@@ -12,7 +13,11 @@ const Table = React.forwardRef<HTMLTableElement, TableProps>(
({ className, containerProps, ...props }, ref) => {
return (
<ShadowScrollArea {...containerProps}>
- <table ref={ref} className={cn('w-full caption-bottom text-sm', className)} {...props} />
+ <table
+ ref={ref}
+ className={cn('group/table w-full caption-bottom text-sm', className)}
+ {...props}
+ />
</ShadowScrollArea>
)
}
@@ -76,6 +81,72 @@ const TableHead = React.forwardRef<
))
TableHead.displayName = 'TableHead'
+interface TableHeadSortProps<TColumn extends string = string> {
+ column: TColumn
+ currentSort: string
+ onSortChange: (column: TColumn) => void
+ children: React.ReactNode
+ className?: string
+}
+
+function TableHeadSort<TColumn extends string = string>({
+ column,
+ currentSort,
+ onSortChange,
+ children,
+ className,
+}: TableHeadSortProps<TColumn>) {
+ const [currentCol, currentOrder] = currentSort.split(':')
+ const isActive = currentCol === column
+ const isAsc = isActive && currentOrder === 'asc'
+ const isDesc = isActive && currentOrder === 'desc'
+
+ const getSortIcon = () => {
+ const baseIconClass = 'w-3 h-3 absolute inset-0'
+
+ return (
+ <>
+ <ArrowUp
+ className={cn(
+ baseIconClass,
+ 'transition-transform',
+ isAsc ? 'translate-y-0' : 'translate-y-full'
+ )}
+ />
+ <ArrowDown
+ className={cn(
+ baseIconClass,
+ 'transition-transform',
+ isDesc ? 'translate-y-0' : '-translate-y-full'
+ )}
+ />
+ <ChevronsUpDown
+ className={cn(
+ baseIconClass,
+ 'transition-opacity opacity-80 md:opacity-40',
+ !isActive ? 'group-hover/table-head-sort:opacity-80' : '!opacity-0'
+ )}
+ />
+ </>
+ )
+ }
+
+ return (
+ <button
+ type="button"
+ className={cn(
+ 'group/table-head-sort heading-meta whitespace-nowrap flex items-center gap-1 cursor-pointer select-none !bg-transparent border-none p-0 w-full text-left',
+ className
+ )}
+ onClick={() => onSortChange(column)}
+ >
+ {children}
+ <div className="w-3 h-3 relative overflow-hidden">{getSortIcon()}</div>
+ </button>
+ )
+}
+TableHeadSort.displayName = 'TableHeadSort'
+
const TableCell = React.forwardRef<
HTMLTableCellElement,
React.TdHTMLAttributes<HTMLTableCellElement>
@@ -96,4 +167,14 @@ const TableCaption = React.forwardRef<
))
TableCaption.displayName = 'TableCaption'
-export { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow }
+export {
+ Table,
+ TableBody,
+ TableCaption,
+ TableCell,
+ TableFooter,
+ TableHead,
+ TableHeader,
+ TableHeadSort,
+ TableRow,
+}