fix: prevent saving tiny scroll positions in restoration hook (#13554)

fixes 1-2px scroll restoration issue

- browser reports 1-2px instead of exact 0 when at top
- old logic kept saving these tiny values 
- now clears storage when <= 3px (essentially at top)
- prevents weird micro-scroll when returning to pages


re: [greptile infinite polling
concern](https://github.com/twentyhq/twenty/pull/13363#discussion_r2247232974)
- not valid in practice:
- restoration only runs if position was previously saved
- no scroll = no save = no restore = no polling
- tested across all page types, zero infinite loops

tested: works perfectly, no side effects
This commit is contained in:
nitin
2025-08-01 20:21:52 +05:30
committed by GitHub
parent c879136d29
commit 75214c3e61
2 changed files with 9 additions and 2 deletions
@@ -0,0 +1 @@
export const SCROLL_RESTORATION_TOP_THRESHOLD_PX = 3;
@@ -1,3 +1,4 @@
import { SCROLL_RESTORATION_TOP_THRESHOLD_PX } from '@/ui/utilities/scroll/constants/ScrollRestorationTopThreshold';
import { scrollWrapperScrollTopComponentState } from '@/ui/utilities/scroll/states/scrollWrapperScrollTopComponentState';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
import { useCallback, useEffect, useState } from 'react';
@@ -43,9 +44,14 @@ export const useScrollRestoration = (componentInstanceId: string) => {
);
useEffect(() => {
if (scrollTop > 0 && !isRestoring) {
sessionStorage.setItem(storageKey, scrollTop.toString());
if (isRestoring) return;
if (scrollTop <= SCROLL_RESTORATION_TOP_THRESHOLD_PX) {
sessionStorage.removeItem(storageKey);
return;
}
sessionStorage.setItem(storageKey, scrollTop.toString());
}, [scrollTop, storageKey, isRestoring]);
useEffect(() => {