mirror of
https://github.com/StanGirard/quivr.git
synced 2024-12-29 14:25:20 +03:00
Ux improvment (#382)
* feat: make chat list hidden on mobile by default * feat: autoclose chat list on click * feat: move footer to chat lists bottom when user is logged in * feat: fix header when user is logged in * chore: refacto ChatMessages * feat: reverse chat list display on fetch * feat: fix new chat button
This commit is contained in:
parent
56c761ed0e
commit
dc64470d5d
@ -15,8 +15,8 @@ export default function ChatPage() {
|
||||
subtitle="Talk to a language model about your uploaded data"
|
||||
/>
|
||||
<ChatProvider>
|
||||
<div className="relative h-full w-full flex flex-col flex-1 items-center">
|
||||
<div className="h-full flex-1 w-full flex flex-col items-center">
|
||||
<div className="relative w-full flex flex-col flex-1 items-center">
|
||||
<div className="flex-1 w-full flex flex-col items-center">
|
||||
<ChatMessages />
|
||||
</div>
|
||||
<ChatInput />
|
||||
|
@ -0,0 +1,47 @@
|
||||
import { useCallback, useEffect, useRef } from "react";
|
||||
|
||||
import { useChat } from "@/app/chat/[chatId]/hooks/useChat";
|
||||
|
||||
//TODO: link this to chat input to get the right height
|
||||
const chatInputHeightEstimation = 100;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
export const useChatMessages = () => {
|
||||
const chatListRef = useRef<HTMLDivElement | null>(null);
|
||||
const { history } = useChat();
|
||||
|
||||
const scrollToBottom = useCallback(() => {
|
||||
if (chatListRef.current) {
|
||||
chatListRef.current.scrollTo({
|
||||
top: chatListRef.current.scrollHeight,
|
||||
behavior: "smooth",
|
||||
});
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const computeCardHeight = () => {
|
||||
if (chatListRef.current) {
|
||||
const cardTop = chatListRef.current.getBoundingClientRect().top;
|
||||
const windowHeight = window.innerHeight;
|
||||
const cardHeight = windowHeight - cardTop - chatInputHeightEstimation;
|
||||
chatListRef.current.style.height = `${cardHeight}px`;
|
||||
}
|
||||
};
|
||||
|
||||
computeCardHeight();
|
||||
window.addEventListener("resize", computeCardHeight);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener("resize", computeCardHeight);
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
scrollToBottom();
|
||||
}, [history, scrollToBottom]);
|
||||
|
||||
return {
|
||||
chatListRef,
|
||||
};
|
||||
};
|
@ -1,54 +1,42 @@
|
||||
/* eslint-disable */
|
||||
"use client";
|
||||
import { useCallback, useEffect, useRef } from "react";
|
||||
import React from "react";
|
||||
|
||||
import Card from "@/lib/components/ui/Card";
|
||||
import { useChat } from "../../[chatId]/hooks/useChat";
|
||||
import { ChatMessage } from "./ChatMessage";
|
||||
|
||||
import { ChatMessage } from "./components/ChatMessage";
|
||||
import { useChatMessages } from "./hooks/useChatMessages";
|
||||
import { useChatContext } from "../../[chatId]/context/ChatContext";
|
||||
|
||||
export const ChatMessages = (): JSX.Element => {
|
||||
const lastChatRef = useRef<HTMLDivElement | null>(null);
|
||||
const { history } = useChat();
|
||||
|
||||
const scrollToBottom = useCallback(() => {
|
||||
if (lastChatRef.current) {
|
||||
lastChatRef.current.scrollIntoView({
|
||||
behavior: "smooth",
|
||||
block: "start",
|
||||
});
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
scrollToBottom();
|
||||
}, [history, scrollToBottom]);
|
||||
const { chatListRef } = useChatMessages();
|
||||
const { history } = useChatContext();
|
||||
|
||||
return (
|
||||
<Card className="p-5 max-w-3xl w-full flex flex-col h-full mb-8">
|
||||
<Card
|
||||
className="p-5 max-w-3xl w-full flex flex-col mb-8 overflow-y-auto"
|
||||
ref={chatListRef}
|
||||
>
|
||||
<div className="flex-1">
|
||||
{history.length === 0 ? (
|
||||
<div className="text-center opacity-50">
|
||||
Ask a question, or describe a task.
|
||||
</div>
|
||||
) : (
|
||||
history.map(({ assistant, message_id, user_message }, idx) => (
|
||||
<>
|
||||
history.map(({ assistant, message_id, user_message }) => (
|
||||
<React.Fragment key={message_id}>
|
||||
<ChatMessage
|
||||
key={message_id}
|
||||
key={`user-${message_id}`}
|
||||
speaker={"user"}
|
||||
text={user_message}
|
||||
/>
|
||||
<ChatMessage
|
||||
key={message_id}
|
||||
key={`assistant-${message_id}`}
|
||||
speaker={"assistant"}
|
||||
text={assistant}
|
||||
/>
|
||||
</>
|
||||
</React.Fragment>
|
||||
))
|
||||
)}
|
||||
<div ref={lastChatRef} />
|
||||
</div>
|
||||
</Card>
|
||||
);
|
||||
};
|
||||
export default ChatMessages;
|
||||
|
@ -4,7 +4,7 @@ import { BsPlusSquare } from "react-icons/bs";
|
||||
export const NewChatButton = (): JSX.Element => (
|
||||
<Link
|
||||
href="/chat"
|
||||
className="px-4 py-2 mx-4 my-2 border border-primary bg-white dark:bg-black hover:text-white hover:bg-primary shadow-lg rounded-lg flex items-center justify-center sticky top-2 z-20"
|
||||
className="px-4 py-2 mx-4 my-1 border border-primary bg-white dark:bg-black hover:text-white hover:bg-primary shadow-lg rounded-lg flex items-center justify-center top-1 z-20"
|
||||
>
|
||||
<BsPlusSquare className="h-6 w-6 mr-2" /> New Chat
|
||||
</Link>
|
||||
|
@ -20,7 +20,6 @@ export const ChatsListItem = ({
|
||||
chat,
|
||||
deleteChat,
|
||||
}: ChatsListItemProps): JSX.Element => {
|
||||
console.log({ chat });
|
||||
const pathname = usePathname()?.split("/").at(-1);
|
||||
const selected = chat.chat_id === pathname;
|
||||
const [chatName, setChatName] = useState(chat.chat_name);
|
||||
|
@ -0,0 +1,38 @@
|
||||
import { DISCORD_URL, GITHUB_URL, TWITTER_URL } from "@/lib/config/CONSTANTS";
|
||||
|
||||
export const MiniFooter = (): JSX.Element => {
|
||||
return (
|
||||
<footer className="bg-white dark:bg-black border-t dark:border-white/10 mt-auto py-4">
|
||||
<div className="max-w-screen-xl mx-auto flex justify-center items-center gap-4">
|
||||
<a
|
||||
href={GITHUB_URL}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
aria-label="Quivr GitHub"
|
||||
>
|
||||
<img
|
||||
className="h-4 w-auto dark:invert"
|
||||
src="/github.svg"
|
||||
alt="GitHub"
|
||||
/>
|
||||
</a>
|
||||
<a
|
||||
href={TWITTER_URL}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
aria-label="Quivr Twitter"
|
||||
>
|
||||
<img className="h-4 w-auto" src="/twitter.svg" alt="Twitter" />
|
||||
</a>
|
||||
<a
|
||||
href={DISCORD_URL}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
aria-label="Quivr Discord"
|
||||
>
|
||||
<img className="h-4 w-auto" src="/discord.svg" alt="Discord" />
|
||||
</a>
|
||||
</div>
|
||||
</footer>
|
||||
);
|
||||
};
|
26
frontend/app/chat/components/ChatsList/hooks/useChatsList.ts
Normal file
26
frontend/app/chat/components/ChatsList/hooks/useChatsList.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { usePathname } from "next/navigation";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
import { useDevice } from "@/lib/hooks/useDevice";
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
export const useChatsList = () => {
|
||||
const { isMobile } = useDevice();
|
||||
|
||||
const [open, setOpen] = useState(!isMobile);
|
||||
|
||||
const pathname = usePathname();
|
||||
|
||||
useEffect(() => {
|
||||
setOpen(!isMobile);
|
||||
}, [isMobile]);
|
||||
|
||||
useEffect(() => {
|
||||
setOpen(!isMobile);
|
||||
}, [isMobile, pathname]);
|
||||
|
||||
return {
|
||||
open,
|
||||
setOpen,
|
||||
};
|
||||
};
|
@ -3,23 +3,21 @@
|
||||
import useChatsContext from "@/lib/context/ChatsProvider/hooks/useChatsContext";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { MotionConfig, motion } from "framer-motion";
|
||||
import { useState } from "react";
|
||||
import { MdChevronRight } from "react-icons/md";
|
||||
|
||||
import { NewChatButton } from "./NewChatButton";
|
||||
import { ChatsListItem } from "./components/ChatsListItem/";
|
||||
import { MiniFooter } from "./components/ChatsListItem/components/MiniFooter";
|
||||
import { useChatsList } from "./hooks/useChatsList";
|
||||
|
||||
export const ChatsList = (): JSX.Element => {
|
||||
const { allChats, deleteChat } = useChatsContext();
|
||||
|
||||
const [open, setOpen] = useState(true);
|
||||
|
||||
const { open, setOpen } = useChatsList();
|
||||
return (
|
||||
<MotionConfig transition={{ mass: 1, damping: 10 }}>
|
||||
<motion.div
|
||||
drag="x"
|
||||
dragConstraints={{ right: 0, left: 0 }}
|
||||
// dragSnapToOrigin
|
||||
dragElastic={0.15}
|
||||
onDragEnd={(event, info) => {
|
||||
if (info.offset.x > 100 && !open) {
|
||||
@ -37,30 +35,35 @@ export const ChatsList = (): JSX.Element => {
|
||||
boxShadow: open
|
||||
? "10px 10px 16px rgba(0, 0, 0, 0)"
|
||||
: "10px 10px 16px rgba(0, 0, 0, 0.5)",
|
||||
// shadow: open ? "none" : "10px 10px 16px black",
|
||||
}}
|
||||
className={cn("overflow-hidden")}
|
||||
>
|
||||
<div className="min-w-fit max-h-screen overflow-auto scrollbar">
|
||||
<aside className="relative max-w-xs w-full h-screen">
|
||||
<NewChatButton />
|
||||
<div className="flex flex-col gap-0">
|
||||
{allChats.map((chat) => (
|
||||
<ChatsListItem
|
||||
key={chat.chat_id}
|
||||
chat={chat}
|
||||
deleteChat={deleteChat}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</aside>
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
flex: 1,
|
||||
height: "90vh",
|
||||
}}
|
||||
>
|
||||
<NewChatButton />
|
||||
<div style={{ flex: 1, overflow: "scroll", height: "100%" }}>
|
||||
{allChats.map((chat) => (
|
||||
<ChatsListItem
|
||||
key={chat.chat_id}
|
||||
chat={chat}
|
||||
deleteChat={deleteChat}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
<MiniFooter />
|
||||
</div>
|
||||
</motion.div>
|
||||
<button
|
||||
onClick={() => {
|
||||
setOpen(!open);
|
||||
}}
|
||||
className="absolute left-full top-16 lg:top-0 text-3xl bg-black dark:bg-white text-white dark:text-black rounded-r-full p-3 pl-1"
|
||||
className="absolute left-full top-16 text-3xl bg-black dark:bg-white text-white dark:text-black rounded-r-full p-3 pl-1"
|
||||
>
|
||||
<motion.div
|
||||
whileTap={{ scale: 0.9 }}
|
||||
|
@ -1,4 +1,4 @@
|
||||
export * from "./ChatInput";
|
||||
export * from "./ChatMessages";
|
||||
export * from "./ChatMessages/ChatInput";
|
||||
export * from "./ChatMessages/ChatMessage";
|
||||
export * from "./ChatMessages/components/ChatMessage";
|
||||
export * from "./ChatsList";
|
||||
|
@ -1,9 +1,19 @@
|
||||
"use client";
|
||||
import { DISCORD_URL, GITHUB_URL, TWITTER_URL } from "@/lib/config/CONSTANTS";
|
||||
import { useSupabase } from "@/lib/context/SupabaseProvider";
|
||||
|
||||
const Footer = (): JSX.Element => {
|
||||
const { session } = useSupabase();
|
||||
|
||||
if (session?.user !== undefined) {
|
||||
return <div />;
|
||||
}
|
||||
|
||||
return (
|
||||
<footer className="bg-white dark:bg-black border-t dark:border-white/10 mt-auto py-10">
|
||||
<footer className="bg-white dark:bg-black border-t dark:border-white/10 mt-auto py-4">
|
||||
<div className="max-w-screen-xl mx-auto flex justify-center items-center gap-4">
|
||||
<a
|
||||
href="https://github.com/stangirard/quivr"
|
||||
href={GITHUB_URL}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
aria-label="Quivr GitHub"
|
||||
@ -15,13 +25,21 @@ const Footer = (): JSX.Element => {
|
||||
/>
|
||||
</a>
|
||||
<a
|
||||
href="https://twitter.com/quivr_brain"
|
||||
href={TWITTER_URL}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
aria-label="Quivr Twitter"
|
||||
>
|
||||
<img className="h-8 w-auto" src="/twitter.svg" alt="Twitter" />
|
||||
</a>
|
||||
<a
|
||||
href={DISCORD_URL}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
aria-label="Quivr Discord"
|
||||
>
|
||||
<img className="h-8 w-auto" src="/discord.svg" alt="Discord" />
|
||||
</a>
|
||||
</div>
|
||||
</footer>
|
||||
);
|
||||
|
@ -1,12 +1,19 @@
|
||||
/* eslint-disable */
|
||||
import { useSupabase } from "@/lib/context/SupabaseProvider";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
|
||||
export const useHeader = () => {
|
||||
const [hidden, setHidden] = useState(false);
|
||||
const scrollPos = useRef<number>(0);
|
||||
|
||||
const { session } = useSupabase();
|
||||
useEffect(() => {
|
||||
const handleScroll = (e: Event) => {
|
||||
if (session?.user !== undefined) {
|
||||
setHidden(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const target = e.currentTarget as Window;
|
||||
if (target.scrollY > scrollPos.current) {
|
||||
setHidden(true);
|
||||
|
@ -15,7 +15,7 @@ export const Header = ({
|
||||
y: hidden ? "-100%" : "0%",
|
||||
transition: { ease: "circOut" },
|
||||
}}
|
||||
className="sticky top-0 w-full border-b border-b-black/10 dark:border-b-white/25 bg-white dark:bg-black z-20"
|
||||
className="sticky top-0 w-full border-b border-b-black/10 dark:border-b-white/25 bg-white dark:bg-black z-[1200]"
|
||||
>
|
||||
<nav className="max-w-screen-xl mx-auto py-1 flex items-center justify-between gap-8">
|
||||
{children}
|
||||
|
3
frontend/lib/config/CONSTANTS.ts
Normal file
3
frontend/lib/config/CONSTANTS.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export const GITHUB_URL = "https://github.com/stangirard/quivr";
|
||||
export const TWITTER_URL = "https://twitter.com/quivr_brain";
|
||||
export const DISCORD_URL = "https://discord.gg/HUpRgp2HG8";
|
@ -22,7 +22,7 @@ export default function useChats() {
|
||||
const response = await axiosInstance.get<{
|
||||
chats: ChatEntity[];
|
||||
}>(`/chat`);
|
||||
setAllChats(response.data.chats);
|
||||
setAllChats(response.data.chats.reverse());
|
||||
console.log("Fetched all chats");
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
25
frontend/lib/hooks/useDevice.ts
Normal file
25
frontend/lib/hooks/useDevice.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export const useDevice = (): { isMobile: boolean } => {
|
||||
const [isMobile, setIsMobile] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const handleResize = () => {
|
||||
const screenWidth = window.innerWidth;
|
||||
setIsMobile(screenWidth < 576);
|
||||
};
|
||||
|
||||
// Initial check
|
||||
handleResize();
|
||||
|
||||
// Event listener for screen resize
|
||||
window.addEventListener("resize", handleResize);
|
||||
|
||||
// Clean up event listener on component unmount
|
||||
return () => {
|
||||
window.removeEventListener("resize", handleResize);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return { isMobile };
|
||||
};
|
8
frontend/public/discord.svg
Normal file
8
frontend/public/discord.svg
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->
|
||||
<svg width="800px" height="800px" viewBox="0 -28.5 256 256" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid">
|
||||
<g>
|
||||
<path d="M216.856339,16.5966031 C200.285002,8.84328665 182.566144,3.2084988 164.041564,0 C161.766523,4.11318106 159.108624,9.64549908 157.276099,14.0464379 C137.583995,11.0849896 118.072967,11.0849896 98.7430163,14.0464379 C96.9108417,9.64549908 94.1925838,4.11318106 91.8971895,0 C73.3526068,3.2084988 55.6133949,8.86399117 39.0420583,16.6376612 C5.61752293,67.146514 -3.4433191,116.400813 1.08711069,164.955721 C23.2560196,181.510915 44.7403634,191.567697 65.8621325,198.148576 C71.0772151,190.971126 75.7283628,183.341335 79.7352139,175.300261 C72.104019,172.400575 64.7949724,168.822202 57.8887866,164.667963 C59.7209612,163.310589 61.5131304,161.891452 63.2445898,160.431257 C105.36741,180.133187 151.134928,180.133187 192.754523,160.431257 C194.506336,161.891452 196.298154,163.310589 198.110326,164.667963 C191.183787,168.842556 183.854737,172.420929 176.223542,175.320965 C180.230393,183.341335 184.861538,190.991831 190.096624,198.16893 C211.238746,191.588051 232.743023,181.531619 254.911949,164.955721 C260.227747,108.668201 245.831087,59.8662432 216.856339,16.5966031 Z M85.4738752,135.09489 C72.8290281,135.09489 62.4592217,123.290155 62.4592217,108.914901 C62.4592217,94.5396472 72.607595,82.7145587 85.4738752,82.7145587 C98.3405064,82.7145587 108.709962,94.5189427 108.488529,108.914901 C108.508531,123.290155 98.3405064,135.09489 85.4738752,135.09489 Z M170.525237,135.09489 C157.88039,135.09489 147.510584,123.290155 147.510584,108.914901 C147.510584,94.5396472 157.658606,82.7145587 170.525237,82.7145587 C183.391518,82.7145587 193.761324,94.5189427 193.539891,108.914901 C193.539891,123.290155 183.391518,135.09489 170.525237,135.09489 Z" fill="#5865F2" fill-rule="nonzero">
|
||||
</path>
|
||||
</g>
|
||||
</svg>
|
After Width: | Height: | Size: 2.0 KiB |
Loading…
Reference in New Issue
Block a user