Skip to content

Commit

Permalink
fix scrolling via MutationObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
jjabari-op committed Dec 13, 2024
1 parent 71fc03d commit 5f3b963
Showing 1 changed file with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,42 @@ export default class IndexController extends Controller {
this.tryScroll(activityId, 0, maxAttempts);
}

private scrollToBottom() {
private tryScrollToBottom(attempts:number = 0, maxAttempts:number = 20) {
const scrollableContainer = this.getScrollableContainer();
if (scrollableContainer) {
scrollableContainer.scrollTop = scrollableContainer.scrollHeight;

if (!scrollableContainer) {
if (attempts < maxAttempts) {
setTimeout(() => this.tryScrollToBottom(attempts + 1, maxAttempts), 1000);
}
return;
}

scrollableContainer.scrollTop = 0;

let timeoutId:ReturnType<typeof setTimeout>;

const observer = new MutationObserver(() => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
clearTimeout(timeoutId);

timeoutId = setTimeout(() => {
observer.disconnect();
scrollableContainer.scrollTo({
top: scrollableContainer.scrollHeight,
behavior: 'smooth',
});
}, 100);
});

observer.observe(scrollableContainer, {
childList: true,
subtree: true,
attributes: true,
});
}

private scrollToBottom() {
this.tryScrollToBottom();
}

setFilterToOnlyComments() { this.filterValue = 'only_comments'; }
Expand Down

0 comments on commit 5f3b963

Please sign in to comment.