Skip to content

Commit

Permalink
Simplify component lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
afgomez authored and Alejandro Fernández Gómez committed Nov 16, 2020
1 parent 34991f6 commit caf9969
Showing 1 changed file with 18 additions and 37 deletions.
55 changes: 18 additions & 37 deletions x-pack/plugins/infra/public/components/log_stream/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { useMemo, useCallback } from 'react';
import { noop, isUndefined } from 'lodash';
import usePrevious from 'react-use/lib/usePrevious';
import useDeepCompareEffect from 'react-use/lib/useDeepCompareEffect';
import React, { useMemo, useCallback, useEffect } from 'react';
import { noop } from 'lodash';
import { euiStyled } from '../../../../observability/public';

import { LogEntriesCursor } from '../../../common/http_api';
Expand All @@ -30,17 +28,15 @@ export interface LogStreamProps {
height?: string | number;
}

export const LogStream: React.FC<LogStreamProps> = (props) => {
const {
sourceId = 'default',
startTimestamp,
endTimestamp,
query,
center,
highlight,
height = '400px',
} = props;

export const LogStream: React.FC<LogStreamProps> = ({
sourceId = 'default',
startTimestamp,
endTimestamp,
query,
center,
highlight,
height = '400px',
}) => {
// source boilerplate
const { services } = useKibana();
if (!services?.http?.fetch) {
Expand Down Expand Up @@ -81,8 +77,6 @@ Read more at https://github.com/elastic/kibana/blob/master/src/plugins/kibana_re
});

// Derived state
const prevProps = usePrevious(props);

const isReloading =
isLoadingSourceConfiguration || loadingState === 'uninitialized' || loadingState === 'loading';

Expand All @@ -105,26 +99,13 @@ Read more at https://github.com/elastic/kibana/blob/master/src/plugins/kibana_re
const parsedHeight = typeof height === 'number' ? `${height}px` : height;

// Component lifetime
useDeepCompareEffect(() => {
const shouldReloadSourceConfiguration =
isUndefined(prevProps) || sourceId !== prevProps.sourceId;

const shouldReloadEntries =
isUndefined(prevProps) ||
shouldReloadSourceConfiguration ||
prevProps.startTimestamp !== startTimestamp ||
prevProps.endTimestamp !== endTimestamp ||
prevProps.query !== query ||
prevProps.center !== center;

if (shouldReloadSourceConfiguration) {
loadSourceConfiguration();
}

if (shouldReloadEntries) {
fetchEntries();
}
}, [prevProps, props, loadSourceConfiguration, fetchEntries]);
useEffect(() => {
loadSourceConfiguration();
}, [loadSourceConfiguration]);

useEffect(() => {
fetchEntries();
}, [fetchEntries]);

// Pagination handler
const handlePagination = useCallback(
Expand Down

0 comments on commit caf9969

Please sign in to comment.