jan/web/hooks/useGetInputState.ts
2023-11-16 22:57:28 +07:00

57 lines
1.6 KiB
TypeScript

import { useEffect, useState } from 'react'
import { Model, Thread } from '@janhq/core'
import { useAtomValue } from 'jotai'
import { useActiveModel } from './useActiveModel'
import { useGetDownloadedModels } from './useGetDownloadedModels'
import { currentConversationAtom } from '@/helpers/atoms/Conversation.atom'
export default function useGetInputState() {
const [inputState, setInputState] = useState<InputType>('loading')
const currentConvo = useAtomValue(currentConversationAtom)
const { activeModel } = useActiveModel()
const { downloadedModels } = useGetDownloadedModels()
const handleInputState = (
convo: Thread | undefined,
currentModel: Model | undefined
) => {
if (convo == null) return
if (currentModel == null) {
setInputState('loading')
return
}
// check if convo model id is in downloaded models
const isModelAvailable = downloadedModels.some(
(model) => model.id === convo.modelId
)
if (!isModelAvailable) {
// can't find model in downloaded models
setInputState('model-not-found')
return
}
if (convo.modelId !== currentModel.id) {
// in case convo model and active model is different,
// ask user to init the required model
setInputState('model-mismatch')
return
}
setInputState('available')
}
useEffect(() => {
handleInputState(currentConvo, activeModel)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
return { inputState, currentConvo }
}
type InputType = 'available' | 'loading' | 'model-mismatch' | 'model-not-found'