jan/web/context/FeatureToggle.tsx
markmehere 34d0e6deee
feat: HTTP proxy support (#1562)
* feat: allow self-signed certificates

* fix: Extra information in self signed error

* chore: simplified PR

* feat: allow https proxies

* fix: trim() may save one or two user headaches

* Update web/context/FeatureToggle.tsx

---------

Co-authored-by: Louis <louis@jan.ai>
Co-authored-by: hiento09 <136591877+hiento09@users.noreply.github.com>
2024-01-19 10:25:18 +07:00

78 lines
2.0 KiB
TypeScript

import { createContext, ReactNode, useEffect, useState } from 'react'
interface FeatureToggleContextType {
experimentalFeature: boolean
ignoreSSL: boolean
proxy: string
setExperimentalFeature: (on: boolean) => void
setIgnoreSSL: (on: boolean) => void
setProxy: (value: string) => void
}
const initialContext: FeatureToggleContextType = {
experimentalFeature: false,
ignoreSSL: false,
proxy: '',
setExperimentalFeature: () => {},
setIgnoreSSL: () => {},
setProxy: () => {},
}
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 [experimentalFeature, directSetExperimentalFeature] = 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) ?? ""
)
}, [])
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)
}
return (
<FeatureToggleContext.Provider
value={{
experimentalFeature,
ignoreSSL,
proxy,
setExperimentalFeature,
setIgnoreSSL,
setProxy,
}}
>
{children}
</FeatureToggleContext.Provider>
)
}