From 031759a497f39995bdd5e2774e8d12299e4cddef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20St=C3=BCrmer?= Date: Fri, 14 Dec 2018 14:01:57 +0100 Subject: [PATCH] [6.x] Remove usage of ts-optchain in the browser (#27148) (#27193) Backports the following commits to 6.x: - Remove usage of ts-optchain in the browser (#27148) --- .../containers/with_source/with_source.tsx | 30 ++++++++++++++----- .../infra/public/utils/operation_status.ts | 24 ++++++++++----- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/x-pack/plugins/infra/public/containers/with_source/with_source.tsx b/x-pack/plugins/infra/public/containers/with_source/with_source.tsx index 37fd19dbcda1..4bce104f63fd 100644 --- a/x-pack/plugins/infra/public/containers/with_source/with_source.tsx +++ b/x-pack/plugins/infra/public/containers/with_source/with_source.tsx @@ -9,7 +9,6 @@ import { Container as ConstateContainer, OnMount } from 'constate'; import React from 'react'; import { ApolloConsumer } from 'react-apollo'; import { createSelector } from 'reselect'; -import { oc } from 'ts-optchain'; import { StaticIndexPattern } from 'ui/index_patterns'; import { memoizeLast } from 'ui/utils/memoize'; @@ -46,9 +45,20 @@ const createContainerProps = memoizeLast((sourceId: string, apolloClient: Apollo }); const getDerivedIndexPattern = createSelector( - (state: State) => oc(state).source.status.indexFields([]), - (state: State) => oc(state).source.configuration.logAlias(), - (state: State) => oc(state).source.configuration.metricAlias(), + (state: State) => + (state && state.source && state.source.status && state.source.status.indexFields) || [], + (state: State) => + (state && + state.source && + state.source.configuration && + state.source.configuration.logAlias) || + undefined, + (state: State) => + (state && + state.source && + state.source.configuration && + state.source.configuration.metricAlias) || + undefined, (indexFields, logAlias, metricAlias) => ({ fields: indexFields, title: `${logAlias},${metricAlias}`, @@ -57,9 +67,15 @@ const createContainerProps = memoizeLast((sourceId: string, apolloClient: Apollo const selectors = inferSelectorMap()({ ...createStatusSelectors(({ operationStatusHistory }: State) => operationStatusHistory), - getConfiguredFields: () => state => oc(state).source.configuration.fields(), - getLogIndicesExist: () => state => oc(state).source.status.logIndicesExist(), - getMetricIndicesExist: () => state => oc(state).source.status.metricIndicesExist(), + getConfiguredFields: () => state => + (state && state.source && state.source.configuration && state.source.configuration.fields) || + undefined, + getLogIndicesExist: () => state => + (state && state.source && state.source.status && state.source.status.logIndicesExist) || + undefined, + getMetricIndicesExist: () => state => + (state && state.source && state.source.status && state.source.status.metricIndicesExist) || + undefined, getDerivedIndexPattern: () => getDerivedIndexPattern, }); diff --git a/x-pack/plugins/infra/public/utils/operation_status.ts b/x-pack/plugins/infra/public/utils/operation_status.ts index 0d4fe4623e3f..922323440cab 100644 --- a/x-pack/plugins/infra/public/utils/operation_status.ts +++ b/x-pack/plugins/infra/public/utils/operation_status.ts @@ -5,7 +5,6 @@ */ import last from 'lodash/fp/last'; -import { oc } from 'ts-optchain'; export interface InProgressStatus> { operation: O; @@ -43,13 +42,22 @@ export interface Operation { export const createStatusSelectors = ( selectStatusHistory: (state: S) => Array> ) => ({ - getIsInProgress: () => (state: S) => - oc(last(selectStatusHistory(state))).status() === 'in-progress', - getHasSucceeded: () => (state: S) => - oc(last(selectStatusHistory(state))).status() === 'succeeded', - getHasFailed: () => (state: S) => oc(last(selectStatusHistory(state))).status() === 'failed', - getLastFailureMessage: () => (state: S) => - oc(last(selectStatusHistory(state).filter(isFailedStatus))).message(), + getIsInProgress: () => (state: S) => { + const lastStatus = last(selectStatusHistory(state)); + return lastStatus ? lastStatus.status === 'in-progress' : false; + }, + getHasSucceeded: () => (state: S) => { + const lastStatus = last(selectStatusHistory(state)); + return lastStatus ? lastStatus.status === 'succeeded' : false; + }, + getHasFailed: () => (state: S) => { + const lastStatus = last(selectStatusHistory(state)); + return lastStatus ? lastStatus.status === 'failed' : false; + }, + getLastFailureMessage: () => (state: S) => { + const lastStatus = last(selectStatusHistory(state).filter(isFailedStatus)); + return lastStatus ? lastStatus.message : undefined; + }, }); export type StatusHistoryUpdater> = (