2023-09-22 17:06:04 +03:00
|
|
|
import { useContext } from "react";
|
|
|
|
|
2023-09-28 12:29:55 +03:00
|
|
|
import { FeedItemType } from "@/app/chat/[chatId]/components/ActionsBar/types";
|
|
|
|
|
2023-09-28 16:39:30 +03:00
|
|
|
import { KnowledgeToFeedContext } from "../knowledgeToFeed-provider";
|
2023-09-22 17:06:04 +03:00
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2023-09-28 16:39:30 +03:00
|
|
|
export const useKnowledgeToFeedContext = () => {
|
|
|
|
const context = useContext(KnowledgeToFeedContext);
|
2023-09-22 17:06:04 +03:00
|
|
|
|
2023-09-28 12:29:55 +03:00
|
|
|
const addKnowledgeToFeed = (knowledge: FeedItemType) => {
|
|
|
|
context?.setKnowledgeToFeed((prevKnowledge) => [
|
|
|
|
...prevKnowledge,
|
|
|
|
knowledge,
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeKnowledgeToFeed = (index: number) => {
|
|
|
|
context?.setKnowledgeToFeed((prevKnowledge) => {
|
|
|
|
const newKnowledge = [...prevKnowledge];
|
|
|
|
newKnowledge.splice(index, 1);
|
|
|
|
|
|
|
|
return newKnowledge;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-09-22 17:06:04 +03:00
|
|
|
if (context === undefined) {
|
2023-09-28 16:39:30 +03:00
|
|
|
throw new Error("useKnowledge must be used inside KnowledgeToFeedProvider");
|
2023-09-22 17:06:04 +03:00
|
|
|
}
|
|
|
|
|
2023-09-28 12:29:55 +03:00
|
|
|
return { ...context, addKnowledgeToFeed, removeKnowledgeToFeed };
|
2023-09-22 17:06:04 +03:00
|
|
|
};
|