Skip to content

Commit

Permalink
feat: refecth notifications on Feed
Browse files Browse the repository at this point in the history
  • Loading branch information
mamadoudicko committed Sep 12, 2023
1 parent f4c52f5 commit e2558d3
Showing 1 changed file with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/* eslint-disable max-lines */
import axios from "axios";
import { UUID } from "crypto";
import { useParams } from "next/navigation";
import { useParams, useRouter } from "next/navigation";
import { useCallback, useState } from "react";
import { useTranslation } from "react-i18next";

import { useChatApi } from "@/lib/api/chat/useChatApi";
import { useCrawlApi } from "@/lib/api/crawl/useCrawlApi";
import { useNotificationApi } from "@/lib/api/notification/useNotificationApi";
import { useUploadApi } from "@/lib/api/upload/useUploadApi";
import { useChatContext } from "@/lib/context";
import { useBrainContext } from "@/lib/context/BrainProvider/hooks/useBrainContext";
import { useToast } from "@/lib/hooks";

Expand All @@ -26,18 +28,25 @@ export const useKnowledgeUploader = ({
const { t } = useTranslation(["upload"]);
const { crawlWebsiteUrl } = useCrawlApi();
const { createChat } = useChatApi();

const { currentBrainId } = useBrainContext();
const { setNotifications } = useChatContext();
const { getChatNotifications } = useNotificationApi();
const router = useRouter();
const params = useParams();
const chatId = params?.chatId as UUID | undefined;

const { currentBrainId } = useBrainContext();
const addContent = (content: FeedItemType) => {
setContents((prevContents) => [...prevContents, content]);
};
const removeContent = (index: number) => {
setContents((prevContents) => prevContents.filter((_, i) => i !== index));
};

const fetchNotifications = async (currentChatId: UUID): Promise<void> => {
const fetchedNotifications = await getChatNotifications(currentChatId);
setNotifications(fetchedNotifications);
};

const crawlWebsiteHandler = useCallback(
async (url: string, brainId: UUID, chat_id: UUID) => {
// Configure parameters
Expand All @@ -50,21 +59,19 @@ export const useKnowledgeUploader = ({
};

try {
setHasPendingRequests(true);
await crawlWebsiteUrl({
brainId,
config,
chat_id,
});
await fetchNotifications(chat_id);
} catch (error: unknown) {
publish({
variant: "danger",
text: t("crawlFailed", {
message: JSON.stringify(error),
}),
});
} finally {
setHasPendingRequests(false);
}
},
[crawlWebsiteUrl, publish, t]
Expand All @@ -75,7 +82,6 @@ export const useKnowledgeUploader = ({
const formData = new FormData();
formData.append("uploadFile", file);
try {
setHasPendingRequests(true);
await uploadFile({
brainId,
formData,
Expand All @@ -99,8 +105,6 @@ export const useKnowledgeUploader = ({
text: t("error", { message: e }),
});
}
} finally {
setHasPendingRequests(false);
}
},
[publish, t, uploadFile]
Expand All @@ -125,6 +129,7 @@ export const useKnowledgeUploader = ({
}

try {
setHasPendingRequests(true);
const currentChatId = chatId ?? (await createChat("New Chat")).chat_id;
const uploadPromises = files.map((file) =>
uploadFileHandler(file, currentBrainId, currentChatId)
Expand All @@ -137,6 +142,12 @@ export const useKnowledgeUploader = ({

setContents([]);

if (chatId === undefined) {
void router.push(`/chat/${currentChatId}`);
} else {
await fetchNotifications(currentChatId);
}

publish({
variant: "success",
text: t("knowledgeUploaded"),
Expand All @@ -146,6 +157,8 @@ export const useKnowledgeUploader = ({
variant: "danger",
text: JSON.stringify(e),
});
} finally {
setHasPendingRequests(false);
}
};

Expand Down

0 comments on commit e2558d3

Please sign in to comment.