From 7388eacf74cc75fa5e2e0844ce0eb0443c545758 Mon Sep 17 00:00:00 2001
From: Charis <26616127+charislam@users.noreply.github.com>
Date: Thu, 15 Aug 2024 13:37:38 -0400
Subject: [PATCH] feat: create new table command (#28647)
---
.../App/CommandMenu/CommandMenu.tsx | 2 ++
.../TableEditor/TableEditor.tsx | 9 +++++
.../layouts/AuthLayout/Auth.Commands.tsx | 1 +
.../TableEditor.Commands.tsx | 33 ++++++++++++++++---
apps/studio/hooks/ui/useUrlState.ts | 2 +-
5 files changed, 42 insertions(+), 5 deletions(-)
diff --git a/apps/studio/components/interfaces/App/CommandMenu/CommandMenu.tsx b/apps/studio/components/interfaces/App/CommandMenu/CommandMenu.tsx
index 693647d8de5..7809b0bd9aa 100644
--- a/apps/studio/components/interfaces/App/CommandMenu/CommandMenu.tsx
+++ b/apps/studio/components/interfaces/App/CommandMenu/CommandMenu.tsx
@@ -2,6 +2,7 @@ import { IS_PLATFORM } from 'common'
import { useBranchCommands } from 'components/interfaces/BranchManagement/Branch.Commands'
import { useSnippetCommands } from 'components/layouts/SQLEditorLayout/SqlEditor.Commands'
import { useGenerateSqlCommand } from 'components/interfaces/SqlGenerator/SqlGenerator.Commands'
+import { useProjectLevelTableEditorCommands } from 'components/layouts/TableEditorLayout/TableEditor.Commands'
import { useLayoutNavCommands } from 'components/layouts/useLayoutNavCommands'
import { CommandHeader, CommandInput, CommandList, CommandMenu } from 'ui-patterns/CommandMenu'
import { useChangelogCommand } from 'ui-patterns/CommandMenu/prepackaged/Changelog'
@@ -18,6 +19,7 @@ export default function StudioCommandMenu() {
useGenerateSqlCommand()
useApiKeysCommands()
useApiUrlCommand()
+ useProjectLevelTableEditorCommands()
useProjectSwitchCommand()
useBranchCommands()
useSnippetCommands()
diff --git a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.tsx b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.tsx
index 3b4dabc221d..28e16eeaad8 100644
--- a/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.tsx
+++ b/apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/TableEditor.tsx
@@ -18,6 +18,7 @@ import {
} from 'data/database/foreign-key-constraints-query'
import { useEnumeratedTypesQuery } from 'data/enumerated-types/enumerated-types-query'
import { useIsFeatureEnabled } from 'hooks/misc/useIsFeatureEnabled'
+import { useUrlState } from 'hooks/ui/useUrlState'
import { EXCLUDED_SCHEMAS_WITHOUT_EXTENSIONS } from 'lib/constants/schemas'
import { ExternalLink } from 'lucide-react'
import { useTableEditorStateSnapshot } from 'state/table-editor'
@@ -80,6 +81,14 @@ const TableEditor = ({
const isNewRecord = isUndefined(table)
const realtimeEnabled = useIsFeatureEnabled('realtime:all')
+ const [params, setParams] = useUrlState()
+ useEffect(() => {
+ if (params.create === 'table' && snap.ui.open === 'none') {
+ snap.onAddTable()
+ setParams({ ...params, create: undefined })
+ }
+ }, [snap, params, setParams])
+
const { data: types } = useEnumeratedTypesQuery({
projectRef: project?.ref,
connectionString: project?.connectionString,
diff --git a/apps/studio/components/layouts/AuthLayout/Auth.Commands.tsx b/apps/studio/components/layouts/AuthLayout/Auth.Commands.tsx
index 20f52cd7334..31499fa2390 100644
--- a/apps/studio/components/layouts/AuthLayout/Auth.Commands.tsx
+++ b/apps/studio/components/layouts/AuthLayout/Auth.Commands.tsx
@@ -24,6 +24,7 @@ export function useAuthGotoCommands(options?: CommandOptions) {
{
...options,
deps: [ref],
+ enabled: (options?.enabled ?? true) && ref !== '_',
orderSection: orderCommandSectionsByPriority,
sectionMeta: { priority: 3 },
}
diff --git a/apps/studio/components/layouts/TableEditorLayout/TableEditor.Commands.tsx b/apps/studio/components/layouts/TableEditorLayout/TableEditor.Commands.tsx
index f365ab88d16..bbfc91c5fe3 100644
--- a/apps/studio/components/layouts/TableEditorLayout/TableEditor.Commands.tsx
+++ b/apps/studio/components/layouts/TableEditorLayout/TableEditor.Commands.tsx
@@ -1,11 +1,36 @@
-import { Table } from 'lucide-react'
+import { Table2 } from 'lucide-react'
import { useParams } from 'common'
import { COMMAND_MENU_SECTIONS } from 'components/interfaces/App/CommandMenu/CommandMenu.utils'
import { orderCommandSectionsByPriority } from 'components/interfaces/App/CommandMenu/ordering'
+import { useSelectedProject } from 'hooks/misc/useSelectedProject'
import type { CommandOptions } from 'ui-patterns/CommandMenu'
import { useRegisterCommands } from 'ui-patterns/CommandMenu'
+export function useProjectLevelTableEditorCommands(options?: CommandOptions) {
+ let project = useSelectedProject()
+ const ref = project?.ref || '_'
+
+ useRegisterCommands(
+ COMMAND_MENU_SECTIONS.ACTIONS,
+ [
+ {
+ id: 'create-table',
+ name: 'Create new table',
+ route: `/project/${ref}/editor?create=table`,
+ icon: () =>