Skip to content

Commit

Permalink
pair with Garrett to make a minimal impact on the ux
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierM committed Feb 6, 2020
1 parent 14e4d95 commit 2d10cdd
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,15 @@ export const makeMapStateToProps = () => {
return mapStateToProps;
};

export const updateTimerangeUrl = (timeRange: UrlInputsModel): UrlInputsModel => {
export const updateTimerangeUrl = (
timeRange: UrlInputsModel,
isInitializing: boolean
): UrlInputsModel => {
if (timeRange.global.timerange.kind === 'relative') {
timeRange.global.timerange.from = formatDate(timeRange.global.timerange.fromStr);
timeRange.global.timerange.to = formatDate(timeRange.global.timerange.toStr, { roundUp: true });
}
if (timeRange.timeline.timerange.kind === 'relative') {
if (timeRange.timeline.timerange.kind === 'relative' && isInitializing) {
timeRange.timeline.timerange.from = formatDate(timeRange.timeline.timerange.fromStr);
timeRange.timeline.timerange.to = formatDate(timeRange.timeline.timerange.toStr, {
roundUp: true,
Expand All @@ -198,10 +201,12 @@ export const updateTimerangeUrl = (timeRange: UrlInputsModel): UrlInputsModel =>
};

export const updateUrlStateString = ({
isInitializing,
history,
newUrlStateString,
pathName,
search,
updateTimerange,
urlKey,
}: UpdateUrlStateString): string => {
if (urlKey === CONSTANTS.appQuery) {
Expand All @@ -215,14 +220,14 @@ export const updateUrlStateString = ({
urlStateKey: urlKey,
});
}
} else if (urlKey === CONSTANTS.timerange) {
} else if (urlKey === CONSTANTS.timerange && updateTimerange) {
const queryState = decodeRisonUrlState<UrlInputsModel>(newUrlStateString);
if (queryState != null && queryState.global != null) {
return replaceStateInLocation({
history,
pathName,
search,
urlStateToReplace: updateTimerangeUrl(queryState),
urlStateToReplace: updateTimerangeUrl(queryState, isInitializing),
urlStateKey: urlKey,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ describe('UrlStateContainer', () => {
});
});

describe('After Initialization, keep Relative Date up to date', () => {
describe('After Initialization, keep Relative Date up to date for global only on detections page', () => {
test.each(testCases)(
'%o',
async (page, namespaceLower, namespaceUpper, examplePath, type, pageName, detailName) => {
Expand All @@ -187,17 +187,15 @@ describe('UrlStateContainer', () => {
hookProps: getMockPropsObj({
page: CONSTANTS.hostsPage,
examplePath: '/hosts',
namespaceLower,
pageName,
detailName,
namespaceLower: 'hosts',
pageName: SiemPageName.hosts,
detailName: undefined,
}).relativeTimeSearch.undefinedQuery,
});
wrapper.update();
await wait();
if (CONSTANTS.hostsPage === page) {
// There is no change in url state, so that's expected we only have two actions
expect(mockSetRelativeRangeDatePicker.mock.calls.length).toEqual(2);
} else {

if (CONSTANTS.detectionsPage === page) {
expect(mockSetRelativeRangeDatePicker.mock.calls[3][0]).toEqual({
from: 11223344556677,
fromStr: 'now-1d/d',
Expand All @@ -208,13 +206,16 @@ describe('UrlStateContainer', () => {
});

expect(mockSetRelativeRangeDatePicker.mock.calls[2][0]).toEqual({
from: 11223344556677,
from: 1558732849370,
fromStr: 'now-15m',
kind: 'relative',
to: 11223344556677,
to: 1558733749370,
toStr: 'now',
id: 'timeline',
});
} else {
// There is no change in url state, so that's expected we only have two actions
expect(mockSetRelativeRangeDatePicker.mock.calls.length).toEqual(2);
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ export const getMockPropsObj = ({
pageName,
detailName
),
undefinedLinkQuery: getMockProps(
{
hash: '',
pathname: examplePath,
search: `?query=(language:kuery,query:'host.name:%22siem-es%22')&timerange=(global:(linkTo:!(timeline),timerange:(from:1558591200000,fromStr:now-1d%2Fd,kind:relative,to:1558677599999,toStr:now-1d%2Fd)),timeline:(linkTo:!(global),timerange:(from:1558732849370,fromStr:now-15m,kind:relative,to:1558733749370,toStr:now)))`,
state: '',
},
page,
null,
pageName,
detailName
),
},
absoluteTimeSearch: {
undefinedQuery: getMockProps(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export type UrlStateContainerPropTypes = RouteSpyState &

export interface PreviousLocationUrlState {
pathName: string | undefined;
pageName: string | undefined;
urlState: UrlState;
}

Expand Down Expand Up @@ -155,9 +156,11 @@ export interface ReplaceStateInLocation<T> {
}

export interface UpdateUrlStateString {
isInitializing: boolean;
history?: H.History;
newUrlStateString: string;
pathName: string;
search: string;
updateTimerange: boolean;
urlKey: KeyUrlState;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
ALL_URL_STATE_KEYS,
UrlStateToRedux,
} from './types';
import { SiemPageName } from '../../pages/home/types';

function usePrevious(value: PreviousLocationUrlState) {
const ref = useRef<PreviousLocationUrlState>(value);
Expand All @@ -52,7 +53,7 @@ export const useUrlStateHooks = ({
const [isInitializing, setIsInitializing] = useState(true);
const apolloClient = useApolloClient();
const { filterManager, savedQueries } = useKibana().services.data.query;
const prevProps = usePrevious({ pathName, urlState });
const prevProps = usePrevious({ pathName, pageName, urlState });

const handleInitialize = (type: UrlStateType, needUpdate?: boolean) => {
let mySearch = search;
Expand All @@ -65,9 +66,11 @@ export const useUrlStateHooks = ({
if (newUrlStateString) {
mySearch = updateUrlStateString({
history,
isInitializing,
newUrlStateString,
pathName,
search: mySearch,
updateTimerange: (needUpdate ?? false) || isInitializing,
urlKey,
});
if (isInitializing || needUpdate) {
Expand Down Expand Up @@ -200,7 +203,7 @@ export const useUrlStateHooks = ({
}
});
} else if (pathName !== prevProps.pathName) {
handleInitialize(type, true);
handleInitialize(type, pageName === SiemPageName.detections);
}
}, [isInitializing, history, pathName, pageName, prevProps, urlState]);

Expand Down

0 comments on commit 2d10cdd

Please sign in to comment.