Skip to content

Commit

Permalink
Wrap pagination handler with a useCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
afgomez authored and Alejandro Fernández Gómez committed Nov 10, 2020
1 parent 7e5c892 commit 378803a
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions x-pack/plugins/infra/public/components/log_stream/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { useMemo } from 'react';
import React, { useMemo, useCallback } from 'react';
import { noop } from 'lodash';
import { useMount } from 'react-use';
import { euiStyled } from '../../../../observability/public';
Expand Down Expand Up @@ -97,13 +97,33 @@ Read more at https://github.com/elastic/kibana/blob/master/src/plugins/kibana_re
[entries]
);

const parsedHeight = typeof height === 'number' ? `${height}px` : height;

// Component lifetime
useMount(() => {
loadSourceConfiguration();
fetchEntries();
});

const parsedHeight = typeof height === 'number' ? `${height}px` : height;
// Pagination handler
const handlePagination = useCallback(
({ fromScroll, pagesBeforeStart, pagesAfterEnd }) => {
if (!fromScroll) {
return;
}

if (isLoadingMore) {
return;
}

if (pagesBeforeStart < PAGE_THRESHOLD) {
fetchPreviousEntries();
} else if (pagesAfterEnd < PAGE_THRESHOLD) {
fetchNextEntries();
}
},
[isLoadingMore, fetchPreviousEntries, fetchNextEntries]
);

return (
<LogStreamContent height={parsedHeight}>
Expand All @@ -120,21 +140,7 @@ Read more at https://github.com/elastic/kibana/blob/master/src/plugins/kibana_re
isStreaming={false}
lastLoadedTime={null}
jumpToTarget={noop}
reportVisibleInterval={({ fromScroll, pagesBeforeStart, pagesAfterEnd }) => {
if (!fromScroll) {
return;
}

if (isLoadingMore) {
return;
}

if (pagesBeforeStart < PAGE_THRESHOLD) {
fetchPreviousEntries();
} else if (pagesAfterEnd < PAGE_THRESHOLD) {
fetchNextEntries();
}
}}
reportVisibleInterval={handlePagination}
loadNewerItems={noop}
reloadItems={fetchEntries}
highlightedItem={highlight ?? null}
Expand Down

0 comments on commit 378803a

Please sign in to comment.