Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwizp committed Feb 7, 2020
1 parent afdf8ca commit d673105
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/plugins/kibana_utils/common/url/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const stringifyUrlQuery = (
const encodedQuery =
encodeFunction &&
transform(query, (result, value, key) => {
if (key && value) {
if (key) {
result[key] = encodeFunction(value, true);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { parse, stringify } from 'query-string';
import React from 'react';
import { withRouter, RouteComponentProps } from 'react-router-dom';
import { FlatObject } from '../frontend_types';
import { RendererFunction } from '../utils/typed_react';
import { url } from '../../../../../../src/plugins/kibana_utils/public';

type StateCallback<T> = (previousState: T) => T;

Expand All @@ -31,9 +31,9 @@ export class WithURLStateComponent<URLState extends object> extends React.Compon
> {
private get URLState(): URLState {
// slice because parse does not account for the initial ? in the search string
return parse(decodeURIComponent(this.props.history.location.search).substring(1), {
sort: false,
}) as URLState;
return url.parseUrlQuery<URLState>(
decodeURIComponent(this.props.history.location.search).substring(1)
);
}

private historyListener: (() => void) | null = null;
Expand Down Expand Up @@ -65,13 +65,10 @@ export class WithURLStateComponent<URLState extends object> extends React.Compon
newState = state;
}

const search: string = stringify(
{
...pastState,
...newState,
},
{ sort: false }
);
const search: string = url.stringifyUrlQuery({
...pastState,
...newState,
});

const newLocation = {
...this.props.history.location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

import { Location } from 'history';
import omit from 'lodash/fp/omit';
import { parse as parseQueryString, stringify as stringifyQueryString } from 'querystring';
import React from 'react';
import { RouteComponentProps, withRouter } from 'react-router-dom';
// eslint-disable-next-line @typescript-eslint/camelcase
import { decode_object, encode_object } from 'rison-node';
import { Omit } from '../lib/lib';
import { url } from '../../../../../../src/plugins/kibana_utils/public';

interface AnyObject {
[key: string]: any;
Expand Down Expand Up @@ -102,22 +102,22 @@ const encodeRisonAppState = (state: AnyObject) => ({
export const mapRisonAppLocationToState = <State extends {}>(
mapState: (risonAppState: AnyObject) => State = (state: AnyObject) => state as State
) => (location: Location): State => {
const queryValues = parseQueryString(location.search.substring(1));
const queryValues = url.parseUrlQuery(location.search.substring(1));
const decodedState = decodeRisonAppState(queryValues);
return mapState(decodedState);
};

export const mapStateToRisonAppLocation = <State extends {}>(
mapState: (state: State) => AnyObject = (state: State) => state
) => (state: State, location: Location): Location => {
const previousQueryValues = parseQueryString(location.search.substring(1));
const previousQueryValues = url.parseUrlQuery(location.search.substring(1));
const previousState = decodeRisonAppState(previousQueryValues);

const encodedState = encodeRisonAppState({
...previousState,
...mapState(state),
});
const newQueryValues = stringifyQueryString({
const newQueryValues = url.stringifyUrlQuery({
...previousQueryValues,
...encodedState,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

import React, { FC } from 'react';
import { i18n } from '@kbn/i18n';
import { parse } from 'query-string';

import { MlRoute, PageLoader, PageProps } from '../../router';
import { useResolver } from '../../use_resolver';
import { basicResolvers } from '../../resolvers';
import { Page } from '../../../jobs/new_job/recognize';
import { checkViewOrCreateJobs } from '../../../jobs/new_job/recognize/resolvers';
import { mlJobService } from '../../../services/job_service';
import { ANOMALY_DETECTION_BREADCRUMB, ML_BREADCRUMB } from '../../breadcrumbs';
import { url } from '../../../../../../../../../src/plugins/kibana_utils/public';

const breadcrumbs = [
ML_BREADCRUMB,
Expand Down Expand Up @@ -42,7 +41,7 @@ export const checkViewOrCreateRoute: MlRoute = {
};

const PageWrapper: FC<PageProps> = ({ location, config, deps }) => {
const { id, index, savedSearchId }: Record<string, any> = parse(location.search, { sort: false });
const { id, index, savedSearchId } = url.parseUrlQuery(location.search);
const { context, results } = useResolver(index, savedSearchId, config, {
...basicResolvers(deps),
existingJobsAndGroups: mlJobService.getJobAndGroupIds,
Expand All @@ -56,9 +55,7 @@ const PageWrapper: FC<PageProps> = ({ location, config, deps }) => {
};

const CheckViewOrCreateWrapper: FC<PageProps> = ({ location, config, deps }) => {
const { id: moduleId, index: indexPatternId }: Record<string, any> = parse(location.search, {
sort: false,
});
const { id: moduleId, index: indexPatternId } = url.parseUrlQuery(location.search);

// the single resolver checkViewOrCreateJobs redirects only. so will always reject
useResolver(undefined, undefined, config, {
Expand Down
26 changes: 10 additions & 16 deletions x-pack/legacy/plugins/siem/public/components/url_state/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import { decode, encode } from 'rison-node';
import * as H from 'history';
import { stringify, parse } from 'query-string';
import { Query, esFilters } from 'src/plugins/data/public';

import { isEmpty } from 'lodash/fp';
Expand All @@ -23,6 +22,7 @@ import {
Timeline,
UpdateUrlStateString,
} from './types';
import { url } from '../../../../../../../src/plugins/kibana_utils/public';

export const decodeRisonUrlState = <T>(value: string | undefined): T | null => {
try {
Expand All @@ -41,7 +41,7 @@ export const encodeRisonUrlState = (state: any) => encode(state);
export const getQueryStringFromLocation = (search: string) => search.substring(1);

export const getParamFromQueryString = (queryString: string, key: string): string | undefined => {
const parsedQueryString: Record<string, unknown> = parse(queryString, { sort: false });
const parsedQueryString = url.parseUrlQuery(queryString);
const queryParam = parsedQueryString[key];

return Array.isArray(queryParam) ? queryParam[0] : queryParam;
Expand All @@ -50,30 +50,24 @@ export const getParamFromQueryString = (queryString: string, key: string): strin
export const replaceStateKeyInQueryString = <T>(stateKey: string, urlState: T) => (
queryString: string
): string => {
const previousQueryValues = parse(queryString);
const previousQueryValues = url.parseUrlQuery(queryString);
if (urlState == null || (typeof urlState === 'string' && urlState === '')) {
delete previousQueryValues[stateKey];

return stringify(
{
...previousQueryValues,
},
{ sort: false }
);
return url.stringifyUrlQuery({
...previousQueryValues,
});
}

// ಠ_ಠ Code was copied from x-pack/legacy/plugins/infra/public/utils/url_state.tsx ಠ_ಠ
// Remove this if these utilities are promoted to kibana core
const encodedUrlState =
typeof urlState !== 'undefined' ? encodeRisonUrlState(urlState) : undefined;

return stringify(
{
...previousQueryValues,
[stateKey]: encodedUrlState,
},
{ sort: false }
);
return url.stringifyUrlQuery({
...previousQueryValues,
[stateKey]: encodedUrlState,
});
};

export const replaceQueryStringInLocation = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* you may not use this file except in compliance with the Elastic License.
*/

import qs from 'querystring';
import { HttpFetchQuery } from 'src/core/public';
import { AppAction } from '../action';
import { MiddlewareFactory } from '../../types';
import { url } from '../../../../../../../../src/plugins/kibana_utils/public';

export const alertMiddlewareFactory: MiddlewareFactory = coreStart => {
const qp = qs.parse(window.location.search.slice(1));
const qp = url.parseUrlQuery(window.location.search.slice(1));

return api => next => async (action: AppAction) => {
next(action);
Expand Down

0 comments on commit d673105

Please sign in to comment.