Fix tabs valtio store initial render (#37505)

* Fix tabs valtio store initial render

* Address feedback

---------

Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
This commit is contained in:
Joshen LimandIvan Vasilov authored and GitHub committed 2025-07-29 18:16:11 +08:00
1 parent 6b5abce83b
commit f7009952f9
1 file changed
+12 -7
+12 -7
View File
@@ -1,9 +1,8 @@
import { useConstant } from 'common'
import { ENTITY_TYPE } from 'data/entity-types/entity-type-constants'
import { useSelectedProject } from 'hooks/misc/useSelectedProject'
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
import { partition } from 'lodash'
import { NextRouter } from 'next/router'
import { createContext, PropsWithChildren, ReactNode, useContext, useEffect } from 'react'
import { createContext, PropsWithChildren, ReactNode, useContext, useEffect, useState } from 'react'
import { proxy, subscribe, useSnapshot } from 'valtio'
export const editorEntityTypes = {
@@ -78,6 +77,7 @@ const DEFAULT_TABS_STATE = {
openTabs: [] as string[],
tabsMap: {} as { [key: string]: Tab },
previewTabId: undefined as string | undefined,
recentItems: [],
}
const TABS_STORAGE_KEY = 'supabase_studio_tabs'
const getTabsStorageKey = (ref: string) => `${TABS_STORAGE_KEY}_${ref}`
@@ -399,8 +399,14 @@ export type TabsState = ReturnType<typeof createTabsState>
export const TabsStateContext = createContext<TabsState>(createTabsState(''))
export const TabsStateContextProvider = ({ children }: PropsWithChildren) => {
const project = useSelectedProject()
const state = useConstant(() => createTabsState(project?.ref ?? ''))
const { data: project } = useSelectedProjectQuery()
const [state, setState] = useState(createTabsState(project?.ref ?? ''))
useEffect(() => {
if (typeof window !== 'undefined' && !!project?.ref) {
setState(createTabsState(project?.ref ?? ''))
}
}, [project?.ref])
useEffect(() => {
if (typeof window !== 'undefined' && project?.ref) {
@@ -422,8 +428,7 @@ export const TabsStateContextProvider = ({ children }: PropsWithChildren) => {
)
})
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
}, [project?.ref, state])
return <TabsStateContext.Provider value={state}>{children}</TabsStateContext.Provider>
}