Skip to content

Commit

Permalink
[7.3] [APM] Add react-hooks lint rules for APM folder, fix dep… (#42182)
Browse files Browse the repository at this point in the history
* [APM] Add react-hooks lint rules for APM folder, fix dependencies (#42129)

* [APM] Add react-hooks lint rules for APM folder, fix dependencies

Closes #42128.

* Validate useFetcher dependencies as well

* Add useFetcher hook; move eslint config to kibana eslint config

* Fix type errors
  • Loading branch information
dgieselaar authored Aug 2, 2019
1 parent ede2e46 commit 9903479
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 28 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ export function AddSettingsFlyout({
onSubmit,
selectedConfig
}: Props) {
if (!isOpen) {
return null;
}

const [environment, setEnvironment] = useState<string | undefined>(
selectedConfig
? selectedConfig.service.environment || ENVIRONMENT_NOT_DEFINED
Expand Down Expand Up @@ -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 (
<EuiPortal>
<EuiFlyout size="s" onClose={onClose} ownFocus={true}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ITableColumn<ITransactionGroup>> = [
{
field: 'name',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,22 @@ export function TransactionOverview({

const { data: transactionCharts } = useTransactionCharts();

const {
data: transactionListData,
status: transactionListStatus
} = useTransactionList(urlParams);
const { data: hasMLJob = false } = useFetcher(() => {
return serviceName && transactionType
? getHasMLJob({ serviceName, transactionType })
: undefined;
}, [serviceName, transactionType]);

// TODO: improve urlParams typings.
// `serviceName` or `transactionType` will never be undefined here, and this check should not be needed
if (!serviceName || !transactionType) {
return null;
}

const {
data: transactionListData,
status: transactionListStatus
} = useTransactionList(urlParams);
const { data: hasMLJob = false } = useFetcher(
() => getHasMLJob({ serviceName, transactionType }),
[serviceName, transactionType]
);

return (
<React.Fragment>
{serviceTransactionTypes.length > 1 ? (
Expand Down Expand Up @@ -139,7 +140,6 @@ export function TransactionOverview({
<TransactionList
isLoading={transactionListStatus === 'loading'}
items={transactionListData}
serviceName={serviceName}
/>
</EuiPanel>
</React.Fragment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,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;
Expand Down
12 changes: 10 additions & 2 deletions x-pack/legacy/plugins/apm/public/hooks/useFetcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,22 @@ export function useFetcher<Response>(
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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function useTransactionBreakdown() {
uiFilters
});
}
}, [serviceName, start, end, uiFilters]);
}, [serviceName, start, end, transactionType, transactionName, uiFilters]);

const receivedDataDuringLifetime = useRef(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function useTransactionCharts() {

const memoizedData = useMemo(
() => getTransactionCharts({ transactionType }, data),
[data]
[data, transactionType]
);

return {
Expand Down

0 comments on commit 9903479

Please sign in to comment.