···6767 // Function to load older entries
6868 const loadOlderEntries = async () => {
6969 try {
7070- // Save current scroll position
7171- const scrollPosition = window.scrollY;
7070+ // Save reference to the "Load older flushes" button element to measure its position
7171+ const loadMoreButton = document.getElementById('load-more-button');
7272+ const buttonPosition = loadMoreButton?.getBoundingClientRect();
72737374 setLoading(true);
7475 setError(null);
···9798 const data = await response.json();
989999100 if (data.entries && data.entries.length > 0) {
101101+ // Get the current document height before adding new content
102102+ const oldDocumentHeight = document.body.scrollHeight;
103103+100104 // Append the new entries to our existing list
101101- setEntries([...entries, ...data.entries]);
105105+ setEntries(prevEntries => [...prevEntries, ...data.entries]);
102106103103- // Wait for DOM to update with new entries
104104- setTimeout(() => {
105105- // Restore scroll position after state update and render
106106- window.scrollTo({
107107- top: scrollPosition,
108108- behavior: 'instant' // Use instant to avoid additional animation
107107+ // After state update, maintain position relative to the Load More button
108108+ if (buttonPosition) {
109109+ // Use requestAnimationFrame to ensure DOM has updated
110110+ requestAnimationFrame(() => {
111111+ // Get the button's new position
112112+ const newButtonElement = document.getElementById('load-more-button');
113113+114114+ if (newButtonElement) {
115115+ // Calculate where to scroll to keep the button in the same viewport position
116116+ const newButtonPosition = newButtonElement.getBoundingClientRect();
117117+ const newScrollY = window.scrollY + (newButtonPosition.top - buttonPosition.top);
118118+119119+ // Scroll to the calculated position
120120+ window.scrollTo({
121121+ top: newScrollY,
122122+ behavior: 'instant' // Use instant to avoid animation
123123+ });
124124+ }
109125 });
110110- }, 0);
126126+ }
111127 }
112128 } catch (err: any) {
113129 console.error('Error fetching older entries:', err);
···182198183199 <button
184200 className={styles.loadMoreButton}
201201+ id="load-more-button"
185202 onClick={(e) => {
186203 e.preventDefault(); // Prevent default action
187204 loadOlderEntries();