Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Logs UI] Update <LogStream /> internal state when its props change #83302

Merged
merged 4 commits into from
Nov 18, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions x-pack/plugins/infra/public/components/log_stream/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
*/

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

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

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

// source boilerplate
const { services } = useKibana();
if (!services?.http?.fetch) {
Expand Down Expand Up @@ -78,6 +81,8 @@ 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 @@ -100,10 +105,26 @@ Read more at https://github.com/elastic/kibana/blob/master/src/plugins/kibana_re
const parsedHeight = typeof height === 'number' ? `${height}px` : height;

// Component lifetime
useMount(() => {
loadSourceConfiguration();
fetchEntries();
});
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]);
afgomez marked this conversation as resolved.
Show resolved Hide resolved

// Pagination handler
const handlePagination = useCallback(
Expand Down