Skip to content

Commit

Permalink
Move URL state to hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Zacqary committed Dec 19, 2019
1 parent 8cb525d commit c61f5b1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
*/

export * from './log_filter_state';
export * from './with_log_filter_url_state';
export * from './use_log_filter_url_state';
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 { useContext, useEffect } from 'react';
import * as rt from 'io-ts';
import { LogFilterState } from './log_filter_state';
import { replaceStateKeyInQueryString } from '../../../utils/url_state';
import { useUrlState } from '../../../utils/use_url_state';
import { getEncodeDecodeFromRT } from '../../../utils/validate_url_rt';

const logFilterRT = rt.union([
rt.type({
kind: rt.literal('kuery'),
expression: rt.string,
}),
rt.undefined,
]);
type LogFilterUrlState = rt.TypeOf<typeof logFilterRT>;

const LOG_FILTER_URL_STATE_KEY = 'logFilter';

export const useLogFilterUrlState = () => {
const { filterQueryAsKuery, applyLogFilterQuery } = useContext(LogFilterState.Context);
const [logFilterUrlState, setLogFilterUrlState] = useUrlState({
defaultState: filterQueryAsKuery,
urlStateKey: LOG_FILTER_URL_STATE_KEY,
writeDefaultState: true,
...getEncodeDecodeFromRT(logFilterRT),
});

/* eslint-disable react-hooks/exhaustive-deps */
useEffect(() => applyLogFilterQuery(logFilterUrlState.expression), [logFilterUrlState]);
useEffect(() => setLogFilterUrlState(filterQueryAsKuery), [filterQueryAsKuery]);
/* eslint-enable react-hooks/exhaustive-deps */
};

export const replaceLogFilterInQueryString = (expression: string) =>
replaceStateKeyInQueryString<LogFilterUrlState>('logFilter', {
kind: 'kuery',
expression,
});

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { PageContent } from '../../../components/page';

import { WithSummary } from '../../../containers/logs/log_summary';
import { LogViewConfiguration } from '../../../containers/logs/log_view_configuration';
import { LogFilterState } from '../../../containers/logs/log_filter';
import { LogFilterState, useLogFilterUrlState } from '../../../containers/logs/log_filter';
import {
LogFlyout as LogFlyoutState,
WithFlyoutOptionsUrlState,
Expand All @@ -42,6 +42,9 @@ export const LogsPageLogsContent: React.FunctionComponent = () => {
flyoutItem,
isLoading,
} = useContext(LogFlyoutState.Context);

useLogFilterUrlState();

const { logSummaryHighlights } = useContext(LogHighlightsState.Context);
const { applyLogFilterQuery } = useContext(LogFilterState.Context);
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { LogFlyout } from '../../../containers/logs/log_flyout';
import { LogViewConfiguration } from '../../../containers/logs/log_view_configuration';
import { LogHighlightsState } from '../../../containers/logs/log_highlights/log_highlights';
import { LogPositionState } from '../../../containers/logs/log_position';
import { LogFilterState, WithLogFilterUrlState } from '../../../containers/logs/log_filter';
import { LogFilterState } from '../../../containers/logs/log_filter';
import { LogEntriesState } from '../../../containers/logs/log_entries';

import { Source } from '../../../containers/source';
Expand All @@ -19,10 +19,7 @@ const LogFilterStateProvider: React.FC = ({ children }) => {
const { createDerivedIndexPattern } = useContext(Source.Context);
const derivedIndexPattern = createDerivedIndexPattern('logs');
return (
<LogFilterState.Provider indexPattern={derivedIndexPattern}>
<WithLogFilterUrlState />
{children}
</LogFilterState.Provider>
<LogFilterState.Provider indexPattern={derivedIndexPattern}>{children}</LogFilterState.Provider>
);
};

Expand Down
16 changes: 16 additions & 0 deletions x-pack/legacy/plugins/infra/public/utils/validate_url_rt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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 { fold } from 'fp-ts/lib/Either';
import { constant, identity } from 'fp-ts/lib/function';
import { pipe } from 'fp-ts/lib/pipeable';
import * as rt from 'io-ts';

export const getEncodeDecodeFromRT = (typeRT: rt.Type<any>) => ({
encodeUrlState: typeRT.encode,
decodeUrlState: (value: unknown) =>
pipe(typeRT.decode(value), fold(constant(undefined), identity)),
});

0 comments on commit c61f5b1

Please sign in to comment.