jan/web/hooks/useDeleteConversation.ts
Louis 96dba2690d feat: class-based plugin manager
chore: add facades

refactor: core module export

refactor: inference plugin - deprecate function registering (#537)

* refactor: revamp inference plugin as class - deprecate function registering

* refactor: monitoring plugin - deprecate service registering (#538)

refactor: revamp inference plugin as class - deprecate function registering

chore: update import

refactor: plugin revamp - model management

chore: update build steps and remove experimental plugins

refactor: remove pluggable electron

chore: add sorting for conversations

chore: build plugins for testing

chore: consistent plugin directory name

chore: docs

chore: fix CI

chore: update conversation prefix
2023-11-06 13:46:01 +07:00

65 lines
2.0 KiB
TypeScript

import { currentPromptAtom } from '@helpers/JotaiWrapper'
import { useAtom, useAtomValue, useSetAtom } from 'jotai'
import { PluginType } from '@janhq/core'
import { deleteConversationMessage } from '@helpers/atoms/ChatMessage.atom'
import {
userConversationsAtom,
getActiveConvoIdAtom,
setActiveConvoIdAtom,
} from '@helpers/atoms/Conversation.atom'
import {
showingProductDetailAtom,
showingAdvancedPromptAtom,
} from '@helpers/atoms/Modal.atom'
import {
MainViewState,
setMainViewStateAtom,
} from '@helpers/atoms/MainView.atom'
import { pluginManager } from '../plugin/PluginManager'
import { ConversationalPlugin } from '@janhq/core/lib/plugins'
export default function useDeleteConversation() {
const [userConversations, setUserConversations] = useAtom(
userConversationsAtom
)
const setCurrentPrompt = useSetAtom(currentPromptAtom)
const setShowingProductDetail = useSetAtom(showingProductDetailAtom)
const setShowingAdvancedPrompt = useSetAtom(showingAdvancedPromptAtom)
const activeConvoId = useAtomValue(getActiveConvoIdAtom)
const setActiveConvoId = useSetAtom(setActiveConvoIdAtom)
const deleteMessages = useSetAtom(deleteConversationMessage)
const setMainViewState = useSetAtom(setMainViewStateAtom)
const deleteConvo = async () => {
if (activeConvoId) {
try {
await pluginManager
.get<ConversationalPlugin>(PluginType.Conversational)
?.deleteConversation(activeConvoId)
const currentConversations = userConversations.filter(
(c) => c._id !== activeConvoId
)
setUserConversations(currentConversations)
deleteMessages(activeConvoId)
if (currentConversations.length > 0) {
setActiveConvoId(currentConversations[0]._id)
} else {
setMainViewState(MainViewState.Welcome)
setActiveConvoId(undefined)
}
setCurrentPrompt('')
setShowingProductDetail(false)
setShowingAdvancedPrompt(false)
} catch (err) {
console.error(err)
}
}
}
return {
deleteConvo,
}
}