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 useGeneralStore from '../../hooks/stores/useGeneralStore';
|
||||||
import packageJson from '../../../package.json'
|
import packageJson from '../../../package.json'
|
||||||
const {version} = packageJson
|
const {version} = packageJson
|
||||||
|
let lastVirtuosoStates = {};
|
||||||
|
|
||||||
|
|
||||||
const All = () => {
|
const All = () => {
|
||||||
@@ -85,6 +86,7 @@ const All = () => {
|
|||||||
const quoteRefsMobile = useRef({});
|
const quoteRefsMobile = useRef({});
|
||||||
const postOnHoverRef = useRef(null);
|
const postOnHoverRef = useRef(null);
|
||||||
const selectedThreadCidRef = useRef(null);
|
const selectedThreadCidRef = useRef(null);
|
||||||
|
const virtuosoRef = useRef();
|
||||||
|
|
||||||
const [isReplyOpen, setIsReplyOpen] = useState(false);
|
const [isReplyOpen, setIsReplyOpen] = useState(false);
|
||||||
const [isEditModalOpen, setIsEditModalOpen] = 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 (
|
return (
|
||||||
<>
|
<>
|
||||||
<Helmet>
|
<Helmet>
|
||||||
@@ -620,7 +638,7 @@ const All = () => {
|
|||||||
<div className="board">
|
<div className="board">
|
||||||
{feed ? (
|
{feed ? (
|
||||||
<Virtuoso
|
<Virtuoso
|
||||||
increaseViewportBy={2000}
|
increaseViewportBy={{bottom: 600, top: 600}}
|
||||||
data={selectedFeed}
|
data={selectedFeed}
|
||||||
itemContent={(index, thread) => {
|
itemContent={(index, thread) => {
|
||||||
const { displayedReplies, omittedCount } = filteredRepliesByThread[thread.cid] || {};
|
const { displayedReplies, omittedCount } = filteredRepliesByThread[thread.cid] || {};
|
||||||
@@ -2202,6 +2220,9 @@ const All = () => {
|
|||||||
</Fragment>
|
</Fragment>
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
|
ref={virtuosoRef}
|
||||||
|
restoreStateFrom={lastVirtuosoState}
|
||||||
|
initialScrollTop={lastVirtuosoState?.scrollTop}
|
||||||
endReached={tryLoadMore}
|
endReached={tryLoadMore}
|
||||||
useWindowScroll={true}
|
useWindowScroll={true}
|
||||||
components={{ Footer: hasMore && feed.length > 0 ? () => <PostLoader /> : null }}
|
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 useGeneralStore from '../../hooks/stores/useGeneralStore';
|
||||||
import packageJson from '../../../package.json'
|
import packageJson from '../../../package.json'
|
||||||
const {version} = packageJson
|
const {version} = packageJson
|
||||||
|
let lastVirtuosoStates = {};
|
||||||
|
|
||||||
|
|
||||||
const Board = () => {
|
const Board = () => {
|
||||||
@@ -103,6 +104,7 @@ const Board = () => {
|
|||||||
const quoteRefsMobile = useRef({});
|
const quoteRefsMobile = useRef({});
|
||||||
const postOnHoverRef = useRef(null);
|
const postOnHoverRef = useRef(null);
|
||||||
const selectedThreadCidRef = useRef(null);
|
const selectedThreadCidRef = useRef(null);
|
||||||
|
const virtuosoRef = useRef();
|
||||||
|
|
||||||
const { feed, hasMore, loadMore } = useFeed({subplebbitAddresses: [`${selectedAddress}`], sortType: 'active'});
|
const { feed, hasMore, loadMore } = useFeed({subplebbitAddresses: [`${selectedAddress}`], sortType: 'active'});
|
||||||
const subplebbit = useSubplebbit({subplebbitAddress: selectedAddress});
|
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 (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -1267,7 +1283,7 @@ const Board = () => {
|
|||||||
</>
|
</>
|
||||||
: null}
|
: null}
|
||||||
<Virtuoso
|
<Virtuoso
|
||||||
increaseViewportBy={2000}
|
increaseViewportBy={{bottom: 600, top: 600}}
|
||||||
data={selectedFeed}
|
data={selectedFeed}
|
||||||
itemContent={(index, thread) => {
|
itemContent={(index, thread) => {
|
||||||
const { displayedReplies, omittedCount } = filteredRepliesByThread[thread.cid] || {};
|
const { displayedReplies, omittedCount } = filteredRepliesByThread[thread.cid] || {};
|
||||||
@@ -2825,6 +2841,9 @@ const Board = () => {
|
|||||||
</Fragment>
|
</Fragment>
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
|
ref={virtuosoRef}
|
||||||
|
restoreStateFrom={lastVirtuosoState}
|
||||||
|
initialScrollTop={lastVirtuosoState?.scrollTop}
|
||||||
endReached={tryLoadMore}
|
endReached={tryLoadMore}
|
||||||
useWindowScroll={true}
|
useWindowScroll={true}
|
||||||
components={{ Footer: hasMore && feed.length > 0 ? () => <PostLoader /> : null }}
|
components={{ Footer: hasMore && feed.length > 0 ? () => <PostLoader /> : null }}
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ import useSuccess from '../../hooks/useSuccess';
|
|||||||
import useAnonModeStore from '../../hooks/stores/useAnonModeStore';
|
import useAnonModeStore from '../../hooks/stores/useAnonModeStore';
|
||||||
import packageJson from '../../../package.json'
|
import packageJson from '../../../package.json'
|
||||||
const {version} = packageJson
|
const {version} = packageJson
|
||||||
|
let lastVirtuosoStates = {};
|
||||||
|
|
||||||
|
|
||||||
const Subscriptions = () => {
|
const Subscriptions = () => {
|
||||||
@@ -85,6 +86,7 @@ const Subscriptions = () => {
|
|||||||
const quoteRefsMobile = useRef({});
|
const quoteRefsMobile = useRef({});
|
||||||
const postOnHoverRef = useRef(null);
|
const postOnHoverRef = useRef(null);
|
||||||
const selectedThreadCidRef = useRef(null);
|
const selectedThreadCidRef = useRef(null);
|
||||||
|
const virtuosoRef = useRef();
|
||||||
|
|
||||||
const [isReplyOpen, setIsReplyOpen] = useState(false);
|
const [isReplyOpen, setIsReplyOpen] = useState(false);
|
||||||
const [isEditModalOpen, setIsEditModalOpen] = 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 (
|
return (
|
||||||
<>
|
<>
|
||||||
<Helmet>
|
<Helmet>
|
||||||
@@ -631,7 +649,7 @@ const Subscriptions = () => {
|
|||||||
null
|
null
|
||||||
) : (
|
) : (
|
||||||
<Virtuoso
|
<Virtuoso
|
||||||
increaseViewportBy={2000}
|
increaseViewportBy={{bottom: 600, top: 600}}
|
||||||
data={selectedFeed}
|
data={selectedFeed}
|
||||||
itemContent={(index, thread) => {
|
itemContent={(index, thread) => {
|
||||||
const { displayedReplies, omittedCount } = filteredRepliesByThread[thread.cid] || {};
|
const { displayedReplies, omittedCount } = filteredRepliesByThread[thread.cid] || {};
|
||||||
@@ -2213,6 +2231,9 @@ const Subscriptions = () => {
|
|||||||
</Fragment>
|
</Fragment>
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
|
ref={virtuosoRef}
|
||||||
|
restoreStateFrom={lastVirtuosoState}
|
||||||
|
initialScrollTop={lastVirtuosoState?.scrollTop}
|
||||||
endReached={tryLoadMore}
|
endReached={tryLoadMore}
|
||||||
useWindowScroll={true}
|
useWindowScroll={true}
|
||||||
components={{ Footer: hasMore && feed.length > 0 ? () => <PostLoader /> : null }}
|
components={{ Footer: hasMore && feed.length > 0 ? () => <PostLoader /> : null }}
|
||||||
|
|||||||
Reference in New Issue
Block a user