Skip to content

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

SQL editor persist open close state of each section (#33112)

Joshen Lim36637a8c9401
diff --git a/apps/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx b/apps/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
index 3b91c2bcc71..fa171ff1e06 100644
--- a/apps/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
+++ b/apps/studio/components/layouts/SQLEditorLayout/SQLEditorMenu.tsx
@@ -9,6 +9,7 @@ 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 { useSqlEditorV2StateSnapshot } from 'state/sql-editor-v2'
import {
@@ -44,7 +45,10 @@ export const SQLEditorMenu = ({ onViewOngoingQueries }: SQLEditorMenuProps) => {
const snapV2 = useSqlEditorV2StateSnapshot()
const [search, setSearch] = useState('')
const [showSearch, setShowSearch] = useState(false)
- const [sort, setSort] = useLocalStorage<'name' | 'inserted_at'>('sql-editor-sort', 'inserted_at')
+ const [sort, setSort] = useLocalStorage<'name' | 'inserted_at'>(
+ LOCAL_STORAGE_KEYS.SQL_EDITOR_SORT(ref ?? ''),
+ 'inserted_at'
+ )
const debouncedSearch = useDebounce(search, 500)
diff --git a/apps/studio/components/layouts/SQLEditorLayout/SQLEditorNavV2/SQLEditorNav.tsx b/apps/studio/components/layouts/SQLEditorLayout/SQLEditorNavV2/SQLEditorNav.tsx
index d4a1ada1b19..8ab889f355a 100644
--- a/apps/studio/components/layouts/SQLEditorLayout/SQLEditorNavV2/SQLEditorNav.tsx
+++ b/apps/studio/components/layouts/SQLEditorLayout/SQLEditorNavV2/SQLEditorNav.tsx
@@ -16,7 +16,9 @@ import { useContentUpsertMutation } from 'data/content/content-upsert-mutation'
import { useSQLSnippetFoldersDeleteMutation } from 'data/content/sql-folders-delete-mutation'
import { Snippet, SnippetFolder, useSQLSnippetFoldersQuery } from 'data/content/sql-folders-query'
import { useSqlSnippetsQuery } from 'data/content/sql-snippets-query'
+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 uuidv4 from 'lib/uuid'
import {
@@ -42,19 +44,28 @@ interface SQLEditorNavProps {
sort?: 'inserted_at' | 'name'
}
+type SectionState = { shared: boolean; favorite: boolean; private: boolean }
+const DEFAULT_SECTION_STATE: SectionState = { shared: false, favorite: false, private: true }
+
export const SQLEditorNav = ({ sort = 'inserted_at' }: SQLEditorNavProps) => {
const router = useRouter()
const { profile } = useProfile()
const project = useSelectedProject()
const { ref: projectRef, id } = useParams()
const snapV2 = useSqlEditorV2StateSnapshot()
+ const [sectionVisibility, setSectionVisibility] = useLocalStorage<SectionState>(
+ LOCAL_STORAGE_KEYS.SQL_EDITOR_SECTION_STATE(projectRef ?? ''),
+ DEFAULT_SECTION_STATE
+ )
+ const {
+ shared: showSharedSnippets,
+ favorite: showFavoriteSnippets,
+ private: showPrivateSnippets,
+ } = sectionVisibility
const [showMoveModal, setShowMoveModal] = useState(false)
const [showDeleteModal, setShowDeleteModal] = useState(false)
const [showRenameModal, setShowRenameModal] = useState(false)
- const [showFavoriteSnippets, setShowFavoriteSnippets] = useState(false)
- const [showSharedSnippets, setShowSharedSnippets] = useState(false)
- const [showPrivateSnippets, setShowPrivateSnippets] = useState(true)
const [expandedFolderIds, setExpandedFolderIds] = useState<string[]>([])
const [selectedSnippets, setSelectedSnippets] = useState<Snippet[]>([])
@@ -352,7 +363,7 @@ export const SQLEditorNav = ({ sort = 'inserted_at' }: SQLEditorNavProps) => {
onSuccess: () => {
setSelectedSnippetToShare(undefined)
setSelectedSnippetToUnshare(undefined)
- setShowSharedSnippets(true)
+ setSectionVisibility({ ...sectionVisibility, shared: true })
snapV2.updateSnippet({
id: snippet.id,
snippet: { visibility, folder_id: null },
@@ -457,7 +468,11 @@ export const SQLEditorNav = ({ sort = 'inserted_at' }: SQLEditorNavProps) => {
useEffect(() => {
if (snippet !== undefined && isSuccess) {
- if (snippet.visibility === 'project') setShowSharedSnippets(true)
+ if (snippet.visibility === 'project') {
+ setSectionVisibility({ ...sectionVisibility, shared: true })
+ } else if (snippet.visibility === 'user') {
+ setSectionVisibility({ ...sectionVisibility, private: true })
+ }
if (snippet.folder_id && !expandedFolderIds.includes(snippet.folder_id)) {
setExpandedFolderIds([...expandedFolderIds, snippet.folder_id])
}
@@ -513,9 +528,12 @@ export const SQLEditorNav = ({ sort = 'inserted_at' }: SQLEditorNavProps) => {
return (
<>
<InnerSideMenuSeparator />
+
<InnerSideMenuCollapsible
open={showSharedSnippets}
- onOpenChange={setShowSharedSnippets}
+ onOpenChange={(value) => {
+ setSectionVisibility({ ...(sectionVisibility ?? DEFAULT_SECTION_STATE), shared: value })
+ }}
className="px-0"
>
<InnerSideMenuCollapsibleTrigger
@@ -565,12 +583,15 @@ export const SQLEditorNav = ({ sort = 'inserted_at' }: SQLEditorNavProps) => {
)}
</InnerSideMenuCollapsibleContent>
</InnerSideMenuCollapsible>
+
<InnerSideMenuSeparator />
<InnerSideMenuCollapsible
className="px-0"
open={showFavoriteSnippets}
- onOpenChange={setShowFavoriteSnippets}
+ onOpenChange={(value) => {
+ setSectionVisibility({ ...(sectionVisibility ?? DEFAULT_SECTION_STATE), favorite: value })
+ }}
>
<InnerSideMenuCollapsibleTrigger
title={`Favorites ${numFavoriteSnippets > 0 ? ` (${numFavoriteSnippets})` : ''}`}
@@ -626,11 +647,14 @@ export const SQLEditorNav = ({ sort = 'inserted_at' }: SQLEditorNavProps) => {
)}
</InnerSideMenuCollapsibleContent>
</InnerSideMenuCollapsible>
+
<InnerSideMenuSeparator />
<InnerSideMenuCollapsible
open={showPrivateSnippets}
- onOpenChange={setShowPrivateSnippets}
+ onOpenChange={(value) => {
+ setSectionVisibility({ ...(sectionVisibility ?? DEFAULT_SECTION_STATE), private: value })
+ }}
className="px-0"
>
<InnerSideMenuCollapsibleTrigger
diff --git a/apps/studio/lib/constants/index.ts b/apps/studio/lib/constants/index.ts
index 006cb7ac174..1418f12a5e2 100644
--- a/apps/studio/lib/constants/index.ts
+++ b/apps/studio/lib/constants/index.ts
@@ -51,6 +51,8 @@ export const LOCAL_STORAGE_KEYS = {
SQL_EDITOR_AI_OPEN: 'supabase_sql-editor-ai-open',
SQL_EDITOR_LAST_SELECTED_DB: (ref: string) => `sql-editor-last-selected-db-${ref}`,
SQL_EDITOR_SQL_BLOCK_ACKNOWLEDGED: (ref: string) => `sql-editor-sql-block-acknowledged-${ref}`,
+ SQL_EDITOR_SECTION_STATE: (ref: string) => `sql-editor-section-state-${ref}`,
+ SQL_EDITOR_SORT: (ref: string) => `sql-editor-sort-${ref}`,
LOG_EXPLORER_SPLIT_SIZE: 'supabase_log-explorer-split-size',
GRAPHIQL_RLS_BYPASS_WARNING: 'graphiql-rls-bypass-warning-dismissed',