Skip to content

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

Fix tables not loading in table editor on [id] route after new refactor

Joshen Lim633f296eeeef
diff --git a/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx b/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx
index 9791b01dfa1..19178db155f 100644
--- a/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx
+++ b/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx
@@ -4,7 +4,7 @@ import { observer } from 'mobx-react-lite'
import { PostgresTable } from '@supabase/postgres-meta'
import { PermissionAction } from '@supabase/shared-types/out/constants'
-import { checkPermissions, useStore } from 'hooks'
+import { checkPermissions, useParams, useStore } from 'hooks'
import Error from 'components/ui/Error'
import ProjectLayout from '../ProjectLayout/ProjectLayout'
import TableEditorMenu from './TableEditorMenu'
@@ -30,13 +30,14 @@ const TableEditorLayout: FC<Props> = ({
children,
}) => {
const { vault, meta, ui } = useStore()
+ const { id, type } = useParams()
const { isInitialized, isLoading, error } = meta.tables
const [loaded, setLoaded] = useState<boolean>(isInitialized)
const canReadTables = checkPermissions(PermissionAction.TENANT_SQL_ADMIN_READ, 'tables')
const vaultExtension = meta.extensions.byId('supabase_vault')
- const isEnabled = vaultExtension !== undefined && vaultExtension?.installed_version !== null
+ const isVaultEnabled = vaultExtension !== undefined && vaultExtension?.installed_version !== null
useEffect(() => {
if (ui.selectedProject?.ref) {
@@ -45,6 +46,12 @@ const TableEditorLayout: FC<Props> = ({
meta.policies.load()
meta.publications.load()
meta.extensions.load()
+
+ // [Joshen] pg-meta doesn't support loading views nor foreign tables by a specific ID yet
+ // Separately, Alaister and I chatted that perhaps we leverage on pg-catalog's pg-class
+ // table directly, so we can fetch the schema of tables/views/foreign-tables in one call
+ // rather than trying to discern if the ID is a view, or foreign table (refer to below)
+ meta.views.load()
meta.foreignTables.load()
}
}, [ui.selectedProject?.ref])
@@ -57,10 +64,20 @@ const TableEditorLayout: FC<Props> = ({
}, [ui.selectedProject?.ref, selectedSchema])
useEffect(() => {
- if (isEnabled) {
+ if (ui.selectedProject?.ref && id) {
+ // [Joshen] This is a little silly, but because fetching tables/views/foreign-tables
+ // are all through different endpoints, we need to discern them
+ if (type !== 'view' && type !== 'foreign') {
+ meta.tables.loadById(Number(id))
+ }
+ }
+ }, [ui.selectedProject?.ref, id])
+
+ useEffect(() => {
+ if (isVaultEnabled) {
vault.load()
}
- }, [ui.selectedProject?.ref, isEnabled])
+ }, [ui.selectedProject?.ref, isVaultEnabled])
useEffect(() => {
let cancel = false
diff --git a/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx b/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx
index a832e6643f4..be30f4d81f6 100644
--- a/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx
+++ b/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx
@@ -1,5 +1,4 @@
import { FC, useState } from 'react'
-import { useRouter } from 'next/router'
import Link from 'next/link'
import { partition } from 'lodash'
import { observer } from 'mobx-react-lite'
@@ -22,9 +21,8 @@ import * as Tooltip from '@radix-ui/react-tooltip'
import { PermissionAction } from '@supabase/shared-types/out/constants'
import { PostgresSchema, PostgresTable } from '@supabase/postgres-meta'
-import Base64 from 'lib/base64'
import { SchemaView } from 'types'
-import { checkPermissions, useStore } from 'hooks'
+import { checkPermissions, useParams, useStore } from 'hooks'
import ProductMenuItem from 'components/ui/ProductMenu/ProductMenuItem'
interface Props {
@@ -45,8 +43,7 @@ const TableEditorMenu: FC<Props> = ({
onDuplicateTable = () => {},
}) => {
const { meta, ui } = useStore()
- const router = useRouter()
- const { id } = router.query
+ const { id } = useParams()
const projectRef = ui.selectedProject?.ref
const schemas: PostgresSchema[] = meta.schemas.list()
@@ -324,12 +321,11 @@ const TableEditorMenu: FC<Props> = ({
/>
{filteredViews.map((view: SchemaView) => {
- const viewId = Base64.encode(JSON.stringify(view))
- const isActive = id === viewId
+ const isActive = Number(id) === view.id
return (
- <Link key={viewId} href={`/project/${projectRef}/editor/${viewId}`}>
+ <Link key={view.id} href={`/project/${projectRef}/editor/${view.id}?type=view`}>
<a>
- <Menu.Item key={viewId} rounded active={isActive}>
+ <Menu.Item key={view.id} rounded active={isActive}>
<div className="flex justify-between">
<p className="truncate">{view.name}</p>
</div>
@@ -359,7 +355,10 @@ const TableEditorMenu: FC<Props> = ({
{filteredForeignTables.map((table: Partial<PostgresTable>) => {
const isActive = Number(id) === table.id
return (
- <Link key={table.id} href={`/project/${projectRef}/editor/${table.id}`}>
+ <Link
+ key={table.id}
+ href={`/project/${projectRef}/editor/${table.id}?type=foreign`}
+ >
<a>
<Menu.Item key={table.id} rounded active={isActive}>
<div className="flex justify-between">
diff --git a/studio/pages/project/[ref]/editor/[id].tsx b/studio/pages/project/[ref]/editor/[id].tsx
index 2b191faf79a..32893d76bec 100644
--- a/studio/pages/project/[ref]/editor/[id].tsx
+++ b/studio/pages/project/[ref]/editor/[id].tsx
@@ -7,17 +7,16 @@ import { isUndefined, isNaN } from 'lodash'
import { Alert, Button, Checkbox, IconExternalLink, Modal } from 'ui'
import { PostgresTable, PostgresColumn } from '@supabase/postgres-meta'
-import Base64 from 'lib/base64'
-import { tryParseJson } from 'lib/helpers'
-import { useStore, withAuth, useUrlState } from 'hooks'
+import { useStore, withAuth, useUrlState, useParams } from 'hooks'
import { Dictionary } from 'components/grid'
import { TableEditorLayout } from 'components/layouts'
import { TableGridEditor } from 'components/interfaces'
import ConfirmationModal from 'components/ui/ConfirmationModal'
+import { SchemaView } from 'types'
const TableEditorPage: NextPage = () => {
const router = useRouter()
- const { id }: any = router.query
+ const { id } = useParams()
const [_, setParams] = useUrlState({ arrayKeys: ['filter', 'sort'] })
const { meta, ui } = useStore()
@@ -37,13 +36,17 @@ const TableEditorPage: NextPage = () => {
const projectRef = ui.selectedProject?.ref
const tables: PostgresTable[] = meta.tables.list()
+ const views: SchemaView[] = meta.views.list()
const foreignTables: Partial<PostgresTable>[] = meta.foreignTables.list()
const selectedTable = !isNaN(Number(id))
? // @ts-ignore
- tables.concat(foreignTables).find((table) => table.id === Number(id))
- : id !== undefined
- ? tryParseJson(Base64.decode(id))
+ tables
+ // @ts-ignore
+ .concat(views)
+ // @ts-ignore
+ .concat(foreignTables)
+ .find((table) => table.id === Number(id))
: undefined
useEffect(() => {