From 919e304619127a05c6fe949b6f97528cd5ef2d9a Mon Sep 17 00:00:00 2001 From: Pedro Jaramillo Date: Mon, 27 Jan 2020 11:24:22 -0500 Subject: [PATCH] addresses comments and uses better types --- x-pack/plugins/endpoint/endpoint_app_types.ts | 24 ++++++++++++++++--- .../endpoint/store/alerts/types.ts | 6 ++--- .../endpoint/store/endpoint_list/reducer.ts | 1 - .../endpoint/view/alerts/index.tsx | 3 +-- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/x-pack/plugins/endpoint/endpoint_app_types.ts b/x-pack/plugins/endpoint/endpoint_app_types.ts index eeae0ea5b15f2..ce4f8349bcf0f 100644 --- a/x-pack/plugins/endpoint/endpoint_app_types.ts +++ b/x-pack/plugins/endpoint/endpoint_app_types.ts @@ -4,7 +4,25 @@ * you may not use this file except in compliance with the Elastic License. */ -export interface AlertData { +/** + * A deep readonly type that will make all children of a given object readonly recursively + */ +export type Immutable = T extends undefined | null | boolean | string | number + ? T + : T extends Array + ? ImmutableArray + : T extends Map + ? ImmutableMap + : T extends Set + ? ImmutableSet + : ImmutableObject; + +export type ImmutableArray = ReadonlyArray>; +export type ImmutableMap = ReadonlyMap, Immutable>; +export type ImmutableSet = ReadonlySet>; +export type ImmutableObject = { readonly [K in keyof T]: Immutable }; + +export type AlertData = Immutable<{ value: { source: { endgame: { @@ -26,11 +44,11 @@ export interface AlertData { hostname: string; ip: string; os: { - name: string; // TODO Union types? + name: string; }; }; }; }; -} +}>; export type PageId = 'alertsPage' | 'endpointListPage'; diff --git a/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/types.ts b/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/types.ts index d08795d512e20..4a336caab276b 100644 --- a/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/types.ts +++ b/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/types.ts @@ -4,8 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -import { AlertData } from '../../../../../endpoint_app_types'; +import { AlertData, Immutable } from '../../../../../endpoint_app_types'; -export interface AlertListState { +export type AlertListState = Immutable<{ alerts: AlertData[]; -} +}>; diff --git a/x-pack/plugins/endpoint/public/applications/endpoint/store/endpoint_list/reducer.ts b/x-pack/plugins/endpoint/public/applications/endpoint/store/endpoint_list/reducer.ts index 241c6f69ec6b7..e57d9683e4707 100644 --- a/x-pack/plugins/endpoint/public/applications/endpoint/store/endpoint_list/reducer.ts +++ b/x-pack/plugins/endpoint/public/applications/endpoint/store/endpoint_list/reducer.ts @@ -6,7 +6,6 @@ import { Reducer } from 'redux'; import { EndpointListState } from './types'; -import { EndpointListAction } from './action'; import { AppAction } from '../action'; const initialState = (): EndpointListState => { diff --git a/x-pack/plugins/endpoint/public/applications/endpoint/view/alerts/index.tsx b/x-pack/plugins/endpoint/public/applications/endpoint/view/alerts/index.tsx index f33cf718ea9c0..17d2e562c61cb 100644 --- a/x-pack/plugins/endpoint/public/applications/endpoint/view/alerts/index.tsx +++ b/x-pack/plugins/endpoint/public/applications/endpoint/view/alerts/index.tsx @@ -7,8 +7,7 @@ import { memo, useState, useMemo } from 'react'; import React from 'react'; import { EuiDataGrid } from '@elastic/eui'; -import { useDispatch, useSelector } from 'react-redux'; -import { AlertAction } from '../../store/alerts/action'; +import { useSelector } from 'react-redux'; import * as selectors from '../../store/selectors'; import { usePageId } from '../use_page_id';