jan/web/app/_hooks/useDeleteConversation.ts
Louis b46042654c
chore: update core services and module export (#376)
* chore: update core services and module export

* Correct version of plugins (#374)

Co-authored-by: Hien To <tominhhien97@gmail.com>

* janhq/jan: Update tag build 1.0.2 for data-plugin

* janhq/jan: Update tag build 1.0.2 for inference-plugin

* janhq/jan: Update tag build 1.0.2 for model-management-plugin

* janhq/jan: Update tag build 1.0.2 for monitoring-plugin

* janhq/jan: Update tag build 1.0.2 for openai-plugin

* chore: update web to use @janhq/core module

---------

Co-authored-by: hiento09 <136591877+hiento09@users.noreply.github.com>
Co-authored-by: Hien To <tominhhien97@gmail.com>
Co-authored-by: Service Account <service@jan.ai>
2023-10-17 19:48:43 +07:00

62 lines
2.0 KiB
TypeScript

import { currentPromptAtom } from "@/_helpers/JotaiWrapper";
import { execute } from "@/_services/pluginService";
import { useAtom, useAtomValue, useSetAtom } from "jotai";
import { DataService } 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";
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 execute(DataService.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,
};
}