From 28212efcbfb025518c7daf8dee89866ac3edc038 Mon Sep 17 00:00:00 2001 From: Priyanshu Verma Date: Tue, 29 Oct 2024 12:19:14 +0000 Subject: [PATCH] fixes: AI engine section and chats auto scroll --- .gitignore | 3 +- client/bun.lockb | Bin 316546 -> 316546 bytes client/src/contexts/settings-context.tsx | 2 + client/src/pages/Chatbot.tsx | 25 +++++++- client/src/pages/Imagine.tsx | 74 ++++++++++++++++------- 5 files changed, 77 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 4c8dc75..5b59762 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ .env \myenv -/tailwind/* \ No newline at end of file +/tailwind/* +*.log \ No newline at end of file diff --git a/client/bun.lockb b/client/bun.lockb index 40bd376602b017a82bde3fe52bbef197751e3677..2c4007ba07443616c5963b414d5d0d2648d2ff54 100755 GIT binary patch delta 33 ocmZo#DcrPDxS@ryg=q_Ov6;}3iI|P6&8gJ0J1Czwg3PC delta 33 lcmZo#DcrPDxS@ryg=q_OvD0~oX?sW5L(Qejcp005vM2xtHR diff --git a/client/src/contexts/settings-context.tsx b/client/src/contexts/settings-context.tsx index dfd1747..beca7c6 100644 --- a/client/src/contexts/settings-context.tsx +++ b/client/src/contexts/settings-context.tsx @@ -34,6 +34,8 @@ export const SettingsProvider: React.FC<{ children: React.ReactNode }> = ({ if (savedConfigs) { try { const parsedConfigs = JSON.parse(savedConfigs); + + setCurrentConfig(parsedConfigs[0]); // Optionally, you can check if parsedConfigs is an array or object if (Array.isArray(parsedConfigs) || typeof parsedConfigs === "object") { setConfigurations(parsedConfigs); diff --git a/client/src/pages/Chatbot.tsx b/client/src/pages/Chatbot.tsx index bda7659..1ab75a2 100644 --- a/client/src/pages/Chatbot.tsx +++ b/client/src/pages/Chatbot.tsx @@ -15,7 +15,7 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import axios from "axios"; import { ArrowLeft, Loader2, SendIcon, Trash2 } from "lucide-react"; -import { useRef, useState } from "react"; +import { useCallback, useEffect, useRef, useState } from "react"; import { useForm } from "react-hook-form"; import toast from "react-hot-toast"; import { useNavigate, useParams } from "react-router-dom"; @@ -32,6 +32,7 @@ export default function ChatbotPage() { queryKey: ["chatbot", id], queryFn: () => fetchChatbotData(id), }); + const messageEl = useRef(null); const singleClickTimeout = useRef(null); const { currentConfig } = useSettings(); const [loading, setLoading] = useState(false); // Loading state for request @@ -46,11 +47,25 @@ export default function ChatbotPage() { const mutation = useMutation({ mutationFn: deleteAllChats, - onSuccess: () => rq.invalidateQueries({ queryKey: ["chatbot", id] }), + onSuccess: async () => { + await rq.invalidateQueries({ queryKey: ["chatbot", id] }); + }, }); + const scrollToBottom = useCallback(() => { + if (messageEl.current) { + // @ts-ignore + messageEl.current.scrollTop = messageEl.current.scrollHeight; + } + }, []); + + useEffect(() => { + scrollToBottom(); + }, [data?.chats, scrollToBottom]); + async function onSubmit(values: z.infer) { try { + if (!values.query.trim()) return; if (currentConfig == null) { toast.error("Please Select AI engine in settings"); return; @@ -140,7 +155,11 @@ export default function ChatbotPage() { -
+
{data ? ( <> {data.chats.map((chat) => ( diff --git a/client/src/pages/Imagine.tsx b/client/src/pages/Imagine.tsx index 4584a51..37dbf87 100644 --- a/client/src/pages/Imagine.tsx +++ b/client/src/pages/Imagine.tsx @@ -1,5 +1,11 @@ import Separator from "@/components/Separator"; import { Button } from "@/components/ui/button"; +import { + Card, + CardContent, + CardFooter, + CardHeader, +} from "@/components/ui/card"; import { Form, FormControl, @@ -15,7 +21,7 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import axios from "axios"; import { ArrowLeft, Download, Loader2, SendIcon } from "lucide-react"; -import { useState } from "react"; +import { useCallback, useEffect, useRef, useState } from "react"; import { useForm } from "react-hook-form"; import toast from "react-hot-toast"; import { Link } from "react-router-dom"; @@ -26,6 +32,7 @@ export default function ImaginePage() { queryKey: ["images"], queryFn: fetchImagesData, }); + const messageEl = useRef(null); const [loading, setLoading] = useState(false); // Loading state for request const rq = useQueryClient(); const form = useForm>({ @@ -35,6 +42,17 @@ export default function ImaginePage() { }, }); + const scrollToBottom = useCallback(() => { + if (messageEl.current) { + // @ts-ignore + messageEl.current.scrollTop = messageEl.current.scrollHeight; + } + }, []); + + useEffect(() => { + scrollToBottom(); + }, [data, scrollToBottom]); + async function onSubmit(values: z.infer) { try { const token = localStorage.getItem("token"); @@ -86,7 +104,10 @@ export default function ImaginePage() {
-
+
{!data ? ( ) : ( @@ -98,28 +119,35 @@ export default function ImaginePage() { )} {data.map((image) => ( <> -
-
- {image.prompt} -

{image.prompt}

-
- - - + + +
+ {image.prompt}
-
-
+ + +

+ {image.prompt} +

+
+ + + + + + ))}