jan/web/context/FeatureToggle.tsx
Louis 780f957b9a
fix: expand assistant and model settings by default (#2081)
* fix: expand assistant and model settings by default

* fix: add proxy enabled toggle
2024-02-19 12:44:04 +07:00

92 lines
2.5 KiB
TypeScript

import { createContext, ReactNode, useEffect, useState } from 'react'
interface FeatureToggleContextType {
experimentalFeature: boolean
ignoreSSL: boolean
proxy: string
proxyEnabled: boolean
setExperimentalFeature: (on: boolean) => void
setIgnoreSSL: (on: boolean) => void
setProxy: (value: string) => void
setProxyEnabled: (on: boolean) => void
}
const initialContext: FeatureToggleContextType = {
experimentalFeature: false,
ignoreSSL: false,
proxy: '',
proxyEnabled: false,
setExperimentalFeature: () => {},
setIgnoreSSL: () => {},
setProxy: () => {},
setProxyEnabled: () => {},
}
export const FeatureToggleContext =
createContext<FeatureToggleContextType>(initialContext)
export default function FeatureToggleWrapper({
children,
}: {
children: ReactNode
}) {
const EXPERIMENTAL_FEATURE = 'experimentalFeature'
const IGNORE_SSL = 'ignoreSSLFeature'
const HTTPS_PROXY_FEATURE = 'httpsProxyFeature'
const PROXY_FEATURE_ENABLED = 'proxyFeatureEnabled'
const [experimentalFeature, directSetExperimentalFeature] =
useState<boolean>(false)
const [proxyEnabled, directSetProxyEnabled] = useState<boolean>(false)
const [ignoreSSL, directSetIgnoreSSL] = useState<boolean>(false)
const [proxy, directSetProxy] = useState<string>('')
useEffect(() => {
directSetExperimentalFeature(
localStorage.getItem(EXPERIMENTAL_FEATURE) === 'true'
)
directSetIgnoreSSL(localStorage.getItem(IGNORE_SSL) === 'true')
directSetProxy(localStorage.getItem(HTTPS_PROXY_FEATURE) ?? '')
directSetProxyEnabled(
localStorage.getItem(PROXY_FEATURE_ENABLED) === 'true'
)
}, [])
const setExperimentalFeature = (on: boolean) => {
localStorage.setItem(EXPERIMENTAL_FEATURE, on ? 'true' : 'false')
directSetExperimentalFeature(on)
}
const setIgnoreSSL = (on: boolean) => {
localStorage.setItem(IGNORE_SSL, on ? 'true' : 'false')
directSetIgnoreSSL(on)
}
const setProxy = (proxy: string) => {
localStorage.setItem(HTTPS_PROXY_FEATURE, proxy)
directSetProxy(proxy)
}
const setProxyEnabled = (on: boolean) => {
localStorage.setItem(PROXY_FEATURE_ENABLED, on ? 'true' : 'false')
directSetProxyEnabled(on)
}
return (
<FeatureToggleContext.Provider
value={{
experimentalFeature,
ignoreSSL,
proxy,
proxyEnabled,
setExperimentalFeature,
setIgnoreSSL,
setProxy,
setProxyEnabled,
}}
>
{children}
</FeatureToggleContext.Provider>
)
}