Skip to content

Commit

Permalink
Fix message scrolling and message order
Browse files Browse the repository at this point in the history
  • Loading branch information
Rikthepixel committed Jan 18, 2024
1 parent 6a176dd commit c9566f5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { Text, Group, Stack, Paper } from '@mantine/core';
import { useEffect, useRef } from 'react';

interface MessageBubbleProps {
username: string;
content: string;
sentAt: Date;
fromSelf: boolean;
latest: boolean;
}

export default function MessageBubble(props: MessageBubbleProps) {
const bubbleRef = useRef<HTMLElement>(null);
useEffect(() => {
if (!props.latest) return;
bubbleRef.current?.scrollIntoView({ behavior: "auto"})

}, [props.latest])

return (
<Paper
ref={bubbleRef}
bg={props.fromSelf ? 'red' : 'blue'}
maw="80%"
p="md"
Expand Down
16 changes: 3 additions & 13 deletions apps/frontend/src/pages/messages/[uid]/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Center,
Paper,
} from '@mantine/core';
import { useEffect, useRef } from 'react';
import { useEffect } from 'react';
import { MdChevronLeft } from 'react-icons/md';
import { Link } from 'wouter';
import { z } from 'zod';
Expand All @@ -27,7 +27,6 @@ export default function MessagePage() {
const params = useTypedParams(PARAMS_SCHEMA);
const { chat, connectToChat, disconnect } = useChats();
const { user: currentUser } = useAuth();
const previousMessagesLength = useRef(0);

useEffect(() => {
if (!params?.uid) return;
Expand All @@ -39,16 +38,6 @@ export default function MessagePage() {
.unwrapValue()?.[0]
?.users.find((_user) => _user.provider_id !== currentUser?.providerId);

useEffect(() => {
chat.map(([chat]) => {
window.scrollTo({
top: document.documentElement.scrollHeight,
behavior: previousMessagesLength.current === 0 ? 'instant' : 'smooth',
});
previousMessagesLength.current = chat.messages.length;
});
}, [chat]);

return (
<PageContainer display="flex">
<Stack
Expand Down Expand Up @@ -87,7 +76,7 @@ export default function MessagePage() {
</Paper>
</Center>
)}
{chat.messages.map((message) => (
{chat.messages.map((message, idx, arr) => (
<MessageBubble
key={message.uid}
username={message.user.username}
Expand All @@ -96,6 +85,7 @@ export default function MessagePage() {
fromSelf={
message.user.provider_id === currentUser?.providerId
}
latest={idx === arr.length - 1}
/>
))}
</Stack>
Expand Down
5 changes: 3 additions & 2 deletions apps/messages/src/repositories/chat/KnexChatRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default class KnexChatRepository implements ChatRepository {
'users.username as user_username',
'users.provider_id as user_provider_id',
)
.orderBy('messages.sent_at', 'asc')
.then<ChatWithUsersAndMessages['messages']>((msgs) =>
msgs.map((msg) => ({
...msg,
Expand Down Expand Up @@ -124,9 +125,9 @@ export default class KnexChatRepository implements ChatRepository {
if (!chat) return null;
return {
...chat,
uid: this.db.fn.binToUuid(chat.uid)
uid: this.db.fn.binToUuid(chat.uid),
};
})
});
}

create(chat: CreatableChat): Promise<void> {
Expand Down

0 comments on commit c9566f5

Please sign in to comment.