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

채팅 컴포넌트 버그 수정 #184

Merged
merged 4 commits into from
Aug 2, 2024
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
1 change: 1 addition & 0 deletions frontend/src/components/ChatList/ChatList.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const list = ({ theme }: { theme: Theme }) => css`
gap: 1rem;

height: 100%;
min-height: 100vh;
padding: 2rem;

background-color: ${theme.colorPalette.grey[100]};
Expand Down
24 changes: 24 additions & 0 deletions frontend/src/components/ChatList/ChatList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Chat, { ChatMessageProps } from '@_components/Chat/Chat';
import { useEffect, useRef, useState } from 'react';

import { list } from './ChatList.style';
import { useTheme } from '@emotion/react';
Expand All @@ -9,13 +10,36 @@ interface ChatListProps {

export default function ChatList(props: ChatListProps) {
const { chats } = props;
const [lastChats, setLastChats] = useState<ChatMessageProps[]>([]);
const messageEndRef = useRef<HTMLDivElement | null>(null);
const observer = useRef<IntersectionObserver | null>();
const theme = useTheme();

useEffect(() => {
observer.current = new IntersectionObserver(() => {
if (lastChats.length === chats.length) return;
if (messageEndRef) messageEndRef.current?.focus();
});
if (messageEndRef.current) observer.current.observe(messageEndRef.current);
}, [messageEndRef, lastChats, chats]);

useEffect(() => {
if (lastChats.length === chats.length) return;
const latestMyMessageIndex = chats.findLastIndex(
(chat) => chat.isMyMessage,
);
if (latestMyMessageIndex >= lastChats.length)
messageEndRef?.current?.focus();

setLastChats(chats);
}, [lastChats, chats]);

return (
<div css={list({ theme })}>
{chats.map((chat) => (
<Chat key={chat.message + chat.time} {...chat} />
))}
<div ref={messageEndRef}></div>
</div>
);
}
5 changes: 4 additions & 1 deletion frontend/src/components/ChattingFooter/ChattingFooter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeEvent, FormEvent, useState } from 'react';
import { ChangeEvent, FormEvent, useRef, useState } from 'react';
import {
footer,
menuButton,
Expand All @@ -22,6 +22,7 @@ export default function ChattingFooter(props: ChattingFooterProps) {
const [isMenuClicked, setIsMenuClicked] = useState(false);
const [message, setMessage] = useState('');
const theme = useTheme();
const input = useRef<HTMLInputElement | null>(null);

return (
<div css={footer({ theme })}>
Expand All @@ -39,6 +40,7 @@ export default function ChattingFooter(props: ChattingFooterProps) {
e.preventDefault();
onSubmit(message);
setMessage('');
input.current?.focus();
}}
css={messageForm({ theme })}
>
Expand All @@ -52,6 +54,7 @@ export default function ChattingFooter(props: ChattingFooterProps) {
setMessage(e.target.value)
}
required
ref={input}
/>
<button css={sendingButton} type="submit" disabled={message === ''}>
<SendButton disabled={message === ''} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ export const container = css`
align-items: center;
justify-content: space-between;

width: 100%;
height: 10rem;
padding: 0 2rem;
padding: 2rem;

border-radius: 25px;
box-shadow: 0 0 10px 0 #00000040;
Expand Down
Loading