-
Notifications
You must be signed in to change notification settings - Fork 22
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 auto scroll pause in chat #26
Open
smarttcoder
wants to merge
4
commits into
marwanhawari:main
Choose a base branch
from
smarttcoder:fix_auto_scroll_pause
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { io, Socket } from "socket.io-client"; | ||
import { useEffect, useRef, useState } from "react"; | ||
import { CSSProperties, useEffect, useRef, useState } from "react"; | ||
import Question from "./Question"; | ||
import { QuestionInterface, SubmissionStatus } from "../types/Question"; | ||
import CopyIcon from "../icons/CopyIcon"; | ||
|
@@ -49,6 +49,25 @@ export default function Room({ | |
duration: number | undefined | null; | ||
}) { | ||
const isLoadingGlobal = useIsMutating(); | ||
const [autoScroll, setAutoScroll] = useState(true); | ||
const [showNotification, setShowNotification] = useState(false); | ||
const notificationStyles: CSSProperties = { | ||
position: 'absolute', | ||
bottom: '60px', | ||
left: '50%', | ||
transform: 'translateX(-50%)', | ||
backgroundColor: '#444', | ||
color: 'white', | ||
padding: '10px', | ||
borderRadius: '5px', | ||
opacity: showNotification ? 1 : 0, | ||
transition: 'opacity 0.5s', | ||
pointerEvents: 'none', | ||
fontSize: '12px', | ||
whiteSpace: 'nowrap' | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should use tailwind css styles. |
||
|
||
|
||
let inputRef = useRef<HTMLInputElement>(null); | ||
let messagesRef = useRef<HTMLUListElement>(null); | ||
let [messages, setMessages] = useState<MessageInterface[]>([ | ||
|
@@ -245,13 +264,24 @@ export default function Room({ | |
} | ||
|
||
let latestMessage = messagesRef.current.lastElementChild; | ||
latestMessage?.scrollIntoView({ | ||
behavior: "auto", | ||
}); | ||
if(autoScroll) { | ||
latestMessage?.scrollIntoView({ | ||
behavior: "auto", | ||
}); | ||
} | ||
} | ||
|
||
autoScrollToLatestMessage(); | ||
}, [messages]); | ||
}, [messages, autoScroll]); | ||
|
||
// Add scroll event handler with the correct type for 'e' | ||
const handleScroll = (e: React.UIEvent<HTMLDivElement>) => { | ||
const { scrollTop, scrollHeight, clientHeight } = e.currentTarget; | ||
const threshold = 50; | ||
const atBottom = scrollHeight - scrollTop <= clientHeight + threshold; | ||
setAutoScroll(atBottom); | ||
setShowNotification(!atBottom); | ||
}; | ||
|
||
return ( | ||
<div className="flex h-screen flex-col gap-y-2 border-x-8 border-t-8 border-lc-border-light bg-lc-bg-light px-2 text-sm text-lc-text-light dark:border-lc-border dark:bg-lc-bg dark:text-white"> | ||
|
@@ -310,12 +340,16 @@ export default function Room({ | |
<div | ||
id="leetrooms-chat" | ||
className="mx-2 grow overflow-auto border border-transparent px-3 py-[10px]" | ||
onScroll={handleScroll} | ||
> | ||
<ul ref={messagesRef} className="flex flex-col gap-y-1.5"> | ||
{messages.map((message, index) => ( | ||
<Message key={index} message={message} /> | ||
))} | ||
</ul> | ||
<div style={notificationStyles}> | ||
Chat paused. Scroll down to resume. | ||
</div> | ||
</div> | ||
|
||
<div className="mx-2 mb-2.5 flex flex-row items-center justify-between gap-x-2 rounded-lg border border-transparent bg-lc-fg-light py-[5px] pl-3 pr-2 focus-within:border-blue-500 hover:border-blue-500 dark:bg-lc-fg"> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to use 2 state variables? Why can't we also just use
autoScroll
for determining when to show the notification?