Files
supabase/studio/components/interfaces/App/FeaturePreview/FeaturePreviewContext.tsx
T
Joshen LimandTerry Sutton 94f5fc5a61 Feat/api side panel (#17880)
* Demo api side panel

* Add docs for storage and edge functions

* Add content

* fix

* Add scroll to logic

* Add storage docs

* Add docs URLs

* Add connecting to project section in getting started

* Link back to docs site for product + resource specific docs

* Add entry points into API side panel for table editor, auth users and storage

* Add entry points into API side panel for edge functions

* Beef up feature preview modal with content

* Fix asset path

* Address several feedback

* Addressing more feedback

* Shift GraphiQL page to database if new api docs flag enabled

* Spelling

* Wording changes

* Spacing and wording

* Fix small issues

* Fix

* Update key showing toggle button

---------

Co-authored-by: Terry Sutton <saltcod@gmail.com>
2023-10-05 13:30:12 +08:00

62 lines
1.9 KiB
TypeScript

import { noop } from 'lodash'
import { PropsWithChildren, createContext, useContext, useEffect, useState } from 'react'
import { LOCAL_STORAGE_KEYS } from 'lib/constants'
import { EMPTY_OBJ } from 'lib/void'
type FeaturePreviewContextType = {
flags: { [key: string]: boolean }
onUpdateFlag: (key: string, value: boolean) => void
}
const FeaturePreviewContext = createContext<FeaturePreviewContextType>({
flags: EMPTY_OBJ,
onUpdateFlag: noop,
})
export default FeaturePreviewContext
export const useFeaturePreviewContext = () => useContext(FeaturePreviewContext)
export const FeaturePreviewContextProvider = ({ children }: PropsWithChildren<{}>) => {
const [flags, setFlags] = useState({
[LOCAL_STORAGE_KEYS.UI_PREVIEW_NAVIGATION_LAYOUT]: false,
[LOCAL_STORAGE_KEYS.UI_PREVIEW_API_SIDE_PANEL]: false,
})
useEffect(() => {
if (typeof window !== 'undefined') {
setFlags({
[LOCAL_STORAGE_KEYS.UI_PREVIEW_NAVIGATION_LAYOUT]:
localStorage.getItem(LOCAL_STORAGE_KEYS.UI_PREVIEW_NAVIGATION_LAYOUT) === 'true',
[LOCAL_STORAGE_KEYS.UI_PREVIEW_API_SIDE_PANEL]:
localStorage.getItem(LOCAL_STORAGE_KEYS.UI_PREVIEW_API_SIDE_PANEL) === 'true',
})
}
}, [])
const value = {
flags,
onUpdateFlag: (key: string, value: boolean) => {
if (typeof window !== 'undefined') {
window.localStorage.setItem(key, value.toString())
}
const updatedFlags = { ...flags, [key]: value }
setFlags(updatedFlags)
},
}
return <FeaturePreviewContext.Provider value={value}>{children}</FeaturePreviewContext.Provider>
}
// Helpers
export const useIsNavigationPreviewEnabled = () => {
const { flags } = useFeaturePreviewContext()
return flags[LOCAL_STORAGE_KEYS.UI_PREVIEW_NAVIGATION_LAYOUT]
}
export const useIsAPIDocsSidePanelEnabled = () => {
const { flags } = useFeaturePreviewContext()
return flags[LOCAL_STORAGE_KEYS.UI_PREVIEW_API_SIDE_PANEL]
}