-
Notifications
You must be signed in to change notification settings - Fork 25
Ux/thread view improvements #634
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
135 changes: 135 additions & 0 deletions
135
...s/layouts/components/thread-view/components/thread-event-input/use-im-input-visibility.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { RefObject, useEffect, useRef, useState } from "react"; | ||
|
|
||
| /** | ||
| * Distance (px) from the bottom of the scroll container within which | ||
| * the ThreadEventInput stays visible. Beyond this threshold, the input | ||
| * slides out to avoid cluttering the reading area. | ||
| */ | ||
| const NEAR_BOTTOM_THRESHOLD = 150; | ||
|
|
||
| /** | ||
| * Minimum downward scroll delta between two animation frames to | ||
| * be considered a "fast scroll". When the user scrolls down quickly, | ||
| * the ThreadEventInput is revealed even if the user hasn't reached the | ||
| * bottom — signaling intent to interact. | ||
| */ | ||
| const FAST_SCROLL_DOWN_THRESHOLD = 35; // px | ||
| const FAST_SCROLL_TIMEOUT = 2000; // ms | ||
|
|
||
| /** | ||
| * Minimum upward scroll distance (px) from the point where fast-scroll | ||
| * was triggered before dismissing the input. Prevents tiny scroll | ||
| * adjustments from hiding the input too eagerly. | ||
| */ | ||
| const SCROLL_UP_DISMISS_THRESHOLD = 100; // px | ||
|
|
||
| type UseIMInputVisibilityOptions = { | ||
| containerRef: RefObject<HTMLDivElement | null>; | ||
| threadId: string; | ||
| isEditing: boolean; | ||
| isMessageFormFocused: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Manages the show/hide logic for the ThreadEventInput based on scroll | ||
| * position and user interaction: | ||
| * - visible when near the bottom of the scroll container | ||
| * - visible during fast downward scrolling (signals intent to interact) | ||
| * - visible when the input itself is focused or in edit mode | ||
| * - hidden when the message reply form has focus | ||
| */ | ||
| export const useIMInputVisibility = ({ | ||
| containerRef, | ||
| threadId, | ||
| isEditing, | ||
| isMessageFormFocused, | ||
| }: UseIMInputVisibilityOptions) => { | ||
| const [isNearBottom, setIsNearBottom] = useState(false); | ||
| const [isFocused, setIsFocused] = useState(false); | ||
| const [isFastScrollingDown, setIsFastScrollingDown] = useState(false); | ||
| const rafRef = useRef<number | null>(null); | ||
| const lastScrollTopRef = useRef<number | null>(null); | ||
| const fastScrollTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); | ||
| const fastScrollOriginRef = useRef<number | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| const el = containerRef.current; | ||
| if (!el) return; | ||
|
|
||
| const handleScroll = () => { | ||
| if (rafRef.current !== null) return; | ||
| rafRef.current = requestAnimationFrame(() => { | ||
| rafRef.current = null; | ||
| const scrollTop = el.scrollTop; | ||
| const distanceFromBottom = | ||
| el.scrollHeight - scrollTop - el.clientHeight; | ||
| setIsNearBottom(distanceFromBottom <= NEAR_BOTTOM_THRESHOLD); | ||
|
|
||
| // Detect fast downward scroll | ||
| const prevScrollTop = lastScrollTopRef.current; | ||
| lastScrollTopRef.current = scrollTop; | ||
| if (prevScrollTop !== null) { | ||
| const delta = scrollTop - prevScrollTop; | ||
| if (delta >= FAST_SCROLL_DOWN_THRESHOLD) { | ||
| setIsFastScrollingDown(true); | ||
| fastScrollOriginRef.current = scrollTop; | ||
| if (fastScrollTimeoutRef.current !== null) { | ||
| clearTimeout(fastScrollTimeoutRef.current); | ||
| } | ||
| fastScrollTimeoutRef.current = setTimeout(() => { | ||
| fastScrollTimeoutRef.current = null; | ||
| fastScrollOriginRef.current = null; | ||
| setIsFastScrollingDown(false); | ||
| }, FAST_SCROLL_TIMEOUT); | ||
| } else if ( | ||
| delta < 0 && | ||
| fastScrollOriginRef.current !== null && | ||
| fastScrollOriginRef.current - scrollTop >= SCROLL_UP_DISMISS_THRESHOLD | ||
| ) { | ||
| if (fastScrollTimeoutRef.current !== null) { | ||
| clearTimeout(fastScrollTimeoutRef.current); | ||
| fastScrollTimeoutRef.current = null; | ||
| } | ||
| fastScrollOriginRef.current = null; | ||
| setIsFastScrollingDown(false); | ||
| } | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| el.addEventListener("scroll", handleScroll, { passive: true }); | ||
| return () => { | ||
| el.removeEventListener("scroll", handleScroll); | ||
| if (rafRef.current !== null) { | ||
| cancelAnimationFrame(rafRef.current); | ||
| rafRef.current = null; | ||
| } | ||
| if (fastScrollTimeoutRef.current !== null) { | ||
| clearTimeout(fastScrollTimeoutRef.current); | ||
| fastScrollTimeoutRef.current = null; | ||
| } | ||
| }; | ||
| }, [threadId]); | ||
|
|
||
| // Reset all scroll-related state on thread change | ||
| useEffect( | ||
| () => () => { | ||
| setIsNearBottom(false); | ||
| setIsFocused(false); | ||
| setIsFastScrollingDown(false); | ||
| lastScrollTopRef.current = null; | ||
| fastScrollOriginRef.current = null; | ||
| }, | ||
| [threadId], | ||
| ); | ||
|
|
||
| const isVisible = | ||
| isEditing || | ||
| isFocused || | ||
| (!isMessageFormFocused && (isNearBottom || isFastScrollingDown)); | ||
|
|
||
| return { | ||
| isVisible, | ||
| onFocusChange: setIsFocused, | ||
| }; | ||
| }; |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.