Skip to content

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

adds table sort selector

Alaister Young53ad215087ce
diff --git a/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx b/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx
index 088984868c0..a20b2f3240c 100644
--- a/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx
+++ b/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx
@@ -5,7 +5,9 @@ import { observer } from 'mobx-react-lite'
import {
Button,
Dropdown,
+ IconCheck,
IconChevronDown,
+ IconChevronsDown,
IconCopy,
IconEdit,
IconLoader,
@@ -22,7 +24,7 @@ import * as Tooltip from '@radix-ui/react-tooltip'
import { PermissionAction } from '@supabase/shared-types/out/constants'
import type { PostgresSchema } from '@supabase/postgres-meta'
-import { checkPermissions, useStore, useParams } from 'hooks'
+import { checkPermissions, useStore, useParams, useLocalStorage } from 'hooks'
import ProductMenuItem from 'components/ui/ProductMenu/ProductMenuItem'
import { useEntityTypesQuery } from 'data/entity-types/entity-types-infinite-query'
import { ENTITY_TYPE } from 'data/entity-types/entity-type-constants'
@@ -51,6 +53,10 @@ const TableEditorMenu = ({
const { id } = useParams()
const [searchText, setSearchText] = useState<string>('')
+ const [sort, setSort] = useLocalStorage<'alphabetical' | 'grouped-alphabetical'>(
+ 'table-editor-sort',
+ 'alphabetical'
+ )
const { project } = useProjectContext()
const {
@@ -68,6 +74,7 @@ const TableEditorMenu = ({
connectionString: project?.connectionString,
schema: selectedSchema,
search: searchText || undefined,
+ sort,
},
{
keepPreviousData: true,
@@ -251,9 +258,74 @@ const TableEditorMenu = ({
<p style={{ fontVariantNumeric: 'tabular-nums' }}>({totalCount})</p>
)}
</div>
- <button className="cursor-pointer" onClick={refreshTables}>
- <IconRefreshCw className={isRefetching ? 'animate-spin' : ''} size={14} />
- </button>
+
+ <div className="flex gap-3 items-center">
+ <Dropdown
+ size="small"
+ side="bottom"
+ align="start"
+ style={{ zIndex: 1 }}
+ overlay={[
+ <Dropdown.Item
+ key="alphabetical"
+ icon={
+ sort === 'alphabetical' ? (
+ <IconCheck size="tiny" />
+ ) : (
+ <div className="w-[14px] h-[14px]" />
+ )
+ }
+ onClick={() => {
+ setSort('alphabetical')
+ }}
+ >
+ Alphabetical
+ </Dropdown.Item>,
+ <Dropdown.Item
+ key="grouped-alphabetical"
+ icon={
+ sort === 'grouped-alphabetical' ? (
+ <IconCheck size="tiny" />
+ ) : (
+ <div className="w-[14px] h-[14px]" />
+ )
+ }
+ onClick={() => {
+ setSort('grouped-alphabetical')
+ }}
+ >
+ Grouped Alphabetical
+ </Dropdown.Item>,
+ ]}
+ >
+ <Tooltip.Root delayDuration={0}>
+ <Tooltip.Trigger asChild>
+ <div className="text-scale-900 transition-colors hover:text-scale-1200">
+ <IconChevronsDown size={18} strokeWidth={1} />
+ </div>
+ </Tooltip.Trigger>
+
+ <Tooltip.Content side="top">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
+ 'border border-scale-200',
+ ].join(' ')}
+ >
+ <span className="text-xs">Sort By</span>
+ </div>
+ </Tooltip.Content>
+ </Tooltip.Root>
+ </Dropdown>
+
+ <button
+ className="cursor-pointer text-scale-900 transition-colors hover:text-scale-1200"
+ onClick={refreshTables}
+ >
+ <IconRefreshCw className={isRefetching ? 'animate-spin' : ''} size={14} />
+ </button>
+ </div>
</div>
</>
}
diff --git a/studio/data/entity-types/entity-types-infinite-query.ts b/studio/data/entity-types/entity-types-infinite-query.ts
index d1e304e810f..155c63ac5a3 100644
--- a/studio/data/entity-types/entity-types-infinite-query.ts
+++ b/studio/data/entity-types/entity-types-infinite-query.ts
@@ -9,6 +9,7 @@ export type EntityTypesVariables = {
search?: string
limit?: number
page?: number
+ sort?: 'alphabetical' | 'grouped-alphabetical'
} & Pick<ExecuteSqlVariables, 'connectionString'>
export type EntityTypesResponse = {
@@ -26,9 +27,13 @@ export async function getEntityTypes(
search,
limit = 100,
page = 0,
+ sort = 'alphabetical',
}: EntityTypesVariables,
signal?: AbortSignal
) {
+ const innerOrderBy = sort === 'alphabetical' ? `c.relkind asc` : `c.relkind asc, c.relname asc`
+ const outerOrderBy = sort === 'alphabetical' ? `r.name asc` : `r.type asc, r.name asc`
+
const sql = /* SQL */ `
with records as (
select
@@ -54,7 +59,7 @@ export async function getEntityTypes(
)
and nc.nspname = '${schema}'
${search ? `and c.relname ilike '%${search}%'` : ''}
- order by c.relkind asc, c.relname asc
+ order by ${innerOrderBy}
limit ${limit}
offset ${page * limit}
)
@@ -68,7 +73,7 @@ export async function getEntityTypes(
'type', r.type,
'comment', r.comment
)
- order by r.name asc
+ order by ${outerOrderBy}
), '[]'::jsonb),
'count', coalesce(min(r.count), 0)
) "data"
@@ -98,6 +103,7 @@ export const useEntityTypesQuery = <TData = EntityTypesData>(
schema = 'public',
search,
limit = 100,
+ sort,
}: Omit<EntityTypesVariables, 'page'>,
{
enabled = true,
@@ -105,10 +111,10 @@ export const useEntityTypesQuery = <TData = EntityTypesData>(
}: UseInfiniteQueryOptions<EntityTypesData, EntityTypesError, TData> = {}
) =>
useInfiniteQuery<EntityTypesData, EntityTypesError, TData>(
- entityTypeKeys.list(projectRef, search),
+ entityTypeKeys.list(projectRef, { schema, search, sort, limit }),
({ signal, pageParam }) =>
getEntityTypes(
- { projectRef, connectionString, schema, search, limit, page: pageParam },
+ { projectRef, connectionString, schema, search, limit, page: pageParam, sort },
signal
),
{
diff --git a/studio/data/entity-types/keys.ts b/studio/data/entity-types/keys.ts
index affef0d559c..d56b82cb4ba 100644
--- a/studio/data/entity-types/keys.ts
+++ b/studio/data/entity-types/keys.ts
@@ -1,4 +1,11 @@
export const entityTypeKeys = {
- list: (projectRef: string | undefined, search?: string) =>
- ['projects', projectRef, 'entity-types', ...(search ? [{ search }] : [])] as const,
+ list: (
+ projectRef: string | undefined,
+ params?: {
+ schema?: string
+ search?: string
+ sort?: 'alphabetical' | 'grouped-alphabetical'
+ limit?: number
+ }
+ ) => ['projects', projectRef, 'entity-types', ...(params ? [params] : [])] as const,
}
diff --git a/studio/hooks/index.ts b/studio/hooks/index.ts
index 5995d22e059..bcfb55c58e7 100644
--- a/studio/hooks/index.ts
+++ b/studio/hooks/index.ts
@@ -4,5 +4,6 @@ export * from './misc/useOptimisticSqlSnippetCreate'
export * from './misc/useImmutableValue'
export * from './misc/useParams'
export * from './misc/useCheckPermissions'
+export * from './misc/useLocalStorage'
export * from './deprecated'
export * from './ui'
diff --git a/studio/hooks/misc/useLocalStorage.ts b/studio/hooks/misc/useLocalStorage.ts
new file mode 100644
index 00000000000..3c428565287
--- /dev/null
+++ b/studio/hooks/misc/useLocalStorage.ts
@@ -0,0 +1,44 @@
+// Reference: https://usehooks.com/useLocalStorage/
+
+import { useState } from 'react'
+
+export function useLocalStorage<T>(key: string, initialValue: T) {
+ // State to store our value
+ // Pass initial state function to useState so logic is only executed once
+ const [storedValue, setStoredValue] = useState<T>(() => {
+ if (typeof window === 'undefined') {
+ return initialValue
+ }
+
+ try {
+ // Get from local storage by key
+ const item = window.localStorage.getItem(key)
+ // Parse stored json or if none return initialValue
+ return item ? JSON.parse(item) : initialValue
+ } catch (error) {
+ // If error also return initialValue
+ console.log(error)
+ return initialValue
+ }
+ })
+
+ // Return a wrapped version of useState's setter function that ...
+ // ... persists the new value to localStorage.
+ const setValue = (value: T | ((val: T) => T)) => {
+ try {
+ // Allow value to be a function so we have same API as useState
+ const valueToStore = value instanceof Function ? value(storedValue) : value
+ // Save state
+ setStoredValue(valueToStore)
+ // Save to local storage
+ if (typeof window !== 'undefined') {
+ window.localStorage.setItem(key, JSON.stringify(valueToStore))
+ }
+ } catch (error) {
+ // A more advanced implementation would handle the error case
+ console.log(error)
+ }
+ }
+
+ return [storedValue, setValue] as const
+}