quivr/frontend/app/chat/[chatId]/page.tsx
Antoine Dewez ff914b9d0f
fix(frontend): double file upload on drag and drop (#2284)
# Description

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context.

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):
2024-03-04 11:06:08 -08:00

115 lines
3.6 KiB
TypeScript

"use client";
import { UUID } from "crypto";
import { useEffect } from "react";
import { AddBrainModal } from "@/lib/components/AddBrainModal";
import { useBrainCreationContext } from "@/lib/components/AddBrainModal/brainCreation-provider";
import PageHeader from "@/lib/components/PageHeader/PageHeader";
import { UploadDocumentModal } from "@/lib/components/UploadDocumentModal/UploadDocumentModal";
import { useChatContext } from "@/lib/context";
import { useBrainContext } from "@/lib/context/BrainProvider/hooks/useBrainContext";
import { useKnowledgeToFeedContext } from "@/lib/context/KnowledgeToFeedProvider/hooks/useKnowledgeToFeedContext";
import { useDevice } from "@/lib/hooks/useDevice";
import { useCustomDropzone } from "@/lib/hooks/useDropzone";
import { ButtonType } from "@/lib/types/QuivrButton";
import { cn } from "@/lib/utils";
import { ActionsBar } from "./components/ActionsBar";
import { ChatDialogueArea } from "./components/ChatDialogueArea/ChatDialogue";
import Sources from "./components/Sources/Sources";
import { useChatNotificationsSync } from "./hooks/useChatNotificationsSync";
import styles from "./page.module.scss";
const SelectedChatPage = (): JSX.Element => {
const { getRootProps } = useCustomDropzone();
const { isMobile } = useDevice();
const { setShouldDisplayFeedCard, shouldDisplayFeedCard } =
useKnowledgeToFeedContext();
const { setIsBrainCreationModalOpened } = useBrainCreationContext();
const { currentBrain, setCurrentBrainId } = useBrainContext();
const { messages } = useChatContext();
const { sourcesMessageIndex, setSourcesMessageIndex } = useChatContext();
useChatNotificationsSync();
const buttons: ButtonType[] = [
{
label: "Create brain",
color: "primary",
onClick: () => {
setIsBrainCreationModalOpened(true);
},
iconName: "brain",
},
{
label: "Add knowledge",
color: "primary",
onClick: () => {
setShouldDisplayFeedCard(true);
},
iconName: "uploadFile",
hidden: !currentBrain?.max_files,
},
{
label: "Manage current brain",
color: "primary",
onClick: () => {
window.location.href = `/studio/${currentBrain?.id}`;
},
iconName: "edit",
},
];
useEffect(() => {
if (!currentBrain && messages.length > 0) {
setCurrentBrainId(messages[messages.length - 1].brain_id as UUID);
}
}, [messages]);
useEffect(() => {
return () => {
setSourcesMessageIndex(undefined);
};
}, []);
return (
<div className={styles.main_container}>
<div className={styles.page_header}>
<PageHeader iconName="chat" label="Chat" buttons={buttons} />
</div>
<div
className={styles.chat_page_container}
{...(shouldDisplayFeedCard ? {} : getRootProps())}
>
<div
className={cn(
"flex flex-col flex-1 items-center justify-stretch w-full h-full overflow-hidden",
"dark:bg-black transition-colors ease-out duration-500"
)}
>
<div
className={`flex flex-col flex-1 w-full max-w-4xl h-full dark:shadow-primary/25 overflow-hidden`}
>
<div className="flex flex-1 flex-col overflow-y-auto">
<ChatDialogueArea />
</div>
<ActionsBar />
</div>
</div>
{!isMobile && sourcesMessageIndex !== undefined && (
<div className={styles.sources_wrapper}>
<Sources />
</div>
)}
<UploadDocumentModal />
<AddBrainModal />
</div>
</div>
);
};
export default SelectedChatPage;