Replace state with ref for uriBuffer

This commit is contained in:
Raphael Wickihalder 2022-05-30 09:53:11 +02:00 committed by infinite-persistence
parent 99a36b6bd6
commit 9eda81db58
2 changed files with 17 additions and 47 deletions

View file

@ -1,7 +1,7 @@
// @flow
import { MAIN_CLASS } from 'constants/classnames';
import type { Node } from 'react';
import React, { useEffect, useState } from 'react';
import React, { useEffect } from 'react';
import classnames from 'classnames';
import ClaimPreview from 'component/claimPreview';
import Spinner from 'component/spinner';
@ -107,7 +107,7 @@ export default function ClaimList(props: Props) {
} = props;
const [currentSort, setCurrentSort] = usePersistedState(persistedStorageKey, SORT_NEW);
const [uriBuffer, setUriBuffer] = useState([]);
const uriBuffer = React.useRef([]);
// Resolve the index for injectedItem, if provided; else injectedIndex will be 'undefined'.
const listRef = React.useRef();
@ -207,25 +207,6 @@ export default function ClaimList(props: Props) {
/>
);
React.useEffect(() => {
refreshBuffer();
}, [tileUris, injectedItem, lastVisibleIndex, pageSize]);
function refreshBuffer() {
tileUris.forEach((uri, index) => {
if (uri) {
const inj = getInjectedItem(index);
if (inj) {
if (uriBuffer.indexOf(index) === -1) {
const newUriBuffer = uriBuffer;
newUriBuffer.push(index);
setUriBuffer(newUriBuffer);
}
}
}
});
}
const getInjectedItem = (index) => {
if (injectedItem && injectedItem.node) {
if (typeof injectedItem.node === 'function') {
@ -249,12 +230,16 @@ export default function ClaimList(props: Props) {
tileUris.map((uri, index) => {
if (uri) {
const inj = getInjectedItem(index);
if (inj) refreshBuffer();
if (inj) {
if (!uriBuffer.current.includes(index)) {
uriBuffer.current.push(index);
}
}
return (
<React.Fragment key={uri}>
{inj && inj}
{(index < tileUris.length - uriBuffer.length ||
(pageSize && index < pageSize - uriBuffer.length)) && (
{(index < tileUris.length - uriBuffer.current.length ||
(pageSize && index < pageSize - uriBuffer.current.length)) && (
<ClaimPreviewTile
uri={uri}
showHiddenByUser={showHiddenByUser}

View file

@ -1,6 +1,6 @@
// @flow
import type { Node } from 'react';
import React, { useState } from 'react';
import React, { useRef } from 'react';
import Button from 'component/button';
import ClaimPreviewTile from 'component/claimPreviewTile';
import I18nMessage from 'component/i18nMessage';
@ -102,7 +102,7 @@ function ClaimTilesDiscover(props: Props) {
const claimSearchUris = claimSearchResults || [];
const isUnfetchedClaimSearch = claimSearchResults === undefined;
const resolvedPinUris = useResolvePins({ pins, claimsById, doResolveClaimIds, doResolveUris });
const [uriBuffer, setUriBuffer] = useState([]);
const uriBuffer = useRef([]);
const timedOut = claimSearchResults === null;
const shouldPerformSearch = !fetchingClaimSearch && !timedOut && claimSearchUris.length === 0;
@ -175,25 +175,6 @@ function ClaimTilesDiscover(props: Props) {
}
}, [doClaimSearch, shouldPerformSearch, optionsStringified]);
React.useEffect(() => {
refreshBuffer();
}, [finalUris, injectedItem, lastVisibleIndex, pageSize]);
function refreshBuffer() {
finalUris.forEach((uri, index) => {
if (uri) {
const inj = getInjectedItem(index);
if (inj) {
if (uriBuffer.indexOf(index) === -1) {
const newUriBuffer = uriBuffer;
newUriBuffer.push(index);
setUriBuffer(newUriBuffer);
}
}
}
});
}
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
@ -226,11 +207,15 @@ function ClaimTilesDiscover(props: Props) {
? finalUris.map((uri, i) => {
if (uri) {
const inj = getInjectedItem(i);
if (inj) refreshBuffer();
if (inj) {
if (!uriBuffer.current.includes(i)) {
uriBuffer.current.push(i);
}
}
return (
<React.Fragment key={uri}>
{inj && inj}
{(i < finalUris.length - uriBuffer.length || i < pageSize - uriBuffer.length) && (
{(i < finalUris.length - uriBuffer.current.length || i < pageSize - uriBuffer.current.length) && (
<ClaimPreviewTile
showNoSourceClaims={hasNoSource || showNoSourceClaims}
uri={uri}