[id].tsx69 lines · main
1import { useParams } from 'common'
2import { useEffect } from 'react'
3
4import { TableGridEditor } from '@/components/interfaces/TableGridEditor/TableGridEditor'
5import { DefaultLayout } from '@/components/layouts/DefaultLayout'
6import { EditorBaseLayout } from '@/components/layouts/editors/EditorBaseLayout'
7import { TableEditorLayout } from '@/components/layouts/TableEditorLayout/TableEditorLayout'
8import { TableEditorMenu } from '@/components/layouts/TableEditorLayout/TableEditorMenu'
9import { useTableEditorQuery } from '@/data/table-editor/table-editor-query'
10import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
11import { createTabId, useTabsStateSnapshot } from '@/state/tabs'
12import type { NextPageWithLayout } from '@/types'
13
14const TableEditorPage: NextPageWithLayout = () => {
15 const { id: _id, ref: projectRef } = useParams()
16 const id = _id ? Number(_id) : undefined
17 const store = useTabsStateSnapshot()
18
19 const { data: project } = useSelectedProjectQuery()
20 const { data: selectedTable, isPending: isLoading } = useTableEditorQuery({
21 projectRef: project?.ref,
22 connectionString: project?.connectionString,
23 id,
24 })
25
26 /**
27 * Effect: Creates or updates tab when table is loaded
28 * Runs when:
29 * - selectedTable changes (when a new table is loaded)
30 * - id changes (when URL parameter changes)
31 */
32
33 useEffect(() => {
34 if (selectedTable && projectRef) {
35 const tabId = createTabId(selectedTable.entity_type, { id: selectedTable.id })
36 if (!store.tabsMap[tabId]) {
37 store.addTab({
38 id: tabId,
39 type: selectedTable.entity_type,
40 label: selectedTable.name,
41 metadata: {
42 schema: selectedTable.schema,
43 name: selectedTable.name,
44 tableId: id,
45 },
46 })
47 } else {
48 // If tab already exists, just make it active
49 store.makeTabActive(tabId)
50 }
51 }
52 }, [selectedTable, id, projectRef])
53
54 return <TableGridEditor isLoadingSelectedTable={isLoading} selectedTable={selectedTable} />
55}
56
57TableEditorPage.getLayout = (page) => (
58 <DefaultLayout>
59 <EditorBaseLayout
60 productMenu={<TableEditorMenu />}
61 product="Table Editor"
62 productMenuClassName="overflow-y-hidden"
63 >
64 <TableEditorLayout>{page}</TableEditorLayout>
65 </EditorBaseLayout>
66 </DefaultLayout>
67)
68
69export default TableEditorPage