Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
jingsam committed Sep 2, 2024
2 parents e4fcf15 + 24f590e commit 7f4768e
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions apps/studio/hooks/misc/useSchemaQueryState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parseAsString, useQueryState } from 'nuqs'
import { useEffect, useMemo } from 'react'

import { useParams } from 'common'
import { LOCAL_STORAGE_KEYS } from 'lib/constants'
Expand All @@ -19,18 +20,22 @@ const useIsomorphicUseQueryState = (defaultSchema: string) => {
export const useQuerySchemaState = () => {
const { ref } = useParams()

let defaultSchema = 'public'
if (typeof window !== 'undefined') {
defaultSchema =
window.localStorage.getItem(LOCAL_STORAGE_KEYS.LAST_SELECTED_SCHEMA(ref ?? '')) || 'public'
}
const defaultSchema =
typeof window !== 'undefined' && ref && ref.length > 0
? window.localStorage.getItem(LOCAL_STORAGE_KEYS.LAST_SELECTED_SCHEMA(ref)) || 'public'
: 'public'

const [schema, setSelectedSchema] = useIsomorphicUseQueryState(defaultSchema)
// cache the original default schema so that it's not changed by another tab and cause issues in the app (saving a
// table on the wrong schema)
const originalDefaultSchema = useMemo(() => defaultSchema, [ref])
const [schema, setSelectedSchema] = useIsomorphicUseQueryState(originalDefaultSchema)

// Update the schema to local storage on every change
if (defaultSchema !== schema && typeof window !== 'undefined') {
window.localStorage.setItem(LOCAL_STORAGE_KEYS.LAST_SELECTED_SCHEMA(ref ?? ''), schema)
}
useEffect(() => {
// Update the schema in local storage on every change
if (typeof window !== 'undefined' && ref && ref.length > 0) {
window.localStorage.setItem(LOCAL_STORAGE_KEYS.LAST_SELECTED_SCHEMA(ref), schema)
}
}, [schema, ref])

return { selectedSchema: schema, setSelectedSchema }
}

0 comments on commit 7f4768e

Please sign in to comment.