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

Fix scrollbar in chat UI #62

Merged
merged 1 commit 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
34 changes: 30 additions & 4 deletions src/client/components/ConversationWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { useState, useCallback } from "react";
import { useState, useCallback, useRef, useEffect } from "react";
import { useParams } from "react-router";
import { Redirect } from "react-router-dom";

Expand All @@ -21,12 +21,13 @@ export default function ConversationWrapper() {
const { id }: { id: string } = useParams();
const { socket, isConnected } = useSocket();
const [isLoading, setIsLoading] = useState(false);
const { data: conversations } = useQuery(
const chatWindowRef = useRef(null);
const { data: conversations, refetch } = useQuery(
getConversations,
{
chatId: Number(id),
},
{ enabled: !!id, refetchInterval: 1000 }
{ enabled: !!id }
);

const googleRedirectLoginMsg: any = getQueryParam("msg");
Expand Down Expand Up @@ -55,6 +56,27 @@ export default function ConversationWrapper() {
]
);

useSocketListener("newConversationAddedToDB", reFetchConversations);

function reFetchConversations() {
refetch();
}

useEffect(() => {
scrollToBottom();
}, [conversations]);

const scrollToBottom = () => {
if (chatWindowRef.current) {
// @ts-ignore
chatWindowRef.current.scrollTo({
// @ts-ignore
top: chatWindowRef.current.scrollHeight,
behavior: "smooth",
});
}
};

async function addMessagesToConversation(
userQuery: string,
conv_id?: number,
Expand Down Expand Up @@ -137,7 +159,11 @@ export default function ConversationWrapper() {
<div className="relative h-full w-full flex-1 overflow-auto transition-width">
<div className="flex h-full flex-col">
<div className="flex-1 overflow-hidden">
<div className={`${chatContainerClass}`} style={{ height: "85%" }}>
<div
ref={chatWindowRef}
className={`${chatContainerClass}`}
style={{ height: "85%" }}
>
{conversations && (
<ConversationsList
conversations={conversations}
Expand Down
2 changes: 2 additions & 0 deletions src/server/webSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ async function updateConversationsInDb(
user: { connect: { id: socket.data.user.id } },
},
});

socket.emit("newConversationAddedToDB");
}

export const checkTeamStatusAndUpdateInDB = (io, context) => {
Expand Down
Loading