Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Menu bar #1885

Merged
merged 18 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions frontend/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { PropsWithChildren, useEffect } from "react";

import { Menu } from "@/lib/components/Menu/Menu";
import { NotificationBanner } from "@/lib/components/NotificationBanner";
import { BrainProvider } from "@/lib/context";
import { useBrainContext } from "@/lib/context/BrainProvider/hooks/useBrainContext";
import { SideBarProvider } from "@/lib/context/SidebarProvider/sidebar-provider";
import { useSupabase } from "@/lib/context/SupabaseProvider";
import { UpdateMetadata } from "@/lib/helpers/updateMetadata";
import { usePageTracking } from "@/services/analytics/june/usePageTracking";

import "../lib/config/LocaleConfig/i18n";

// This wrapper is used to make effect calls at a high level in app rendering.
Expand All @@ -27,10 +31,14 @@ const App = ({ children }: PropsWithChildren): JSX.Element => {
}, [session]);

return (
<>
{children}
<UpdateMetadata />
</>
<div className="flex flex-1 flex-col overflow-auto">
<NotificationBanner />
<div className="relative h-full w-full flex justify-stretch items-stretch overflow-auto">
<Menu />
<div className="flex-1">{children}</div>
<UpdateMetadata />
</div>
</div>
);
};

Expand All @@ -40,7 +48,9 @@ const AppWithQueryClient = ({ children }: PropsWithChildren): JSX.Element => {
return (
<QueryClientProvider client={queryClient}>
<BrainProvider>
<App>{children}</App>
<SideBarProvider>
<App>{children}</App>
</SideBarProvider>
</BrainProvider>
</QueryClientProvider>
);
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/brains-management/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const Layout = ({ children }: LayoutProps): JSX.Element => {
}

return (
<div className="relative h-full w-full flex justify-stretch items-stretch">
<div className="relative h-full w-full flex justify-stretch items-stretch overflow-scroll">
<BrainsList />
{children}
</div>
Expand Down
5 changes: 4 additions & 1 deletion frontend/app/chat/[chatId]/__tests__/page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ChatProviderMock,
} from "@/lib/context/ChatProvider/mocks/ChatProviderMock";
import { KnowledgeToFeedProvider } from "@/lib/context/KnowledgeToFeedProvider";
import { SideBarProvider } from "@/lib/context/SidebarProvider/sidebar-provider";
import {
SupabaseContextMock,
SupabaseProviderMock,
Expand Down Expand Up @@ -87,7 +88,9 @@ describe("Chat page", () => {
<ChatProviderMock>
<SupabaseProviderMock>
<BrainProviderMock>
<SelectedChatPage />,
<SideBarProvider>
<SelectedChatPage />,
</SideBarProvider>
</BrainProviderMock>
</SupabaseProviderMock>
</ChatProviderMock>
Expand Down
10 changes: 5 additions & 5 deletions frontend/app/chat/[chatId]/components/ActionsBar/ActionsBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { AnimatePresence, motion } from "framer-motion";
import { useTranslation } from "react-i18next";
import { AiOutlineLoading3Quarters } from "react-icons/ai";

import { useKnowledgeToFeedContext } from "@/lib/context/KnowledgeToFeedProvider/hooks/useKnowledgeToFeedContext";

import { ChatInput, KnowledgeToFeed } from "./components";
import { useActionBar } from "./hooks/useActionBar";

export const ActionsBar = (): JSX.Element => {
const { hasPendingRequests, setHasPendingRequests, shouldDisplayFeedCard } =
useActionBar();
const { hasPendingRequests, setHasPendingRequests } = useActionBar();
const { shouldDisplayFeedCard } = useKnowledgeToFeedContext();

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

Expand Down Expand Up @@ -39,9 +41,7 @@ export const ActionsBar = (): JSX.Element => {
</motion.div>
</AnimatePresence>
)}
{!shouldDisplayFeedCard && (
<ChatInput shouldDisplayFeedOrSecretsCard={shouldDisplayFeedCard} />
)}
{!shouldDisplayFeedCard && <ChatInput />}
</div>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { ChangeBrainButton } from "./components/ChangeBrainButton";
import { ChatHistoryButton } from "./components/ChatHistoryButton/ChatHistoryButton";
import { ConfigModal } from "./components/ConfigModal";
import { FeedCardTrigger } from "./components/FeedCardTrigger";
import { NewDiscussionButton } from "./components/NewDiscussionButton";
import { SelectedBrainTag } from "./components/SelectedBrainTag";

Expand Down Expand Up @@ -40,6 +41,7 @@ export const ActionsModal = (): JSX.Element => {
>
<SelectedBrainTag />
<NewDiscussionButton />
<FeedCardTrigger />
<ChatHistoryButton />
<ConfigModal />
<ChangeBrainButton />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { LuChevronRight } from "react-icons/lu";

import { useKnowledgeToFeedContext } from "@/lib/context/KnowledgeToFeedProvider/hooks/useKnowledgeToFeedContext";

import { useFeedCardTrigger } from "./hooks/useFeedCardTrigger";
import { Button } from "../Button";

export const FeedCardTrigger = (): JSX.Element => {
const { setShouldDisplayFeedCard } = useKnowledgeToFeedContext();
const { label, Icon } = useFeedCardTrigger();

return (
<Button
label={label}
startIcon={<Icon size={18} />}
endIcon={<LuChevronRight size={18} />}
className="w-full"
onClick={() => setShouldDisplayFeedCard(true)}
/>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Fragment } from "react";

import { useBrainContext } from "@/lib/context/BrainProvider/hooks/useBrainContext";

import { useFeedCardTriggerUtils } from "./useFeedCardTriggerUtils";

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const useFeedCardTrigger = () => {
const { brainTypeToIcon, brainTypeToLabel } = useFeedCardTriggerUtils();
const { currentBrain } = useBrainContext();

const isBrainTypeDefined = currentBrain?.brain_type !== undefined;

return {
label: isBrainTypeDefined ? brainTypeToLabel[currentBrain.brain_type] : "",
Icon: isBrainTypeDefined
? brainTypeToIcon[currentBrain.brain_type]
: Fragment,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useTranslation } from "react-i18next";
import { IconType } from "react-icons/lib";
import { LuBot, LuFilePlus, LuUnlock } from "react-icons/lu";

import { BrainType } from "@/lib/types/brainConfig";

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const useFeedCardTriggerUtils = () => {
const { t } = useTranslation(["chat", "brain"]);

const brainTypeToLabel: Record<BrainType, string> = {
doc: t("chat:add_document"),
api: t("brain:update_secrets_button"),
composite: t("brain:manage_brain"),
};

const brainTypeToIcon: Record<BrainType, IconType> = {
doc: LuFilePlus,
api: LuUnlock,
composite: LuBot,
};

return {
brainTypeToIcon,
brainTypeToLabel,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./FeedCardTrigger";
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useTranslation } from "react-i18next";
import { LuPanelLeftClose, LuPanelRightClose } from "react-icons/lu";

import Button from "@/lib/components/ui/Button";
import { useSideBarContext } from "@/lib/context/SidebarProvider/hooks/useSideBarContext";

export const MenuControlButton = (): JSX.Element => {
const { isOpened, setIsOpened } = useSideBarContext();
const Icon = isOpened ? LuPanelLeftClose : LuPanelRightClose;
const { t } = useTranslation("chat");

return (
<Button
variant="tertiary"
className="px-2 py-0"
type="button"
onClick={() => setIsOpened(!isOpened)}
>
<div className="flex flex-col items-center justify-center gap-1">
<Icon className="text-2xl md:text-3xl self-center text-accent" />
<span className="text-xs">{t("menu")}</span>
</div>
</Button>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,18 @@
import { useTranslation } from "react-i18next";

import Button from "@/lib/components/ui/Button";
import { useBrainContext } from "@/lib/context/BrainProvider/hooks/useBrainContext";
import { useKnowledgeToFeedContext } from "@/lib/context/KnowledgeToFeedProvider/hooks/useKnowledgeToFeedContext";
import { getBrainIconFromBrainType } from "@/lib/helpers/getBrainIconFromBrainType";

import { OnboardingQuestions } from "./components";
import { ActionsModal } from "./components/ActionsModal/ActionsModal";
import { ChatEditor } from "./components/ChatEditor/ChatEditor";
import { MenuControlButton } from "./components/MenuControlButton";
import { useChatInput } from "./hooks/useChatInput";

type ChatInputProps = {
shouldDisplayFeedOrSecretsCard: boolean;
};

export const ChatInput = ({
shouldDisplayFeedOrSecretsCard,
}: ChatInputProps): JSX.Element => {
export const ChatInput = (): JSX.Element => {
const { setMessage, submitQuestion, generatingAnswer, message } =
useChatInput();

const { t } = useTranslation(["chat"]);
const { currentBrainDetails } = useBrainContext();
const { setShouldDisplayFeedCard } = useKnowledgeToFeedContext();

return (
<>
Expand All @@ -37,18 +27,7 @@ export const ChatInput = ({
}}
className="sticky bottom-0 bg-white dark:bg-black w-full flex items-center gap-2 z-20 p-2"
>
{!shouldDisplayFeedOrSecretsCard && (
<Button
className="p-0"
variant={"tertiary"}
data-testid="feed-button"
type="button"
onClick={() => setShouldDisplayFeedCard(true)}
tooltip={t("add_content_card_button_tooltip")}
>
{getBrainIconFromBrainType(currentBrainDetails?.brain_type)}
</Button>
)}
<MenuControlButton />

<div className="flex flex-1">
<ChatEditor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useEffect, useState } from "react";

import { useChatContext } from "@/lib/context";
import { useKnowledgeToFeedContext } from "@/lib/context/KnowledgeToFeedProvider/hooks/useKnowledgeToFeedContext";

import { checkIfHasPendingRequest } from "../utils/checkIfHasPendingRequest";

Expand All @@ -10,15 +9,12 @@ export const useActionBar = () => {
const [hasPendingRequests, setHasPendingRequests] = useState(false);
const { notifications } = useChatContext();

const { shouldDisplayFeedCard } = useKnowledgeToFeedContext();

useEffect(() => {
setHasPendingRequests(checkIfHasPendingRequest(notifications));
}, [notifications]);

return {
hasPendingRequests,
setHasPendingRequests,
shouldDisplayFeedCard,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ChatProviderMock,
} from "@/lib/context/ChatProvider/mocks/ChatProviderMock";
import { KnowledgeToFeedProvider } from "@/lib/context/KnowledgeToFeedProvider";
import { SideBarProvider } from "@/lib/context/SidebarProvider/sidebar-provider";
import { SupabaseContextMock } from "@/lib/context/SupabaseProvider/mocks/SupabaseProviderMock";

vi.mock("@/lib/context/SupabaseProvider/supabase-provider", () => ({
Expand Down Expand Up @@ -91,7 +92,9 @@ describe("ChatsList", () => {
<KnowledgeToFeedProvider>
<ChatProviderMock>
<BrainProviderMock>
<ChatsList />
<SideBarProvider>
<ChatsList />
</SideBarProvider>
</BrainProviderMock>
</ChatProviderMock>
</KnowledgeToFeedProvider>
Expand All @@ -110,7 +113,9 @@ describe("ChatsList", () => {
<KnowledgeToFeedProvider>
<ChatProviderMock>
<BrainProviderMock>
<ChatsList />
<SideBarProvider>
<ChatsList />
</SideBarProvider>
</BrainProviderMock>
</ChatProviderMock>
</KnowledgeToFeedProvider>
Expand All @@ -133,7 +138,9 @@ describe("ChatsList", () => {
<KnowledgeToFeedProvider>
<ChatProviderMock>
<BrainProviderMock>
<ChatsList />
<SideBarProvider>
<ChatsList />
</SideBarProvider>
</BrainProviderMock>
</ChatProviderMock>
</KnowledgeToFeedProvider>
Expand Down
1 change: 0 additions & 1 deletion frontend/app/chat/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "./ChatsList";
export * from "./NotificationBanner";
4 changes: 0 additions & 4 deletions frontend/app/chat/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { ChatsProvider } from "@/lib/context/ChatsProvider/chats-provider";
import { useSupabase } from "@/lib/context/SupabaseProvider";
import { redirectToLogin } from "@/lib/router/redirectToLogin";

import { ChatsList, NotificationBanner } from "./components";

interface LayoutProps {
children?: ReactNode;
}
Expand All @@ -23,9 +21,7 @@ const Layout = ({ children }: LayoutProps): JSX.Element => {
<KnowledgeToFeedProvider>
<ChatsProvider>
<ChatProvider>
<NotificationBanner />
<div className="relative h-full w-full flex justify-stretch items-stretch overflow-auto">
<ChatsList />
{children}
</div>
</ChatProvider>
Expand Down
Loading