Skip to content

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

Init valtio store for table editor

Joshen Lim6ba64e277deb
diff --git a/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx b/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx
index 3d3c15330ea..6035b2dfef5 100644
--- a/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx
+++ b/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx
@@ -15,6 +15,7 @@ import useEntityType from 'hooks/misc/useEntityType'
import Connecting from 'components/ui/Loading/Loading'
import useLatest from 'hooks/misc/useLatest'
import useTableRowsPrefetchWrapper from './TableEditorLayout.utils'
+import { useIsTableLoaded, useTableEditorStateSnapshot } from 'state/table-editor'
export interface TableEditorLayoutProps {
selectedSchema?: string
@@ -36,9 +37,10 @@ const TableEditorLayout = ({
}: PropsWithChildren<TableEditorLayoutProps>) => {
const { vault, meta, ui } = useStore()
const router = useRouter()
- const { id: _id } = useParams()
+ const { ref, id: _id } = useParams()
const id = _id ? Number(_id) : undefined
+ const snap = useTableEditorStateSnapshot()
const canReadTables = checkPermissions(PermissionAction.TENANT_SQL_ADMIN_READ, 'tables')
const vaultExtension = meta.extensions.byId('supabase_vault')
@@ -54,15 +56,10 @@ const TableEditorLayout = ({
}
}, [ui.selectedProject?.ref])
- const [loadedIds, setLoadedIds] = useState<Set<number>>(() => new Set())
- const isLoaded = id !== undefined && loadedIds.has(id)
+ const isLoaded = useIsTableLoaded(ref, id)
const entity = useEntityType(id, function onNotFound(id) {
- setLoadedIds((loadedIds) => {
- const newLoadedIds = new Set(loadedIds)
- newLoadedIds.add(id)
- return newLoadedIds
- })
+ if (ref) snap.addLoadedId(ref, id)
})
const prefetch = useLatest(useTableRowsPrefetchWrapper())
@@ -91,32 +88,23 @@ const TableEditorLayout = ({
loadTable()
?.then(async (entity: any) => {
await prefetch.current(entity)
-
return entity
})
.then((entity: any) => {
- if (mounted) {
- setLoadedIds((loadedIds) => {
- const newLoadedIds = new Set(loadedIds)
- newLoadedIds.add(entity.id ?? entity?.id)
- return newLoadedIds
- })
+ if (mounted && ref) {
+ snap.addLoadedId(ref, entity.id)
}
})
.catch(() => {
- if (mounted && entity?.id) {
- setLoadedIds((loadedIds) => {
- const newLoadedIds = new Set(loadedIds)
- newLoadedIds.add(entity.id)
- return newLoadedIds
- })
+ if (mounted && entity?.id && ref) {
+ snap.addLoadedId(ref, entity.id)
}
})
return () => {
mounted = false
}
- }, [entity])
+ }, [entity?.id])
useEffect(() => {
if (isVaultEnabled) {
diff --git a/studio/state/table-editor.ts b/studio/state/table-editor.ts
new file mode 100644
index 00000000000..f95c87cb909
--- /dev/null
+++ b/studio/state/table-editor.ts
@@ -0,0 +1,29 @@
+import { proxy, snapshot, useSnapshot } from 'valtio'
+import { proxySet } from 'valtio/utils'
+
+export const tableEditorState = proxy({
+ loadedIds: {} as {
+ [key: string]: Set<number>
+ },
+ addLoadedId: (ref: string, id: number) => {
+ if (!tableEditorState.loadedIds[ref]) {
+ tableEditorState.loadedIds[ref] = proxySet<number>([])
+ }
+ tableEditorState.loadedIds[ref].add(id)
+ },
+ clearLoadedIdsForProject: (ref: string) => {
+ tableEditorState.loadedIds[ref] = proxySet<number>([])
+ },
+})
+
+export const getTableEditorStateSnapshot = () => snapshot(tableEditorState)
+
+export const useTableEditorStateSnapshot = (options?: Parameters<typeof useSnapshot>[1]) =>
+ useSnapshot(tableEditorState, options)
+
+export const useIsTableLoaded = (ref?: string, id?: number) => {
+ const snap = useTableEditorStateSnapshot()
+
+ if (!ref || !id) return false
+ return snap.loadedIds[ref]?.has(id) ?? false
+}