Skip to content

Commit

Permalink
manually fix remaining issues
Browse files Browse the repository at this point in the history
  • Loading branch information
oatkiller committed Jun 1, 2020
1 parent 5025447 commit e028461
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,10 @@ const CreateRulePageComponent: React.FC = () => {
[RuleStep.ruleActions]: false,
});
const [{ isLoading, isSaved }, setRule] = usePersistRule();
const actionMessageParams = useMemo(
() =>
getActionMessageParams((stepsData.current['define-rule'].data as DefineStepRule).ruleType),
[stepsData.current['define-rule'].data]
);
const defineRuleData: DefineStepRule = stepsData.current['define-rule'].data;
const actionMessageParams = useMemo(() => getActionMessageParams(defineRuleData.ruleType), [
defineRuleData.ruleType,
]);

const setStepData = useCallback(
(step: RuleStep, data: unknown, isValid: boolean) => {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/siem/public/cases/containers/use_get_tags.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 { useEffect, useReducer } from 'react';
import { useEffect, useReducer, useCallback } from 'react';

import { errorToToaster, useStateToaster } from '../../common/components/toasters';
import { getTags } from './api';
Expand Down Expand Up @@ -86,7 +86,7 @@ export const useGetTags = (): UseGetTags => {
abortCtrl.abort();
didCancel = true;
};
});
}, [dispatchToaster]);
useEffect(() => {
callFetch();
}, [callFetch]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,18 @@ export const MarkdownEditor = React.memo<{
[onChange]
);

const setCursorPosition = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
if (onCursorPositionUpdate) {
onCursorPositionUpdate({
start: e!.target!.selectionStart ?? 0,
end: e!.target!.selectionEnd ?? 0,
});
}
return false;
};
const setCursorPosition = useCallback(
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
if (onCursorPositionUpdate) {
onCursorPositionUpdate({
start: e!.target!.selectionStart ?? 0,
end: e!.target!.selectionEnd ?? 0,
});
}
return false;
},
[onCursorPositionUpdate]
);

const tabs = useMemo(
() => [
Expand Down
192 changes: 104 additions & 88 deletions x-pack/plugins/siem/public/common/components/url_state/use_url_state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,104 +56,103 @@ export const useUrlStateHooks = ({
const { filterManager, savedQueries } = useKibana().services.data.query;
const prevProps = usePrevious({ pathName, pageName, urlState });

const handleInitialize = (type: UrlStateType, needUpdate?: boolean) => {
let mySearch = search;
let urlStateToUpdate: UrlStateToRedux[] = [];
URL_STATE_KEYS[type].forEach((urlKey: KeyUrlState) => {
const newUrlStateString = getParamFromQueryString(
getQueryStringFromLocation(mySearch),
urlKey
);
if (newUrlStateString) {
mySearch = updateUrlStateString({
history,
isInitializing,
newUrlStateString,
pathName,
search: mySearch,
updateTimerange: (needUpdate ?? false) || isInitializing,
urlKey,
});
if (isInitializing || needUpdate) {
const updatedUrlStateString =
getParamFromQueryString(getQueryStringFromLocation(mySearch), urlKey) ??
newUrlStateString;
if (isInitializing || !deepEqual(updatedUrlStateString, newUrlStateString)) {
urlStateToUpdate = [
...urlStateToUpdate,
{
urlKey,
newUrlStateString: updatedUrlStateString,
},
];
useEffect(() => {
const handleInitialize = (type: UrlStateType, needUpdate?: boolean) => {
let mySearch = search;
let urlStateToUpdate: UrlStateToRedux[] = [];
URL_STATE_KEYS[type].forEach((urlKey: KeyUrlState) => {
const newUrlStateString = getParamFromQueryString(
getQueryStringFromLocation(mySearch),
urlKey
);
if (newUrlStateString) {
mySearch = updateUrlStateString({
history,
isInitializing,
newUrlStateString,
pathName,
search: mySearch,
updateTimerange: (needUpdate ?? false) || isInitializing,
urlKey,
});
if (isInitializing || needUpdate) {
const updatedUrlStateString =
getParamFromQueryString(getQueryStringFromLocation(mySearch), urlKey) ??
newUrlStateString;
if (isInitializing || !deepEqual(updatedUrlStateString, newUrlStateString)) {
urlStateToUpdate = [
...urlStateToUpdate,
{
urlKey,
newUrlStateString: updatedUrlStateString,
},
];
}
}
} else if (
urlKey === CONSTANTS.appQuery &&
urlState[urlKey] != null &&
urlState[urlKey]?.query === ''
) {
mySearch = replaceStateInLocation({
history,
pathName,
search: mySearch,
urlStateToReplace: '',
urlStateKey: urlKey,
});
} else if (urlKey === CONSTANTS.filters && isEmpty(urlState[urlKey])) {
mySearch = replaceStateInLocation({
history,
pathName,
search: mySearch,
urlStateToReplace: '',
urlStateKey: urlKey,
});
} else if (
urlKey === CONSTANTS.timeline &&
urlState[urlKey] != null &&
urlState[urlKey].id === ''
) {
mySearch = replaceStateInLocation({
history,
pathName,
search: mySearch,
urlStateToReplace: '',
urlStateKey: urlKey,
});
} else {
mySearch = replaceStateInLocation({
history,
pathName,
search: mySearch,
urlStateToReplace: urlState[urlKey] || '',
urlStateKey: urlKey,
});
}
} else if (
urlKey === CONSTANTS.appQuery &&
urlState[urlKey] != null &&
urlState[urlKey]?.query === ''
) {
mySearch = replaceStateInLocation({
history,
pathName,
search: mySearch,
urlStateToReplace: '',
urlStateKey: urlKey,
});
} else if (urlKey === CONSTANTS.filters && isEmpty(urlState[urlKey])) {
mySearch = replaceStateInLocation({
history,
pathName,
search: mySearch,
urlStateToReplace: '',
urlStateKey: urlKey,
});
} else if (
urlKey === CONSTANTS.timeline &&
urlState[urlKey] != null &&
urlState[urlKey].id === ''
) {
});
difference(ALL_URL_STATE_KEYS, URL_STATE_KEYS[type]).forEach((urlKey: KeyUrlState) => {
mySearch = replaceStateInLocation({
history,
pathName,
search: mySearch,
urlStateToReplace: '',
urlStateKey: urlKey,
});
} else {
mySearch = replaceStateInLocation({
history,
pathName,
search: mySearch,
urlStateToReplace: urlState[urlKey] || '',
urlStateKey: urlKey,
});
}
});
difference(ALL_URL_STATE_KEYS, URL_STATE_KEYS[type]).forEach((urlKey: KeyUrlState) => {
mySearch = replaceStateInLocation({
history,
pathName,
search: mySearch,
urlStateToReplace: '',
urlStateKey: urlKey,
});
});

setInitialStateFromUrl({
apolloClient,
detailName,
filterManager,
indexPattern,
pageName,
savedQueries,
updateTimeline,
updateTimelineIsLoading,
urlStateToUpdate,
})();
};

useEffect(() => {
setInitialStateFromUrl({
apolloClient,
detailName,
filterManager,
indexPattern,
pageName,
savedQueries,
updateTimeline,
updateTimelineIsLoading,
urlStateToUpdate,
})();
};
const type: UrlStateType = getUrlType(pageName);
if (isInitializing && pageName != null && pageName !== '') {
handleInitialize(type);
Expand Down Expand Up @@ -206,7 +205,24 @@ export const useUrlStateHooks = ({
} else if (pathName !== prevProps.pathName) {
handleInitialize(type, pageName === SiemPageName.detections);
}
}, [isInitializing, history, pathName, pageName, prevProps, urlState, handleInitialize, search]);
}, [
apolloClient,
detailName,
filterManager,
history,
indexPattern,
isInitializing,
pageName,
pathName,
prevProps.pathName,
prevProps.urlState,
savedQueries,
search,
setInitialStateFromUrl,
updateTimeline,
updateTimelineIsLoading,
urlState,
]);

useEffect(() => {
document.title = `${getTitle(pageName, detailName, navTabs)} - Kibana`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface OwnProps extends QueryTemplateProps {
indexKey: LastEventIndexKey;
}

export function useLastEventTimeQuery<TCache = object>(
export function useLastEventTimeQuery(
indexKey: LastEventIndexKey,
details: LastTimeDetails,
sourceId: string
Expand All @@ -44,7 +44,10 @@ export function useLastEventTimeQuery<TCache = object>(
const [currentIndexKey, updateCurrentIndexKey] = useState<LastEventIndexKey | null>(null);
const [defaultIndex] = useUiSetting$<string[]>(DEFAULT_INDEX_KEY);
const apolloClient = useApolloClient();
async function fetchLastEventTime(signal: AbortSignal) {

useEffect(() => {
const abortCtrl = new AbortController();
const signal = abortCtrl.signal;
updateLoading(true);
if (apolloClient) {
apolloClient
Expand Down Expand Up @@ -77,14 +80,8 @@ export function useLastEventTimeQuery<TCache = object>(
}
);
}
}

useEffect(() => {
const abortCtrl = new AbortController();
const signal = abortCtrl.signal;
fetchLastEventTime(signal);
return () => abortCtrl.abort();
}, [apolloClient, indexKey, details.hostName, details.ip, fetchLastEventTime]);
}, [apolloClient, currentIndexKey, defaultIndex, details, indexKey, sourceId]);

return { lastSeen, loading, errorMessage };
}
15 changes: 6 additions & 9 deletions x-pack/plugins/siem/public/common/containers/source/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const getIndexFields = memoizeOne(
);

export const getBrowserFields = memoizeOne(
(title: string, fields: IndexField[]): BrowserFields =>
(_title: string, fields: IndexField[]): BrowserFields =>
fields && fields.length > 0
? fields.reduce<BrowserFields>(
(accumulator: BrowserFields, field: IndexField) =>
Expand Down Expand Up @@ -131,7 +131,10 @@ export const useWithSource = (sourceId: string, indices: string[]) => {
const [errorMessage, updateErrorMessage] = useState<string | null>(null);

const apolloClient = useApolloClient();
async function fetchSource(signal: AbortSignal) {

useEffect(() => {
const abortCtrl = new AbortController();
const signal = abortCtrl.signal;
updateLoading(true);
if (apolloClient) {
apolloClient
Expand Down Expand Up @@ -166,14 +169,8 @@ export const useWithSource = (sourceId: string, indices: string[]) => {
}
);
}
}

useEffect(() => {
const abortCtrl = new AbortController();
const signal = abortCtrl.signal;
fetchSource(signal);
return () => abortCtrl.abort();
}, [apolloClient, sourceId, indices, fetchSource]);
}, [apolloClient, sourceId, indices]);

return { indicesExist, browserFields, indexPattern, loading, errorMessage };
};
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ export function useFirstLastSeenHostQuery<TCache = object>(
const [errorMessage, updateErrorMessage] = useState<string | null>(null);
const [defaultIndex] = useUiSetting$<string[]>(DEFAULT_INDEX_KEY);

async function fetchFirstLastSeenHost(signal: AbortSignal) {
useEffect(() => {
const abortCtrl = new AbortController();
const signal = abortCtrl.signal;
updateLoading(true);
return apolloClient
apolloClient
.query<GetHostFirstLastSeenQuery.Query, GetHostFirstLastSeenQuery.Variables>({
query: HostFirstLastSeenGqlQuery,
fetchPolicy: 'cache-first',
Expand Down Expand Up @@ -72,14 +74,8 @@ export function useFirstLastSeenHostQuery<TCache = object>(
updateErrorMessage(error.message);
}
);
}

useEffect(() => {
const abortCtrl = new AbortController();
const signal = abortCtrl.signal;
fetchFirstLastSeenHost(signal);
return () => abortCtrl.abort();
}, [fetchFirstLastSeenHost]);
}, [apolloClient, defaultIndex, hostName, sourceId]);

return { firstSeen, lastSeen, loading, errorMessage };
}
Loading

0 comments on commit e028461

Please sign in to comment.