-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: improve useJumpToMessage (#31907)
- Loading branch information
Showing
3 changed files
with
49 additions
and
44 deletions.
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
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
81 changes: 44 additions & 37 deletions
81
apps/meteor/client/views/room/MessageList/hooks/useJumpToMessage.ts
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,51 +1,58 @@ | ||
import type { IMessage } from '@rocket.chat/core-typings'; | ||
import { useRouter } from '@rocket.chat/ui-contexts'; | ||
import type { RefObject } from 'react'; | ||
import { useLayoutEffect } from 'react'; | ||
import { useCallback } from 'react'; | ||
|
||
import { useMessageListJumpToMessageParam, useMessageListScroll } from '../../../../components/message/list/MessageListContext'; | ||
import { setHighlightMessage, clearHighlightMessage } from '../providers/messageHighlightSubscription'; | ||
|
||
// this is an arbitrary value so that there's a gap between the header and the message; | ||
const SCROLL_EXTRA_OFFSET = 60; | ||
|
||
export const useJumpToMessage = (messageId: IMessage['_id'], messageRef: RefObject<HTMLDivElement>): void => { | ||
export const useJumpToMessage = (messageId: IMessage['_id']) => { | ||
const jumpToMessageParam = useMessageListJumpToMessageParam(); | ||
const scroll = useMessageListScroll(); | ||
const router = useRouter(); | ||
|
||
useLayoutEffect(() => { | ||
if (jumpToMessageParam !== messageId || !messageRef.current || !scroll) { | ||
return; | ||
} | ||
|
||
setTimeout(() => { | ||
scroll((wrapper) => { | ||
if (!wrapper || !messageRef.current) { | ||
return; | ||
} | ||
const containerRect = wrapper.getBoundingClientRect(); | ||
const messageRect = messageRef.current.getBoundingClientRect(); | ||
|
||
const offset = messageRect.top - containerRect.top; | ||
const scrollPosition = wrapper.scrollTop; | ||
const newScrollPosition = scrollPosition + offset - SCROLL_EXTRA_OFFSET; | ||
|
||
return { top: newScrollPosition, behavior: 'smooth' }; | ||
}); | ||
|
||
const search = router.getSearchParameters(); | ||
delete search.msg; | ||
router.navigate( | ||
{ | ||
pathname: router.getLocationPathname(), | ||
search, | ||
}, | ||
{ replace: true }, | ||
); | ||
|
||
setHighlightMessage(messageId); | ||
setTimeout(clearHighlightMessage, 2000); | ||
}, 500); | ||
}, [messageId, jumpToMessageParam, messageRef, scroll, router]); | ||
const ref = useCallback( | ||
(node: HTMLElement | null) => { | ||
if (!node || !scroll) { | ||
return; | ||
} | ||
setTimeout(() => { | ||
scroll((wrapper) => { | ||
if (!wrapper || !node) { | ||
return; | ||
} | ||
const containerRect = wrapper.getBoundingClientRect(); | ||
const messageRect = node.getBoundingClientRect(); | ||
|
||
const offset = messageRect.top - containerRect.top; | ||
const scrollPosition = wrapper.scrollTop; | ||
const newScrollPosition = scrollPosition + offset - SCROLL_EXTRA_OFFSET; | ||
|
||
return { top: newScrollPosition, behavior: 'smooth' }; | ||
}); | ||
|
||
const { msg: _, ...search } = router.getSearchParameters(); | ||
|
||
router.navigate( | ||
{ | ||
pathname: router.getLocationPathname(), | ||
search, | ||
}, | ||
{ replace: true }, | ||
); | ||
|
||
setHighlightMessage(messageId); | ||
setTimeout(clearHighlightMessage, 2000); | ||
}, 500); | ||
}, | ||
[messageId, router, scroll], | ||
); | ||
|
||
if (jumpToMessageParam !== messageId) { | ||
return undefined; | ||
} | ||
|
||
return ref; | ||
}; |