Reset scroll position when changing searchTerm (#373)

This commit is contained in:
schroda
2023-06-17 16:29:55 +02:00
committed by GitHub
parent 938b5166d9
commit 3ad33b0a15

View File

@@ -174,6 +174,7 @@ export default function SourceMangas() {
const [dialogFiltersToApply, setDialogFiltersToApply] = useState<IPos[]>([]); const [dialogFiltersToApply, setDialogFiltersToApply] = useState<IPos[]>([]);
const [filtersToApply, setFiltersToApply] = useState<IPos[]>([]); const [filtersToApply, setFiltersToApply] = useState<IPos[]>([]);
const searchTerm = useDebounce(query, 1000); const searchTerm = useDebounce(query, 1000);
const [resetScrollPosition, setResetScrollPosition] = useState(false);
const [contentType, setContentType] = useState(currentLocationContentType); const [contentType, setContentType] = useState(currentLocationContentType);
const { const {
data: { items: mangas, hasNextPage } = { items: [], hasNextPage: false }, data: { items: mangas, hasNextPage } = { items: [], hasNextPage: false },
@@ -198,15 +199,27 @@ export default function SourceMangas() {
</> </>
) : undefined; ) : undefined;
const updateContentType = useCallback(
(newContentType: SourceContentType, updateLocationState: boolean = true) => {
if (updateLocationState) {
navigate('', { replace: true, state: { contentType: newContentType } });
}
setContentType(newContentType);
setResetScrollPosition(true);
},
[setContentType],
);
const isSearchTermAvailable = searchTerm && query?.length; const isSearchTermAvailable = searchTerm && query?.length;
const setSearchContentType = isSearchTermAvailable && contentType !== SourceContentType.SEARCH; const setSearchContentType = isSearchTermAvailable && contentType !== SourceContentType.SEARCH;
if (setSearchContentType) { if (setSearchContentType) {
setContentType(SourceContentType.SEARCH); updateContentType(SourceContentType.SEARCH, false);
} }
const closeSearch = !query?.length && contentType === SourceContentType.SEARCH; const closeSearch = !query?.length && contentType === SourceContentType.SEARCH;
if (closeSearch) { if (closeSearch) {
setContentType(currentLocationContentType); updateContentType(currentLocationContentType, false);
} }
let wasLoadMoreTriggered = false; let wasLoadMoreTriggered = false;
@@ -232,14 +245,6 @@ export default function SourceMangas() {
setTriggerDataRefresh(true); setTriggerDataRefresh(true);
}, [sourceId]); }, [sourceId]);
const updateContentType = useCallback(
(newContentType: SourceContentType) => {
navigate('', { replace: true, state: { contentType: newContentType } });
setContentType(newContentType);
},
[setContentType],
);
useEffect( useEffect(
() => () => { () => () => {
if (contentType !== SourceContentType.SEARCH) { if (contentType !== SourceContentType.SEARCH) {
@@ -285,8 +290,12 @@ export default function SourceMangas() {
}, [t, source]); }, [t, source]);
useEffect(() => { useEffect(() => {
if (!resetScrollPosition) {
return;
}
window.scrollTo(0, 0); window.scrollTo(0, 0);
}, [contentType]); }, [resetScrollPosition]);
return ( return (
<StyledGridWrapper hasContent={!!mangas.length}> <StyledGridWrapper hasContent={!!mangas.length}>