mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
retain virtuoso scroll position
This commit is contained in:
@@ -41,6 +41,7 @@ import useAnonModeStore from '../../hooks/stores/useAnonModeStore';
|
||||
import useGeneralStore from '../../hooks/stores/useGeneralStore';
|
||||
import packageJson from '../../../package.json'
|
||||
const {version} = packageJson
|
||||
let lastVirtuosoStates = {};
|
||||
|
||||
|
||||
const All = () => {
|
||||
@@ -85,6 +86,7 @@ const All = () => {
|
||||
const quoteRefsMobile = useRef({});
|
||||
const postOnHoverRef = useRef(null);
|
||||
const selectedThreadCidRef = useRef(null);
|
||||
const virtuosoRef = useRef();
|
||||
|
||||
const [isReplyOpen, setIsReplyOpen] = useState(false);
|
||||
const [isEditModalOpen, setIsEditModalOpen] = useState(false);
|
||||
@@ -484,6 +486,22 @@ const All = () => {
|
||||
};
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
const setLastVirtuosoState = () => {
|
||||
virtuosoRef.current?.getState((snapshot) => {
|
||||
if (snapshot?.ranges?.length) {
|
||||
lastVirtuosoStates['all'] = snapshot;
|
||||
}
|
||||
});
|
||||
};
|
||||
window.addEventListener('scroll', setLastVirtuosoState);
|
||||
|
||||
return () => window.removeEventListener('scroll', setLastVirtuosoState);
|
||||
}, []);
|
||||
|
||||
const lastVirtuosoState = lastVirtuosoStates['all'];
|
||||
|
||||
|
||||
return (
|
||||
<>
|
||||
<Helmet>
|
||||
@@ -620,7 +638,7 @@ const All = () => {
|
||||
<div className="board">
|
||||
{feed ? (
|
||||
<Virtuoso
|
||||
increaseViewportBy={2000}
|
||||
increaseViewportBy={{bottom: 600, top: 600}}
|
||||
data={selectedFeed}
|
||||
itemContent={(index, thread) => {
|
||||
const { displayedReplies, omittedCount } = filteredRepliesByThread[thread.cid] || {};
|
||||
@@ -2202,6 +2220,9 @@ const All = () => {
|
||||
</Fragment>
|
||||
);
|
||||
}}
|
||||
ref={virtuosoRef}
|
||||
restoreStateFrom={lastVirtuosoState}
|
||||
initialScrollTop={lastVirtuosoState?.scrollTop}
|
||||
endReached={tryLoadMore}
|
||||
useWindowScroll={true}
|
||||
components={{ Footer: hasMore && feed.length > 0 ? () => <PostLoader /> : null }}
|
||||
|
||||
@@ -43,6 +43,7 @@ import useAnonModeStore from '../../hooks/stores/useAnonModeStore';
|
||||
import useGeneralStore from '../../hooks/stores/useGeneralStore';
|
||||
import packageJson from '../../../package.json'
|
||||
const {version} = packageJson
|
||||
let lastVirtuosoStates = {};
|
||||
|
||||
|
||||
const Board = () => {
|
||||
@@ -103,6 +104,7 @@ const Board = () => {
|
||||
const quoteRefsMobile = useRef({});
|
||||
const postOnHoverRef = useRef(null);
|
||||
const selectedThreadCidRef = useRef(null);
|
||||
const virtuosoRef = useRef();
|
||||
|
||||
const { feed, hasMore, loadMore } = useFeed({subplebbitAddresses: [`${selectedAddress}`], sortType: 'active'});
|
||||
const subplebbit = useSubplebbit({subplebbitAddress: selectedAddress});
|
||||
@@ -648,7 +650,21 @@ const Board = () => {
|
||||
};
|
||||
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
const setLastVirtuosoState = () => {
|
||||
virtuosoRef.current?.getState((snapshot) => {
|
||||
if (snapshot?.ranges?.length) {
|
||||
lastVirtuosoStates[selectedAddress] = snapshot;
|
||||
}
|
||||
});
|
||||
};
|
||||
window.addEventListener('scroll', setLastVirtuosoState);
|
||||
|
||||
return () => window.removeEventListener('scroll', setLastVirtuosoState);
|
||||
}, [selectedAddress]);
|
||||
|
||||
const lastVirtuosoState = lastVirtuosoStates[selectedAddress];
|
||||
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -1267,7 +1283,7 @@ const Board = () => {
|
||||
</>
|
||||
: null}
|
||||
<Virtuoso
|
||||
increaseViewportBy={2000}
|
||||
increaseViewportBy={{bottom: 600, top: 600}}
|
||||
data={selectedFeed}
|
||||
itemContent={(index, thread) => {
|
||||
const { displayedReplies, omittedCount } = filteredRepliesByThread[thread.cid] || {};
|
||||
@@ -2825,6 +2841,9 @@ const Board = () => {
|
||||
</Fragment>
|
||||
);
|
||||
}}
|
||||
ref={virtuosoRef}
|
||||
restoreStateFrom={lastVirtuosoState}
|
||||
initialScrollTop={lastVirtuosoState?.scrollTop}
|
||||
endReached={tryLoadMore}
|
||||
useWindowScroll={true}
|
||||
components={{ Footer: hasMore && feed.length > 0 ? () => <PostLoader /> : null }}
|
||||
|
||||
@@ -41,6 +41,7 @@ import useSuccess from '../../hooks/useSuccess';
|
||||
import useAnonModeStore from '../../hooks/stores/useAnonModeStore';
|
||||
import packageJson from '../../../package.json'
|
||||
const {version} = packageJson
|
||||
let lastVirtuosoStates = {};
|
||||
|
||||
|
||||
const Subscriptions = () => {
|
||||
@@ -85,6 +86,7 @@ const Subscriptions = () => {
|
||||
const quoteRefsMobile = useRef({});
|
||||
const postOnHoverRef = useRef(null);
|
||||
const selectedThreadCidRef = useRef(null);
|
||||
const virtuosoRef = useRef();
|
||||
|
||||
const [isReplyOpen, setIsReplyOpen] = useState(false);
|
||||
const [isEditModalOpen, setIsEditModalOpen] = useState(false);
|
||||
@@ -487,6 +489,22 @@ const Subscriptions = () => {
|
||||
};
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
const setLastVirtuosoState = () => {
|
||||
virtuosoRef.current?.getState((snapshot) => {
|
||||
if (snapshot?.ranges?.length) {
|
||||
lastVirtuosoStates['subscriptions'] = snapshot;
|
||||
}
|
||||
});
|
||||
};
|
||||
window.addEventListener('scroll', setLastVirtuosoState);
|
||||
|
||||
return () => window.removeEventListener('scroll', setLastVirtuosoState);
|
||||
}, []);
|
||||
|
||||
const lastVirtuosoState = lastVirtuosoStates['subscriptions'];
|
||||
|
||||
|
||||
return (
|
||||
<>
|
||||
<Helmet>
|
||||
@@ -631,7 +649,7 @@ const Subscriptions = () => {
|
||||
null
|
||||
) : (
|
||||
<Virtuoso
|
||||
increaseViewportBy={2000}
|
||||
increaseViewportBy={{bottom: 600, top: 600}}
|
||||
data={selectedFeed}
|
||||
itemContent={(index, thread) => {
|
||||
const { displayedReplies, omittedCount } = filteredRepliesByThread[thread.cid] || {};
|
||||
@@ -2213,6 +2231,9 @@ const Subscriptions = () => {
|
||||
</Fragment>
|
||||
);
|
||||
}}
|
||||
ref={virtuosoRef}
|
||||
restoreStateFrom={lastVirtuosoState}
|
||||
initialScrollTop={lastVirtuosoState?.scrollTop}
|
||||
endReached={tryLoadMore}
|
||||
useWindowScroll={true}
|
||||
components={{ Footer: hasMore && feed.length > 0 ? () => <PostLoader /> : null }}
|
||||
|
||||
Reference in New Issue
Block a user