kibana/x-pack/plugins/observability/public/hooks/use_query_params.ts
Brandon Kobel 4584a8b570
Elastic License 2.0 (#90099)
* Updating everything except the license headers themselves

* Applying ESLint rules

* Manually replacing the stragglers
2021-02-03 18:12:39 -08:00

32 lines
1.2 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { useLocation } from 'react-router-dom';
import { useMemo } from 'react';
import { parse } from 'query-string';
import { UI_SETTINGS, useKibanaUISettings } from './use_kibana_ui_settings';
import { TimePickerTime } from '../components/shared/date_picker';
import { getAbsoluteTime } from '../utils/date';
const getParsedParams = (search: string) => {
return search ? parse(search[0] === '?' ? search.slice(1) : search, { sort: false }) : {};
};
export function useQueryParams() {
const { from, to } = useKibanaUISettings<TimePickerTime>(UI_SETTINGS.TIMEPICKER_TIME_DEFAULTS);
const { rangeFrom, rangeTo } = getParsedParams(useLocation().search);
return useMemo(() => {
return {
start: (rangeFrom as string) ?? from,
end: (rangeTo as string) ?? to,
absStart: getAbsoluteTime((rangeFrom as string) ?? from)!,
absEnd: getAbsoluteTime((rangeTo as string) ?? to, { roundUp: true })!,
};
}, [rangeFrom, rangeTo, from, to]);
}