Skip to content

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

Chore/update tab labels when entering sql or table editor (#35219)

Joshen Lim270935bec89a
diff --git a/apps/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx b/apps/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
index 52b458484e9..15b3be04104 100644
--- a/apps/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
+++ b/apps/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
@@ -1,15 +1,16 @@
import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useDebounce } from '@uidotdev/usehooks'
+import { FilePlus, FolderPlus, Plus, X } from 'lucide-react'
+import { useRouter } from 'next/router'
+import { useEffect, useState } from 'react'
+import { toast } from 'sonner'
+
import { useParams } from 'common'
import { useCheckPermissions } from 'hooks/misc/useCheckPermissions'
import { useLocalStorage } from 'hooks/misc/useLocalStorage'
import { useSelectedProject } from 'hooks/misc/useSelectedProject'
import { LOCAL_STORAGE_KEYS } from 'lib/constants'
import { useProfile } from 'lib/profile'
-import { FilePlus, FolderPlus, Plus, X } from 'lucide-react'
-import { useRouter } from 'next/router'
-import { useEffect, useState } from 'react'
-import { toast } from 'sonner'
import { getAppStateSnapshot } from 'state/app-state'
import { useSqlEditorV2StateSnapshot } from 'state/sql-editor-v2'
import {
@@ -37,8 +38,8 @@ export const SQLEditorMenu = () => {
const { profile } = useProfile()
const project = useSelectedProject()
const { ref } = useParams()
-
const snapV2 = useSqlEditorV2StateSnapshot()
+
const [search, setSearch] = useState('')
const [showSearch, setShowSearch] = useState(false)
const [sort, setSort] = useLocalStorage<'name' | 'inserted_at'>(
diff --git a/apps/studio/components/layouts/SQLEditorLayout/SQLEditorNavV2/SQLEditorNav.tsx b/apps/studio/components/layouts/SQLEditorLayout/SQLEditorNavV2/SQLEditorNav.tsx
index 31a02e6c9fe..cb26ba3cd0c 100644
--- a/apps/studio/components/layouts/SQLEditorLayout/SQLEditorNavV2/SQLEditorNav.tsx
+++ b/apps/studio/components/layouts/SQLEditorLayout/SQLEditorNavV2/SQLEditorNav.tsx
@@ -29,7 +29,7 @@ import {
useSnippetFolders,
useSqlEditorV2StateSnapshot,
} from 'state/sql-editor-v2'
-import { createTabId, getTabsStore, makeTabPermanent, removeTabs } from 'state/tabs'
+import { createTabId, getTabsStore, makeTabPermanent, removeTabs, useTabsStore } from 'state/tabs'
import { SqlSnippets } from 'types'
import { Separator, TreeView } from 'ui'
import {
@@ -40,7 +40,6 @@ import {
InnerSideMenuSeparator,
} from 'ui-patterns'
import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
-import { useSnapshot } from 'valtio'
import SQLEditorLoadingSnippets from './SQLEditorLoadingSnippets'
import { formatFolderResponseForTreeView, getLastItemIds, ROOT_NODE } from './SQLEditorNav.utils'
import { SQLEditorTreeViewItem } from './SQLEditorTreeViewItem'
@@ -61,7 +60,7 @@ export const SQLEditorNav = ({ sort = 'inserted_at' }: SQLEditorNavProps) => {
const snapV2 = useSqlEditorV2StateSnapshot()
const tabStore = getTabsStore(projectRef)
- const tabs = useSnapshot(tabStore)
+ const tabs = useTabsStore(projectRef)
const isSQLEditorTabsEnabled = useIsSQLEditorTabsEnabled()
const [sectionVisibility, setSectionVisibility] = useLocalStorage<SectionState>(
@@ -540,11 +539,10 @@ export const SQLEditorNav = ({ sort = 'inserted_at' }: SQLEditorNavProps) => {
}, [projectRef, sharedSqlSnippetsData?.pages])
useEffect(() => {
- if (projectRef && isSuccess && isSharedSqlSnippetsSuccess && isSQLEditorTabsEnabled) {
+ if (projectRef && isSuccess && isSQLEditorTabsEnabled) {
sqlEditorTabsCleanup({ ref: projectRef, snippets: allSnippetsInView as any })
}
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [isSuccess, isSharedSqlSnippetsSuccess, allSnippetsInView])
+ }, [isSuccess, isSharedSqlSnippetsSuccess, allSnippetsInView, isSQLEditorTabsEnabled])
return (
<>
diff --git a/apps/studio/components/layouts/SQLEditorLayout/SQLEditorNavV2/SearchList.tsx b/apps/studio/components/layouts/SQLEditorLayout/SQLEditorNavV2/SearchList.tsx
index 3a7305a4bcc..9c74944dbc9 100644
--- a/apps/studio/components/layouts/SQLEditorLayout/SQLEditorNavV2/SearchList.tsx
+++ b/apps/studio/components/layouts/SQLEditorLayout/SQLEditorNavV2/SearchList.tsx
@@ -29,7 +29,7 @@ export const SearchList = ({ search }: SearchListProps) => {
{ keepPreviousData: true }
)
- const { data: count, isLoading: isLoadingCount } = useContentCountQuery(
+ const { data: count } = useContentCountQuery(
{
projectRef,
cumulative: true,
diff --git a/apps/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx b/apps/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx
index 1474176cd1a..df73b7f6ade 100644
--- a/apps/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx
+++ b/apps/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx
@@ -40,16 +40,17 @@ import {
InnerSideBarFilters,
} from 'ui-patterns/InnerSideMenu'
import { useProjectContext } from '../ProjectLayout/ProjectContext'
+import { tableEditorTabsCleanUp } from '../Tabs/Tabs.utils'
import EntityListItem from './EntityListItem'
import { TableMenuEmptyState } from './TableMenuEmptyState'
const TableEditorMenu = () => {
const { ref, id: _id } = useParams()
+ const isMobile = useBreakpoint()
const id = _id ? Number(_id) : undefined
const snap = useTableEditorStateSnapshot()
- const { selectedSchema, setSelectedSchema } = useQuerySchemaState()
- const isMobile = useBreakpoint()
const isTableEditorTabsEnabled = useIsTableEditorTabsEnabled()
+ const { selectedSchema, setSelectedSchema } = useQuerySchemaState()
const [showModal, setShowModal] = useState(false)
const [searchText, setSearchText] = useState<string>('')
@@ -114,6 +115,13 @@ const TableEditorMenu = () => {
}
}, [selectedTable?.schema])
+ useEffect(() => {
+ // Clean up tabs + recent items for any tables that might have been removed outside of the dashboard session
+ if (isTableEditorTabsEnabled && ref && entityTypes && !searchText) {
+ tableEditorTabsCleanUp({ ref, schemas: [selectedSchema], entities: entityTypes })
+ }
+ }, [entityTypes])
+
return (
<>
<div className="flex flex-col flex-grow gap-5 pt-5 h-full">
diff --git a/apps/studio/components/layouts/Tabs/Tabs.utils.ts b/apps/studio/components/layouts/Tabs/Tabs.utils.ts
index e9636ab5177..9fc1e1225c3 100644
--- a/apps/studio/components/layouts/Tabs/Tabs.utils.ts
+++ b/apps/studio/components/layouts/Tabs/Tabs.utils.ts
@@ -5,7 +5,7 @@ import {
RecentItem,
removeRecentItems,
} from 'state/recent-items'
-import { createTabId, getTabsStore, removeTabs } from 'state/tabs'
+import { createTabId, editorEntityTypes, getTabsStore, removeTabs, updateTab } from 'state/tabs'
export const tableEditorTabsCleanUp = ({
ref,
@@ -58,6 +58,18 @@ export const tableEditorTabsCleanUp = ({
// perform tabs cleanup
removeTabs(ref, tableEditorTabsToBeCleaned)
+
+ // [Joshen] Validate for opened tabs, if their label matches the entity's name - update label if not
+ // As the entity could've been renamed outside of the table editor
+ // e.g Using the SQL editor to rename the entity
+ const openTabs = tabsStore.openTabs
+ .map((id) => tabsStore.tabsMap[id])
+ .filter((tab) => editorEntityTypes['table']?.includes(tab.type))
+
+ openTabs.forEach((tab) => {
+ const entity = entities?.find((x) => tab.metadata?.tableId === x.id)
+ if (!!entity && entity.name !== tab.label) updateTab(ref, tab.id, { label: entity.name })
+ })
}
export const sqlEditorTabsCleanup = ({
@@ -65,7 +77,7 @@ export const sqlEditorTabsCleanup = ({
snippets,
}: {
ref: string
- snippets: { id: string; type: string }[]
+ snippets: { id: string; type: string; name: string }[]
}) => {
// these are tabs that are static content
// these canot be removed from localstorage based on this query request
@@ -95,4 +107,16 @@ export const sqlEditorTabsCleanup = ({
? recentItems.filter((item) => !currentContentIds.includes(item.id)).map((item) => item.id)
: []
)
+
+ // [Joshen] Validate for opened tabs, if their label matches the snippet's name - update label if not
+ // As the snippets name could've been updated outside of the SQL Editor session
+ // e.g for a shared snippet, the owner could've updated the name of the snippet
+ const openSqlTabs = tabsStore.openTabs
+ .map((id) => tabsStore.tabsMap[id])
+ .filter((tab) => editorEntityTypes['sql']?.includes(tab.type))
+
+ openSqlTabs.forEach((tab) => {
+ const snippet = snippets?.find((x) => tab.metadata?.sqlId === x.id)
+ if (!!snippet && snippet.name !== tab.label) updateTab(ref, tab.id, { label: snippet.name })
+ })
}