-
Notifications
You must be signed in to change notification settings - Fork 3
버그 수정 및 채팅 자동 스크롤 및 새 메시지 알림 기능 구현을 통한 UX 개선 #235
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
Changes from 3 commits
14ab9fd
7a516b2
a49e12c
c1efd62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { useEffect, useRef, useState } from 'react'; | ||
| import { WebSocketResponsePayload } from './websocket.type'; | ||
| import { useToast } from '@workspace/ui/hooks/Toast/use-toast'; | ||
|
|
||
| export const useChatAutoScroll = (messages: WebSocketResponsePayload[]) => { | ||
| const bottomRef = useRef<HTMLDivElement | null>(null); | ||
| const containerRef = useRef<HTMLDivElement | null>(null); | ||
| const [prevMessageCount, setPrevMessageCount] = useState(0); | ||
| const [isUserScrollingUp, setIsUserScrollingUp] = useState(false); | ||
| const [newMessageCount, setNewMessageCount] = useState(0); | ||
| const { toast, dismiss } = useToast(); | ||
|
|
||
| useEffect(() => { | ||
| if (!containerRef.current) return; | ||
| const container = containerRef.current; | ||
|
|
||
| const handleScroll = () => { | ||
| const { scrollTop, scrollHeight, clientHeight } = container; | ||
| const isAtBottom = scrollHeight - scrollTop <= clientHeight + 100; | ||
|
|
||
| setIsUserScrollingUp(!isAtBottom); | ||
| if (isAtBottom && newMessageCount > 0) { | ||
| setNewMessageCount(0); | ||
| dismiss('new-message'); | ||
| } | ||
| }; | ||
|
|
||
| container.addEventListener('scroll', handleScroll); | ||
|
|
||
| if (messages.length > prevMessageCount) { | ||
| if (!isUserScrollingUp) { | ||
| setTimeout(() => { | ||
| bottomRef.current?.scrollIntoView({ | ||
| behavior: 'smooth', | ||
| block: 'nearest', | ||
| }); | ||
| }, 0); | ||
| } else { | ||
| setNewMessageCount((prev) => prev + 1); | ||
| toast({ | ||
| title: '새 메시지가 있습니다', | ||
| description: `${newMessageCount + 1}개의 새 메시지가 도착했습니다.`, | ||
| }); | ||
| } | ||
| setPrevMessageCount(messages.length); | ||
| } | ||
|
|
||
| return () => container.removeEventListener('scroll', handleScroll); | ||
| }, [ | ||
| messages, | ||
| isUserScrollingUp, | ||
| newMessageCount, | ||
| toast, | ||
| dismiss, | ||
| prevMessageCount, | ||
KimKyuHoi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ]); | ||
|
||
|
|
||
| return { bottomRef, containerRef }; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,12 @@ | ||
| export { type ToastProps, type ToastActionElement, ToastProvider, ToastViewport, Toast, ToastTitle, ToastDescription, ToastClose, ToastAction } from './toast'; | ||
| export { | ||
| type ToastProps, | ||
| type ToastActionElement, | ||
| ToastProvider, | ||
| ToastViewport, | ||
| Toast, | ||
| ToastTitle, | ||
| ToastDescription, | ||
| ToastClose, | ||
| ToastAction, | ||
| } from './toast'; | ||
| export { Toaster } from './toaster'; |
Uh oh!
There was an error while loading. Please reload this page.