Skip to content

Commit

Permalink
Simplify destructuring signature
Browse files Browse the repository at this point in the history
  • Loading branch information
Zacqary committed Dec 19, 2019
1 parent c5e165f commit ef00491
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,38 @@ export interface LogFilterCallbacks {

export const useLogFilterState: (props: {
indexPattern: IIndexPattern;
}) => [LogFilterStateParams, LogFilterCallbacks] = ({ indexPattern }) => {
}) => LogFilterStateParams & LogFilterCallbacks = ({ indexPattern }) => {
const [state, setState] = useState(logFilterInitialState);
const { filterQuery, filterQueryDraft } = state;

const callbacks: LogFilterCallbacks = useMemo(() => {
const setLogFilterQueryDraft = (payload: KueryFilterQuery) =>
setState({ ...state, filterQueryDraft: payload });
const applyLogFilterQuery = (payload: SerializedFilterQuery) =>
setState({ ...state, filterQueryDraft: payload.query, filterQuery: payload });
return {
setLogFilterQueryDraft: expression =>
setLogFilterQueryDraft({
const setLogFilterQueryDraft = useMemo(() => {
const setDraft = (payload: KueryFilterQuery) =>
setState(prevState => ({ ...prevState, filterQueryDraft: payload }));
return (expression: string) =>
setDraft({
kind: 'kuery',
expression,
});
}, []);
const applyLogFilterQuery = useMemo(() => {
const applyQuery = (payload: SerializedFilterQuery) =>
setState(prevState => ({
...prevState,
filterQueryDraft: payload.query,
filterQuery: payload,
}));
return (expression: string) =>
applyQuery({
query: {
kind: 'kuery',
expression,
}),
applyLogFilterQuery: expression =>
applyLogFilterQuery({
query: {
kind: 'kuery',
expression,
},
serializedQuery: convertKueryToElasticSearchQuery(expression, indexPattern),
}),
};
}, [state, indexPattern]);
},
serializedQuery: convertKueryToElasticSearchQuery(expression, indexPattern),
});
}, [indexPattern]);

const isFilterQueryDraftValid = useMemo(() => {
if (filterQueryDraft && filterQueryDraft.kind === 'kuery') {
if (filterQueryDraft?.kind === 'kuery') {
try {
esKuery.fromKueryExpression(filterQueryDraft.expression);
} catch (err) {
Expand All @@ -84,15 +88,14 @@ export const useLogFilterState: (props: {
filterQuery,
]);

return [
{
...state,
filterQueryAsKuery: state.filterQuery ? state.filterQuery.query : null,
filterQuery: serializedFilterQuery,
isFilterQueryDraftValid,
},
callbacks,
];
return {
...state,
filterQueryAsKuery: state.filterQuery ? state.filterQuery.query : null,
filterQuery: serializedFilterQuery,
isFilterQueryDraftValid,
setLogFilterQueryDraft,
applyLogFilterQuery,
};
};

export const LogFilterState = createContainer(useLogFilterState);
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { replaceStateKeyInQueryString, UrlStateContainer } from '../../../utils/
type LogFilterUrlState = LogFilterStateParams['filterQueryAsKuery'];

export const WithLogFilterUrlState: React.FC = () => {
const [{ filterQueryAsKuery }, { applyLogFilterQuery }] = useContext(LogFilterState.Context);
const { filterQueryAsKuery, applyLogFilterQuery } = useContext(LogFilterState.Context);
return (
<UrlStateContainer
urlState={filterQueryAsKuery}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const WithSummary = connect((state: State) => ({
}) => {
const { intervalSize } = useContext(LogViewConfiguration.Context);
const { sourceId } = useContext(Source.Context);
const [{ filterQuery }] = useContext(LogFilterState.Context);
const { filterQuery } = useContext(LogFilterState.Context);

const { buckets, start, end } = useLogSummary(
sourceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const LogsPageLogsContent: React.FunctionComponent = () => {
isLoading,
} = useContext(LogFlyoutState.Context);
const { logSummaryHighlights } = useContext(LogHighlightsState.Context);
const [, { applyLogFilterQuery }] = useContext(LogFilterState.Context);
const { applyLogFilterQuery } = useContext(LogFilterState.Context);
return (
<>
<LogHighlightsBridge />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const LogEntriesStateProvider: React.FC = ({ children }) => {
const { timeKey, pagesBeforeStart, pagesAfterEnd, isAutoReloading } = useContext(
LogPositionState.Context
);
const [{ filterQuery }] = useContext(LogFilterState.Context);
const { filterQuery } = useContext(LogFilterState.Context);
const entriesProps = {
timeKey,
pagesBeforeStart,
Expand All @@ -46,7 +46,7 @@ const LogEntriesStateProvider: React.FC = ({ children }) => {
const LogHighlightsStateProvider: React.FC = ({ children }) => {
const { sourceId, version } = useContext(Source.Context);
const [{ entriesStart, entriesEnd }] = useContext(LogEntriesState.Context);
const [{ filterQuery }] = useContext(LogFilterState.Context);
const { filterQuery } = useContext(LogFilterState.Context);
const highlightsProps = {
sourceId,
sourceVersion: version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ export const LogsToolbar = () => {
textScale,
textWrap,
} = useContext(LogViewConfiguration.Context);
const [
{ filterQueryDraft, isFilterQueryDraftValid },
{ applyLogFilterQuery, setLogFilterQueryDraft },
] = useContext(LogFilterState.Context);
const {
filterQueryDraft,
isFilterQueryDraftValid,
applyLogFilterQuery,
setLogFilterQueryDraft,
} = useContext(LogFilterState.Context);
const { setSurroundingLogsId } = useContext(LogFlyout.Context);

const {
Expand Down

0 comments on commit ef00491

Please sign in to comment.