kibana/x-pack/plugins/observability/public/hooks/use_query_params.ts

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;
* you may not use this file except in compliance with the Elastic License.
*/
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/data_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]);
}