-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(webapp): pause deployment log auto-scroll on scroll-up #4776
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
+132
−7
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
58b4ecc
feat(webapp): pause deployment log auto-scroll when scrolled up
myftija 052b40e
chore(webapp): add server-changes note for deployment log follow scro…
myftija 9078452
fix(webapp): keep deployment log follow state in sync when the log ca…
myftija e4822ad
fix(webapp): scroll deployment logs to top instantly so follow mode s…
myftija 4f97cb6
fix(webapp): pause deployment log follow on any upward scroll intent
myftija fb8f98c
fix(webapp): treat a one pixel upward log scroll as leaving the bottom
myftija d71e5a4
fix(webapp): ignore the scroll event echoed by pinning deployment log…
myftija 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| area: webapp | ||
| type: feature | ||
| --- | ||
|
|
||
| Deployment logs no longer jump to the bottom while you are reading earlier output. Scroll up to pause auto-scroll, and scroll back down or use the new scroll-to-bottom button in the log header to resume following. |
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,100 @@ | ||
| import { useEffect, useLayoutEffect, useRef, useState, type RefObject } from "react"; | ||
|
|
||
| const AT_BOTTOM_TOLERANCE_PX = 4; | ||
|
|
||
| type FollowState = { | ||
| follow: boolean; | ||
| pinnedScrollTop: number | null; | ||
| lastScrollTop: number; | ||
| lastClientHeight: number; | ||
| }; | ||
|
|
||
| export function useFollowScroll(containerRef: RefObject<HTMLElement | null>, content: unknown) { | ||
| const [isAtBottom, setIsAtBottom] = useState(true); | ||
| const stateRef = useRef<FollowState>({ | ||
| follow: true, | ||
| pinnedScrollTop: null, | ||
| lastScrollTop: 0, | ||
| lastClientHeight: 0, | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| const container = containerRef.current; | ||
| if (!container) return; | ||
|
|
||
| const state = stateRef.current; | ||
| state.lastScrollTop = container.scrollTop; | ||
| state.lastClientHeight = container.clientHeight; | ||
|
|
||
| const onScroll = () => { | ||
| const { scrollTop, scrollHeight, clientHeight } = container; | ||
| const isEcho = scrollTop === state.pinnedScrollTop; | ||
| const movedUp = scrollTop < state.lastScrollTop && clientHeight <= state.lastClientHeight; | ||
| state.pinnedScrollTop = null; | ||
| state.lastScrollTop = scrollTop; | ||
| state.lastClientHeight = clientHeight; | ||
| if (isEcho) return; | ||
|
|
||
| state.follow = !movedUp && scrollHeight - scrollTop - clientHeight <= AT_BOTTOM_TOLERANCE_PX; | ||
| setIsAtBottom(state.follow); | ||
| }; | ||
|
|
||
| const onWheel = (event: WheelEvent) => { | ||
| if (event.deltaY >= 0 || !canScroll(container)) return; | ||
| state.follow = false; | ||
| setIsAtBottom(false); | ||
| }; | ||
|
|
||
| container.addEventListener("scroll", onScroll, { passive: true }); | ||
| container.addEventListener("wheel", onWheel, { passive: true }); | ||
| return () => { | ||
| container.removeEventListener("scroll", onScroll); | ||
| container.removeEventListener("wheel", onWheel); | ||
| }; | ||
| }, [containerRef]); | ||
|
|
||
| useLayoutEffect(() => { | ||
| const container = containerRef.current; | ||
| if (container && stateRef.current.follow) pinToBottom(container, stateRef.current); | ||
| }, [containerRef, isAtBottom, content]); | ||
|
|
||
| useEffect(() => { | ||
| const container = containerRef.current; | ||
| if (!container) return; | ||
|
|
||
| const observer = new ResizeObserver(() => { | ||
| if (stateRef.current.follow) pinToBottom(container, stateRef.current); | ||
| }); | ||
| observer.observe(container); | ||
| return () => observer.disconnect(); | ||
| }, [containerRef]); | ||
|
|
||
| const scrollToBottom = () => { | ||
| const container = containerRef.current; | ||
| if (!container) return; | ||
| stateRef.current.follow = true; | ||
| setIsAtBottom(true); | ||
| pinToBottom(container, stateRef.current); | ||
| }; | ||
|
|
||
| const scrollToTop = () => { | ||
| const container = containerRef.current; | ||
| if (!container || !canScroll(container)) return; | ||
| stateRef.current.follow = false; | ||
| setIsAtBottom(false); | ||
| container.scrollTop = 0; | ||
| }; | ||
|
|
||
| return { isAtBottom, scrollToBottom, scrollToTop }; | ||
| } | ||
|
|
||
| function pinToBottom(container: HTMLElement, state: FollowState) { | ||
| container.scrollTop = container.scrollHeight; | ||
| state.pinnedScrollTop = container.scrollTop; | ||
| state.lastScrollTop = container.scrollTop; | ||
| state.lastClientHeight = container.clientHeight; | ||
| } | ||
|
|
||
| function canScroll(container: HTMLElement) { | ||
| return container.scrollHeight - container.clientHeight > AT_BOTTOM_TOLERANCE_PX; | ||
| } | ||
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
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.