mirror of
https://github.com/supabase/supabase.git
synced 2026-10-10 11:55:05 +03:00
* Split the TOC state from the components to avoid cyclic dependencies on Tabs component. * Fix type errors.
33 lines
994 B
TypeScript
33 lines
994 B
TypeScript
'use client'
|
|
|
|
import { createContext, useContext } from 'react'
|
|
import { type AnchorProviderProps } from 'ui-patterns/Toc'
|
|
import { proxy, useSnapshot } from 'valtio'
|
|
|
|
const TocAnchorsContext = createContext<AnchorProviderProps | undefined>(undefined)
|
|
|
|
const useTocAnchors = () => {
|
|
const context = useContext(TocAnchorsContext)
|
|
if (!context) {
|
|
throw new Error('useTocAnchors must be used within an TocAnchorsContext')
|
|
}
|
|
return context
|
|
}
|
|
|
|
const useTocRerenderTrigger = () => {
|
|
const { toggleRenderFlag } = useSnapshot(tocRenderSwitch)
|
|
return toggleRenderFlag
|
|
}
|
|
|
|
const tocRenderSwitch = proxy({
|
|
renderFlag: 0,
|
|
toggleRenderFlag: () => void (tocRenderSwitch.renderFlag = (tocRenderSwitch.renderFlag + 1) % 2),
|
|
})
|
|
|
|
const useSubscribeTocRerender = () => {
|
|
const { renderFlag } = useSnapshot(tocRenderSwitch)
|
|
return void renderFlag // Prevent it from being detected as unused code
|
|
}
|
|
|
|
export { TocAnchorsContext, useSubscribeTocRerender, useTocAnchors, useTocRerenderTrigger }
|