Skip to content

Commit

Permalink
use simpler types (elastic#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
orouz committed Jan 13, 2022
1 parent e8af6a5 commit 2f4fcd9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 42 deletions.
6 changes: 0 additions & 6 deletions x-pack/plugins/cloud_security_posture/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,3 @@ export interface CloudPostureStats extends BenchmarkStats {
benchmarksStats: BenchmarkStats[];
resourcesEvaluations: EvaluationStats[];
}

// This is needed when we want to pick some types without losing their relations to their union type (tagged unions)
// source: https://github.com/microsoft/TypeScript/issues/28339#issuecomment-463577347
export type DistributivePick<T, K extends keyof T> = T extends any
? Pick<T, Extract<keyof T, K>>
: never;
24 changes: 0 additions & 24 deletions x-pack/plugins/cloud_security_posture/public/common/types.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,36 @@ import { css } from '@emotion/react';
import { EuiSpacer } from '@elastic/eui';
import type { SearchResponse } from '@elastic/elasticsearch/lib/api/types';
import type { UseMutationResult } from 'react-query';
import type { Filter } from '@kbn/es-query';
import type { Filter, Query } from '@kbn/es-query';
import { FindingsTable } from './findings_table';
import { FindingsRuleFlyout } from './findings_flyout';
import { FindingsSearchBar } from './findings_search_bar';
import { TEST_SUBJECTS } from './constants';
import { useKibana } from '../../../../../../src/plugins/kibana_react/public';
import {
extractErrorMessage,
useSourceQueryParam,
useEsClientMutation,
isNonNullable,
} from './utils';
import type { CspFinding, FindingsFetchState } from './types';
import type { DataView, IKibanaSearchResponse } from '../../../../../../src/plugins/data/common';
import type { SearchBarProps } from '../../../../../../src/plugins/data/public';
import { useKibana } from '../../../../../../src/plugins/kibana_react/public';
import type {
DataView,
IKibanaSearchResponse,
TimeRange,
} from '../../../../../../src/plugins/data/common';

type FindingsEsSearchMutation = UseMutationResult<
IKibanaSearchResponse<SearchResponse<CspFinding>>,
unknown,
void
>;

export type URLState = Parameters<NonNullable<SearchBarProps['onQuerySubmit']>>[0] & {
export interface URLState {
dateRange: TimeRange;
query?: Query;
filters: Filter[];
};
}

const getDefaultQuery = (): Required<URLState> => ({
query: { language: 'kuery', query: '' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
* 2.0.
*/

import type { MutationFetchState } from '../../common/types';

export type FindingsFetchState = MutationFetchState<CspFinding[], string>;
export type FindingsFetchState =
| { status: 'idle'; error: null; data: undefined }
| { status: 'loading'; error: null; data: undefined }
| { status: 'success'; error: null; data: CspFinding[] }
| { status: 'error'; error: string; data: undefined };

export interface CspFinding {
'@timestamp': string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useState, useEffect } from 'react';
import type { Filter, Query } from '@kbn/es-query';
import { useMutation, useQuery } from 'react-query';
import type { SearchResponse } from '@elastic/elasticsearch/lib/api/types';
import { encode, decode, RisonValue } from 'rison-node';
import { encode, decode, RisonObject } from 'rison-node';
import { useHistory } from 'react-router-dom';
import type {
DataView,
Expand Down Expand Up @@ -52,11 +52,11 @@ export const useKubebeatDataView = () => {
return useQuery(['kubebeat_dataview'], getKubebeatDataView);
};

export const useSourceQueryParam = <T extends object>(getDefaultQuery: () => T) => {
export const useSourceQueryParam = <T extends RisonObject>(getDefaultQuery: () => T) => {
const history = useHistory();
const [state, set] = useState<T>(getDefaultQuery());

const setSource = (v: RisonValue) => {
const setSource = (v: T) => {
try {
const next = `source=${encode(v)}`;
const current = history.location.search.slice(1);
Expand Down

0 comments on commit 2f4fcd9

Please sign in to comment.