jan/web/hooks/useDownloadState.ts
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

95 lines
2.8 KiB
TypeScript

import { atom, useSetAtom, useAtomValue } from 'jotai'
import { toaster } from '@/containers/Toast'
// download states
const modelDownloadStateAtom = atom<Record<string, DownloadState>>({})
const setDownloadStateAtom = atom(null, (get, set, state: DownloadState) => {
const currentState = { ...get(modelDownloadStateAtom) }
console.debug(
`current download state for ${state.modelId} is ${JSON.stringify(state)}`
)
currentState[state.modelId] = state
set(modelDownloadStateAtom, currentState)
})
const setDownloadStateSuccessAtom = atom(null, (get, set, modelId: string) => {
const currentState = { ...get(modelDownloadStateAtom) }
const state = currentState[modelId]
if (!state) {
console.debug(`Cannot find download state for ${modelId}`)
return
}
delete currentState[modelId]
set(modelDownloadStateAtom, currentState)
toaster({
title: 'Download Completed',
description: `Download ${modelId} completed`,
})
})
const setDownloadStateFailedAtom = atom(
null,
(get, set, modelId: string, error: string) => {
const currentState = { ...get(modelDownloadStateAtom) }
const state = currentState[modelId]
if (!state) {
console.debug(`Cannot find download state for ${modelId}`)
return
}
if (error.includes('certificate')) {
error += '. To fix enable "Ignore SSL Certificates" in Advanced settings.'
}
toaster({
title: 'Download Failed',
description: `Model ${modelId} download failed: ${error}`,
type: 'error',
})
delete currentState[modelId]
set(modelDownloadStateAtom, currentState)
}
)
const setDownloadStateCancelledAtom = atom(
null,
(get, set, modelId: string) => {
const currentState = { ...get(modelDownloadStateAtom) }
const state = currentState[modelId]
if (!state) {
console.debug(`Cannot find download state for ${modelId}`)
toaster({
title: 'Cancel Download',
description: `Model ${modelId} cancel download`,
})
return
}
delete currentState[modelId]
set(modelDownloadStateAtom, currentState)
}
)
export function useDownloadState() {
const modelDownloadState = useAtomValue(modelDownloadStateAtom)
const setDownloadState = useSetAtom(setDownloadStateAtom)
const setDownloadStateSuccess = useSetAtom(setDownloadStateSuccessAtom)
const setDownloadStateFailed = useSetAtom(setDownloadStateFailedAtom)
const setDownloadStateCancelled = useSetAtom(setDownloadStateCancelledAtom)
const downloadStates: DownloadState[] = []
for (const [, value] of Object.entries(modelDownloadState)) {
downloadStates.push(value)
}
return {
modelDownloadStateAtom,
modelDownloadState,
setDownloadState,
setDownloadStateSuccess,
setDownloadStateFailed,
setDownloadStateCancelled,
downloadStates,
}
}