Skip to content

Commit

Permalink
feat: add polling logic on pending notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
mamadoudicko committed Sep 12, 2023
1 parent 419e2c1 commit f4c52f5
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ export const ActionsBar = (): JSX.Element => {
shouldDisplayUploadCard,
setShouldDisplayUploadCard,
hasPendingRequests,
setHasPendingRequests,
} = useActionBar();
const { addContent, contents, feedBrain, removeContent } =
useKnowledgeUploader();
useKnowledgeUploader({
setHasPendingRequests,
});

const { t } = useTranslation(["chat"]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ export const useActionBar = () => {
shouldDisplayUploadCard,
setShouldDisplayUploadCard,
hasPendingRequests,
setHasPendingRequests,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ import { useToast } from "@/lib/hooks";

import { FeedItemCrawlType, FeedItemType, FeedItemUploadType } from "../types";

type UseKnowledgeUploaderProps = {
setHasPendingRequests: (hasPendingRequests: boolean) => void;
};
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const useKnowledgeUploader = () => {
export const useKnowledgeUploader = ({
setHasPendingRequests,
}: UseKnowledgeUploaderProps) => {
const [contents, setContents] = useState<FeedItemType[]>([]);
const { publish } = useToast();
const { uploadFile } = useUploadApi();
Expand Down Expand Up @@ -45,6 +50,7 @@ export const useKnowledgeUploader = () => {
};

try {
setHasPendingRequests(true);
await crawlWebsiteUrl({
brainId,
config,
Expand All @@ -57,6 +63,8 @@ export const useKnowledgeUploader = () => {
message: JSON.stringify(error),
}),
});
} finally {
setHasPendingRequests(false);
}
},
[crawlWebsiteUrl, publish, t]
Expand All @@ -67,6 +75,7 @@ export const useKnowledgeUploader = () => {
const formData = new FormData();
formData.append("uploadFile", file);
try {
setHasPendingRequests(true);
await uploadFile({
brainId,
formData,
Expand All @@ -90,6 +99,8 @@ export const useKnowledgeUploader = () => {
text: t("error", { message: e }),
});
}
} finally {
setHasPendingRequests(false);
}
},
[publish, t, uploadFile]
Expand Down
45 changes: 42 additions & 3 deletions frontend/app/chat/[chatId]/hooks/useSelectedChatPage.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,59 @@
import { useQuery } from "@tanstack/react-query";
import { useParams } from "next/navigation";
import { useEffect } from "react";

import { useChatApi } from "@/lib/api/chat/useChatApi";
import { useNotificationApi } from "@/lib/api/notification/useNotificationApi";
import { useChatContext } from "@/lib/context";

import { getChatNotificationsQueryKey } from "../utils/getChatNotificationsQueryKey";
import { getMessagesFromChatItems } from "../utils/getMessagesFromChatItems";
import { getNotificationsFromChatItems } from "../utils/getNotificationsFromChatItems";

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const useSelectedChatPage = () => {
const { setMessages, setNotifications } = useChatContext();
const { setMessages, setNotifications, notifications } = useChatContext();
const { getChatItems } = useChatApi();

const { getChatNotifications } = useNotificationApi();
const params = useParams();

const chatId = params?.chatId as string | undefined;

const chatNotificationsQueryKey = getChatNotificationsQueryKey(chatId ?? "");
const { data: fetchedNotifications = [] } = useQuery({
queryKey: [chatNotificationsQueryKey],
enabled: notifications.length > 0,
queryFn: () => {
if (chatId === undefined) {
return [];
}

return getChatNotifications(chatId);
},
refetchInterval: () => {
if (notifications.length === 0) {
return false;
}
const hasAPendingNotification = notifications.find(
(item) => item.status === "Pending"
);

if (hasAPendingNotification) {
//30 seconds
return 30_000;
}

return false;
},
});

useEffect(() => {
if (fetchedNotifications.length === 0) {
return;
}
setNotifications(fetchedNotifications);
}, [fetchedNotifications]);

useEffect(() => {
const fetchHistory = async () => {
if (chatId === undefined) {
Expand All @@ -30,5 +69,5 @@ export const useSelectedChatPage = () => {
setNotifications(getNotificationsFromChatItems(chatItems));
};
void fetchHistory();
}, [chatId, setMessages]);
}, [chatId]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const getChatNotificationsQueryKey = (chatId: string): string =>
`notifications-${chatId}`;
34 changes: 22 additions & 12 deletions frontend/app/chat/components/ChatsList/__tests__/ChatsList.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable max-lines */
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { act, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";

Expand All @@ -11,6 +12,7 @@ import * as useChatsListModule from "../hooks/useChatsList";
import { ChatsList } from "../index";

const getChatsMock = vi.fn(() => []);
const queryClient = new QueryClient();

const setOpenMock = vi.fn();

Expand Down Expand Up @@ -56,9 +58,11 @@ describe("ChatsList", () => {

it("should render correctly", () => {
const { getByTestId } = render(
<ChatProviderMock>
<ChatsList />
</ChatProviderMock>
<QueryClientProvider client={queryClient}>
<ChatProviderMock>
<ChatsList />
</ChatProviderMock>
</QueryClientProvider>
);
const chatsList = getByTestId("chats-list");
expect(chatsList).toBeDefined();
Expand All @@ -72,9 +76,11 @@ describe("ChatsList", () => {

it("renders the chats list with correct number of items", () => {
render(
<ChatProviderMock>
<ChatsList />
</ChatProviderMock>
<QueryClientProvider client={queryClient}>
<ChatProviderMock>
<ChatsList />
</ChatProviderMock>
</QueryClientProvider>
);
const chatItems = screen.getAllByTestId("chats-list-item");
expect(chatItems).toHaveLength(2);
Expand All @@ -88,9 +94,11 @@ describe("ChatsList", () => {

await act(() =>
render(
<ChatProviderMock>
(<ChatsList />)
</ChatProviderMock>
<QueryClientProvider client={queryClient}>
<ChatProviderMock>
(<ChatsList />)
</ChatProviderMock>
</QueryClientProvider>
)
);

Expand All @@ -109,9 +117,11 @@ describe("ChatsList", () => {
}));
await act(() =>
render(
<ChatProviderMock>
<ChatsList />
</ChatProviderMock>
<QueryClientProvider client={queryClient}>
<ChatProviderMock>
<ChatsList />
</ChatProviderMock>
</QueryClientProvider>
)
);

Expand Down

0 comments on commit f4c52f5

Please sign in to comment.