Skip to content

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

SQL editor open last viewed snippet (#18350)

Joshen Lima0416f22d8f1
diff --git a/studio/components/interfaces/App/RouteValidationWrapper.tsx b/studio/components/interfaces/App/RouteValidationWrapper.tsx
index 9689db2fdee..fc1a18e4082 100644
--- a/studio/components/interfaces/App/RouteValidationWrapper.tsx
+++ b/studio/components/interfaces/App/RouteValidationWrapper.tsx
@@ -7,15 +7,17 @@ import { useOrganizationsQuery } from 'data/organizations/organizations-query'
import { useProjectsQuery } from 'data/projects/projects-query'
import { useFlag, useStore, useLatest } from 'hooks'
import { DEFAULT_HOME, IS_PLATFORM, LOCAL_STORAGE_KEYS } from 'lib/constants'
+import { useAppStateSnapshot } from 'state/app-state'
// Ideally these could all be within a _middleware when we use Next 12
const RouteValidationWrapper = ({ children }: PropsWithChildren<{}>) => {
const { ui } = useStore()
const router = useRouter()
- const { ref, slug } = useParams()
+ const { ref, slug, id } = useParams()
const navLayoutV2 = useFlag('navigationLayoutV2')
const isLoggedIn = useIsLoggedIn()
+ const snap = useAppStateSnapshot()
/**
* Array of urls/routes that should be ignored
@@ -109,6 +111,17 @@ const RouteValidationWrapper = ({ children }: PropsWithChildren<{}>) => {
}
}, [ref, projectsInitialized])
+ useEffect(() => {
+ if (ref !== undefined && id !== undefined) {
+ if (router.pathname.endsWith('/sql/[id]') && id !== 'new') {
+ snap.setDashboardHistory(ref, 'sql', id)
+ }
+ if (router.pathname.endsWith('/editor/[id]')) {
+ snap.setDashboardHistory(ref, 'editor', id)
+ }
+ }
+ }, [ref, id])
+
return <>{children}</>
}
diff --git a/studio/lib/constants/index.ts b/studio/lib/constants/index.ts
index 5b60f30a67b..adf4a068971 100644
--- a/studio/lib/constants/index.ts
+++ b/studio/lib/constants/index.ts
@@ -98,6 +98,7 @@ export const LOCAL_STORAGE_KEYS = {
TELEMETRY_CONSENT: 'supabase-consent',
UI_PREVIEW_NAVIGATION_LAYOUT: 'supabase-ui-preview-nav-layout',
UI_PREVIEW_API_SIDE_PANEL: 'supabase-ui-api-side-panel',
+ DASHBOARD_HISTORY: (ref: string) => `dashboard-history-${ref}`,
}
export const OPT_IN_TAGS = {
diff --git a/studio/lib/helpers.ts b/studio/lib/helpers.ts
index 5e6700b8414..d095c23937f 100644
--- a/studio/lib/helpers.ts
+++ b/studio/lib/helpers.ts
@@ -5,7 +5,7 @@ import { v4 as _uuidV4 } from 'uuid'
export const tryParseJson = (jsonString: any) => {
try {
- let parsed = JSON.parse(jsonString)
+ const parsed = JSON.parse(jsonString)
return parsed
} catch (error) {
return undefined
diff --git a/studio/pages/_app.tsx b/studio/pages/_app.tsx
index 999c9105603..a49199353ae 100644
--- a/studio/pages/_app.tsx
+++ b/studio/pages/_app.tsx
@@ -90,9 +90,10 @@ loader.config({
// the dashboard, all other layout components should not be doing that
function CustomApp({ Component, pageProps }: AppPropsWithLayout) {
- const consentToastId = useRef<string>()
- const queryClient = useRootQueryClient()
const snap = useAppStateSnapshot()
+ const queryClient = useRootQueryClient()
+
+ const consentToastId = useRef<string>()
const [rootStore] = useState(() => new RootStore())
// [Joshen] Some issues with using createBrowserSupabaseClient
@@ -105,6 +106,24 @@ function CustomApp({ Component, pageProps }: AppPropsWithLayout) {
: undefined
)
+ useAutoAuthRedirect(queryClient)
+
+ const getLayout = Component.getLayout ?? ((page) => page)
+
+ const AuthContainer = useMemo(
+ // eslint-disable-next-line react/display-name
+ () => (props: any) => {
+ return IS_PLATFORM ? (
+ <SessionContextProvider supabaseClient={supabase as any}>
+ <AuthProvider>{props.children}</AuthProvider>
+ </SessionContextProvider>
+ ) : (
+ <AuthProvider>{props.children}</AuthProvider>
+ )
+ },
+ [supabase]
+ )
+
useEffect(() => {
// Check for telemetry consent
if (typeof window !== 'undefined') {
@@ -133,24 +152,6 @@ function CustomApp({ Component, pageProps }: AppPropsWithLayout) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
- useAutoAuthRedirect(queryClient)
-
- const getLayout = Component.getLayout ?? ((page) => page)
-
- const AuthContainer = useMemo(
- // eslint-disable-next-line react/display-name
- () => (props: any) => {
- return IS_PLATFORM ? (
- <SessionContextProvider supabaseClient={supabase as any}>
- <AuthProvider>{props.children}</AuthProvider>
- </SessionContextProvider>
- ) : (
- <AuthProvider>{props.children}</AuthProvider>
- )
- },
- [supabase]
- )
-
return (
<QueryClientProvider client={queryClient}>
<Hydrate state={pageProps.dehydratedState}>
diff --git a/studio/pages/project/[ref]/sql/[id].tsx b/studio/pages/project/[ref]/sql/[id].tsx
index fc9e513dd9d..588ec580cf1 100644
--- a/studio/pages/project/[ref]/sql/[id].tsx
+++ b/studio/pages/project/[ref]/sql/[id].tsx
@@ -1,6 +1,8 @@
+import { useRouter } from 'next/router'
import { useMonaco } from '@monaco-editor/react'
import { observer } from 'mobx-react-lite'
import { useEffect, useRef } from 'react'
+import { useParams } from 'common'
import { useFunctionsQuery } from 'data/database/functions-query'
import { useKeywordsQuery } from 'data/database/keywords-query'
@@ -14,14 +16,19 @@ import { SQLEditorLayout } from 'components/layouts'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import getPgsqlCompletionProvider from 'components/ui/CodeEditor/Providers/PgSQLCompletionProvider'
import getPgsqlSignatureHelpProvider from 'components/ui/CodeEditor/Providers/PgSQLSignatureHelpProvider'
-import { useSqlEditorStateSnapshot } from 'state/sql-editor'
-import { useParams } from 'common'
+import { useAppStateSnapshot } from 'state/app-state'
+import { useSnippets, useSqlEditorStateSnapshot } from 'state/sql-editor'
const SqlEditor: NextPageWithLayout = () => {
+ const router = useRouter()
const monaco = useMonaco()
- const { id } = useParams()
+ const { id, ref } = useParams()
+
const { project } = useProjectContext()
const snap = useSqlEditorStateSnapshot()
+ const appSnap = useAppStateSnapshot()
+
+ const snippets = useSnippets(ref)
const { mutateAsync: formatQuery } = useFormatQueryMutation()
async function formatPgsql(value: string) {
@@ -72,6 +79,13 @@ const SqlEditor: NextPageWithLayout = () => {
pgInfoRef.current.functions = functions?.result
}
+ useEffect(() => {
+ if (id === 'new' && appSnap.dashboardHistory.sql !== undefined) {
+ const snippet = snippets.find((snippet) => snippet.id === appSnap.dashboardHistory.sql)
+ if (snippet !== undefined) router.push(`/project/${ref}/sql/${appSnap.dashboardHistory.sql}`)
+ }
+ }, [id, snippets])
+
// Enable pgsql format
useEffect(() => {
if (monaco) {
diff --git a/studio/state/app-state.ts b/studio/state/app-state.ts
index 689f49ae815..b85ddf1b8d7 100644
--- a/studio/state/app-state.ts
+++ b/studio/state/app-state.ts
@@ -1,7 +1,27 @@
import { LOCAL_STORAGE_KEYS } from 'lib/constants'
import { proxy, snapshot, useSnapshot } from 'valtio'
+const EMPTY_DASHBOARD_HISTORY: {
+ sql?: string
+ editor?: string
+} = {
+ sql: undefined,
+ editor: undefined,
+}
+
export const appState = proxy({
+ // [Joshen] Last visited "entity" for any page that we wanna track
+ dashboardHistory: EMPTY_DASHBOARD_HISTORY,
+ setDashboardHistory: (ref: string, key: 'sql' | 'editor', id: string) => {
+ if (appState.dashboardHistory[key] !== id) {
+ appState.dashboardHistory[key] = id
+ localStorage.setItem(
+ LOCAL_STORAGE_KEYS.DASHBOARD_HISTORY(ref),
+ JSON.stringify(appState.dashboardHistory)
+ )
+ }
+ },
+
activeDocsSection: ['introduction'],
showProjectApiDocs: false,
setShowProjectApiDocs: (value: boolean) => {