jan/web/hooks/useDownloadModel.ts
NamH 86e693b250
refactor: model plugin to follow new specs (#682)
* refactor: model plugin to follow new specs

Signed-off-by: James <james@jan.ai>

* chore: rebase main

chore: rebase main

---------

Signed-off-by: James <james@jan.ai>
Co-authored-by: James <james@jan.ai>
Co-authored-by: Louis <louis@jan.ai>
2023-11-29 11:36:59 +07:00

44 lines
1.0 KiB
TypeScript

import { PluginType } from '@janhq/core'
import { ModelPlugin } from '@janhq/core/lib/plugins'
import { Model } from '@janhq/core/lib/types'
import { useAtom } from 'jotai'
import { useDownloadState } from './useDownloadState'
import { downloadingModelsAtom } from '@/helpers/atoms/Model.atom'
import { pluginManager } from '@/plugin/PluginManager'
export default function useDownloadModel() {
const { setDownloadState } = useDownloadState()
const [downloadingModels, setDownloadingModels] = useAtom(
downloadingModelsAtom
)
const downloadModel = async (model: Model) => {
// set an initial download state
setDownloadState({
modelId: model.id,
time: {
elapsed: 0,
remaining: 0,
},
speed: 0,
percent: 0,
size: {
total: 0,
transferred: 0,
},
fileName: model.id,
})
setDownloadingModels([...downloadingModels, model])
await pluginManager.get<ModelPlugin>(PluginType.Model)?.downloadModel(model)
}
return {
downloadModel,
}
}