database-settings.tsx39 lines · main
| 1 | import { useConstant } from 'common' |
| 2 | import { createContext, PropsWithChildren, useContext } from 'react' |
| 3 | import { proxy, useSnapshot } from 'valtio' |
| 4 | |
| 5 | export function createDatabaseSettingsState() { |
| 6 | const state = proxy({ |
| 7 | usePoolerConnection: true, |
| 8 | setUsePoolerConnection: (value: boolean) => { |
| 9 | state.usePoolerConnection = value |
| 10 | }, |
| 11 | showPoolingModeHelper: false, |
| 12 | setShowPoolingModeHelper: (value: boolean) => { |
| 13 | state.showPoolingModeHelper = value |
| 14 | }, |
| 15 | }) |
| 16 | |
| 17 | return state |
| 18 | } |
| 19 | |
| 20 | export type DatabaseSettingsState = ReturnType<typeof createDatabaseSettingsState> |
| 21 | |
| 22 | export const DatabaseSettingsStateContext = createContext<DatabaseSettingsState>( |
| 23 | createDatabaseSettingsState() |
| 24 | ) |
| 25 | |
| 26 | export const DatabaseSettingsStateContextProvider = ({ children }: PropsWithChildren) => { |
| 27 | const state = useConstant(createDatabaseSettingsState) |
| 28 | |
| 29 | return ( |
| 30 | <DatabaseSettingsStateContext.Provider value={state}> |
| 31 | {children} |
| 32 | </DatabaseSettingsStateContext.Provider> |
| 33 | ) |
| 34 | } |
| 35 | |
| 36 | export function useDatabaseSettingsStateSnapshot(options?: Parameters<typeof useSnapshot>[1]) { |
| 37 | const state = useContext(DatabaseSettingsStateContext) |
| 38 | return useSnapshot(state, options) |
| 39 | } |