diff --git a/.eslintrc.js b/.eslintrc.js index 019f93d91b55..5ff1aba1f3ff 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -396,6 +396,14 @@ module.exports = { 'no-console': ['warn', { allow: ['error'] }], }, }, + { + plugins: ['react-hooks'], + files: ['x-pack/legacy/plugins/apm/**/*.{ts,tsx}'], + rules: { + 'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks + 'react-hooks/exhaustive-deps': ['error', { additionalHooks: '^useFetcher$' }], + }, + }, /** * GIS overrides diff --git a/x-pack/legacy/plugins/apm/public/components/app/Main/useUpdateBadgeEffect.ts b/x-pack/legacy/plugins/apm/public/components/app/Main/useUpdateBadgeEffect.ts index 807655450b4a..866a5a6884d1 100644 --- a/x-pack/legacy/plugins/apm/public/components/app/Main/useUpdateBadgeEffect.ts +++ b/x-pack/legacy/plugins/apm/public/components/app/Main/useUpdateBadgeEffect.ts @@ -11,6 +11,7 @@ import { useCore } from '../../../hooks/useCore'; export const useUpdateBadgeEffect = () => { const { chrome } = useCore(); + useEffect(() => { const uiCapabilities = capabilities.get(); chrome.setBadge( @@ -26,5 +27,5 @@ export const useUpdateBadgeEffect = () => { } : undefined ); - }, []); + }, [chrome]); }; diff --git a/x-pack/legacy/plugins/apm/public/components/app/ServiceOverview/index.tsx b/x-pack/legacy/plugins/apm/public/components/app/ServiceOverview/index.tsx index 2b81e9f7f0a7..f0f0a8acbfbe 100644 --- a/x-pack/legacy/plugins/apm/public/components/app/ServiceOverview/index.tsx +++ b/x-pack/legacy/plugins/apm/public/components/app/ServiceOverview/index.tsx @@ -70,7 +70,7 @@ export function ServiceOverview() { ) }); } - }, [data.hasLegacyData]); + }, [data.hasLegacyData, core.http.basePath]); useTrackPageview({ app: 'apm', path: 'services_overview' }); useTrackPageview({ app: 'apm', path: 'services_overview', delay: 15000 }); diff --git a/x-pack/legacy/plugins/apm/public/components/app/Settings/AddSettings/AddSettingFlyout.tsx b/x-pack/legacy/plugins/apm/public/components/app/Settings/AddSettings/AddSettingFlyout.tsx index 368d70e83b76..1726c2233d40 100644 --- a/x-pack/legacy/plugins/apm/public/components/app/Settings/AddSettings/AddSettingFlyout.tsx +++ b/x-pack/legacy/plugins/apm/public/components/app/Settings/AddSettings/AddSettingFlyout.tsx @@ -47,10 +47,6 @@ export function AddSettingsFlyout({ onSubmit, selectedConfig }: Props) { - if (!isOpen) { - return null; - } - const [environment, setEnvironment] = useState( selectedConfig ? selectedConfig.service.environment || ENVIRONMENT_NOT_DEFINED @@ -88,6 +84,11 @@ export function AddSettingsFlyout({ const hasCorrectDecimals = Number.isInteger(sampleRateFloat * 1000); const isSampleRateValid = sampleRateFloat >= 0 && sampleRateFloat <= 1 && hasCorrectDecimals; + + if (!isOpen) { + return null; + } + return ( diff --git a/x-pack/legacy/plugins/apm/public/components/app/TransactionOverview/List/index.tsx b/x-pack/legacy/plugins/apm/public/components/app/TransactionOverview/List/index.tsx index 487759dcc0ac..3f6cca5dcb61 100644 --- a/x-pack/legacy/plugins/apm/public/components/app/TransactionOverview/List/index.tsx +++ b/x-pack/legacy/plugins/apm/public/components/app/TransactionOverview/List/index.tsx @@ -25,11 +25,10 @@ const TransactionNameLink = styled(TransactionLink)` interface Props { items: ITransactionGroup[]; - serviceName: string; isLoading: boolean; } -export function TransactionList({ items, serviceName, isLoading }: Props) { +export function TransactionList({ items, isLoading }: Props) { const columns: Array> = useMemo( () => [ { @@ -39,7 +38,7 @@ export function TransactionList({ items, serviceName, isLoading }: Props) { }), width: '50%', sortable: true, - render: (transactionName: string, item: typeof items[0]) => { + render: (transactionName: string, item: ITransactionGroup) => { return ( } ], - [serviceName] + [] ); const noItemsMessage = ( diff --git a/x-pack/legacy/plugins/apm/public/components/app/TransactionOverview/index.tsx b/x-pack/legacy/plugins/apm/public/components/app/TransactionOverview/index.tsx index 8622f11ae1e7..e2244e7ea108 100644 --- a/x-pack/legacy/plugins/apm/public/components/app/TransactionOverview/index.tsx +++ b/x-pack/legacy/plugins/apm/public/components/app/TransactionOverview/index.tsx @@ -152,7 +152,6 @@ export function TransactionOverview({ urlParams }: Props) { diff --git a/x-pack/legacy/plugins/apm/public/components/shared/KueryBar/index.tsx b/x-pack/legacy/plugins/apm/public/components/shared/KueryBar/index.tsx index df18b9a464e3..797dd7fcdb2f 100644 --- a/x-pack/legacy/plugins/apm/public/components/shared/KueryBar/index.tsx +++ b/x-pack/legacy/plugins/apm/public/components/shared/KueryBar/index.tsx @@ -120,15 +120,19 @@ export function KueryBar() { let didCancel = false; async function loadIndexPattern() { - setState({ ...state, isLoadingIndexPattern: true }); + setState(value => ({ ...value, isLoadingIndexPattern: true })); const indexPattern = await getAPMIndexPatternForKuery(); if (didCancel) { return; } if (!indexPattern) { - setState({ ...state, isLoadingIndexPattern: false }); + setState(value => ({ ...value, isLoadingIndexPattern: false })); } else { - setState({ ...state, indexPattern, isLoadingIndexPattern: false }); + setState(value => ({ + ...value, + indexPattern, + isLoadingIndexPattern: false + })); } } loadIndexPattern(); diff --git a/x-pack/legacy/plugins/apm/public/context/UrlParamsContext/index.tsx b/x-pack/legacy/plugins/apm/public/context/UrlParamsContext/index.tsx index 3043b033c932..9252abb599f3 100644 --- a/x-pack/legacy/plugins/apm/public/context/UrlParamsContext/index.tsx +++ b/x-pack/legacy/plugins/apm/public/context/UrlParamsContext/index.tsx @@ -47,17 +47,19 @@ const UrlParamsProvider: React.ComponentClass<{}> = withRouter( ({ location, children }) => { const refUrlParams = useRef(resolveUrlParams(location, {})); + const { start, end, rangeFrom, rangeTo } = refUrlParams.current; + const [, forceUpdate] = useState(''); const urlParams = useMemo( () => resolveUrlParams(location, { - start: refUrlParams.current.start, - end: refUrlParams.current.end, - rangeFrom: refUrlParams.current.rangeFrom, - rangeTo: refUrlParams.current.rangeTo + start, + end, + rangeFrom, + rangeTo }), - [location, refUrlParams.current] + [location, start, end, rangeFrom, rangeTo] ); refUrlParams.current = urlParams; diff --git a/x-pack/legacy/plugins/apm/public/hooks/useFetcher.tsx b/x-pack/legacy/plugins/apm/public/hooks/useFetcher.tsx index 0ff770e25707..a540c261def4 100644 --- a/x-pack/legacy/plugins/apm/public/hooks/useFetcher.tsx +++ b/x-pack/legacy/plugins/apm/public/hooks/useFetcher.tsx @@ -74,14 +74,22 @@ export function useFetcher( dispatchStatus({ id, isLoading: false }); didCancel = true; }; - }, [...effectKey, counter]); + /* eslint-disable react-hooks/exhaustive-deps */ + }, [ + counter, + id, + preservePreviousResponse, + dispatchStatus, + ...effectKey + /* eslint-enable react-hooks/exhaustive-deps */ + ]); return useMemo( () => ({ ...result, refresh: () => { // this will invalidate the effectKey and will result in a new request - setCounter(counter + 1); + setCounter(count => count + 1); } }), [result] diff --git a/x-pack/legacy/plugins/apm/public/hooks/useTransactionBreakdown.ts b/x-pack/legacy/plugins/apm/public/hooks/useTransactionBreakdown.ts index 9d6befabbead..22bfb1a1bc23 100644 --- a/x-pack/legacy/plugins/apm/public/hooks/useTransactionBreakdown.ts +++ b/x-pack/legacy/plugins/apm/public/hooks/useTransactionBreakdown.ts @@ -30,7 +30,7 @@ export function useTransactionBreakdown() { uiFilters }); } - }, [serviceName, start, end, uiFilters]); + }, [serviceName, start, end, transactionType, transactionName, uiFilters]); const receivedDataDuringLifetime = useRef(false); diff --git a/x-pack/legacy/plugins/apm/public/hooks/useTransactionCharts.ts b/x-pack/legacy/plugins/apm/public/hooks/useTransactionCharts.ts index c684d8d4c756..629f6bb60e1f 100644 --- a/x-pack/legacy/plugins/apm/public/hooks/useTransactionCharts.ts +++ b/x-pack/legacy/plugins/apm/public/hooks/useTransactionCharts.ts @@ -31,7 +31,7 @@ export function useTransactionCharts() { const memoizedData = useMemo( () => getTransactionCharts({ transactionType }, data), - [data] + [data, transactionType] ); return {