Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import { UptimeDatePicker } from '../uptime_date_picker';

describe('UptimeDatePicker component', () => {
it('validates props with shallow render', () => {
const component = shallowWithIntl(<UptimeDatePicker refreshApp={jest.fn()} />);
const component = shallowWithIntl(<UptimeDatePicker />);
expect(component).toMatchSnapshot();
});

it('renders properly with mock data', () => {
const component = renderWithIntl(<UptimeDatePicker refreshApp={jest.fn()} />);
const component = renderWithIntl(<UptimeDatePicker />);
expect(component).toMatchSnapshot();
});

it('renders properly without commonlyUsedRanges prop', () => {
const component = renderWithIntl(<UptimeDatePicker refreshApp={jest.fn()} />);
const component = renderWithIntl(<UptimeDatePicker />);
expect(component).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React, { useContext, useEffect, useRef } from 'react';
import * as d3 from 'd3';
import { i18n } from '@kbn/i18n';
import { DonutChartLegend } from './donut_chart_legend';
import { UptimeSettingsContext } from '../../../contexts';
import { UptimeThemeContext } from '../../../contexts/uptime_theme_context';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're exporting this from the index.ts of the contexts directory, so we can remove the filename from the import.


interface DonutChartProps {
down: number;
Expand All @@ -23,7 +23,7 @@ export const DonutChart = ({ height, down, up, width }: DonutChartProps) => {

const {
colors: { danger, gray },
} = useContext(UptimeSettingsContext);
} = useContext(UptimeThemeContext);

let upCount = up;
if (up === 0 && down === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { EuiSpacer } from '@elastic/eui';
import React, { useContext } from 'react';
import styled from 'styled-components';
import { DonutChartLegendRow } from './donut_chart_legend_row';
import { UptimeSettingsContext } from '../../../contexts';
import { UptimeThemeContext } from '../../../contexts';

const LegendContainer = styled.div`
max-width: 260px;
Expand All @@ -28,7 +28,7 @@ interface Props {
export const DonutChartLegend = ({ down, up }: Props) => {
const {
colors: { gray, danger },
} = useContext(UptimeSettingsContext);
} = useContext(UptimeThemeContext);
return (
<LegendContainer>
<DonutChartLegendRow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { getChartDateLabel } from '../../../lib/helper';
import { withUptimeGraphQL, UptimeGraphQLQueryProps } from '../../higher_order';
import { snapshotHistogramQuery } from '../../../queries/snapshot_histogram_query';
import { ChartWrapper } from './chart_wrapper';
import { UptimeSettingsContext } from '../../../contexts';
import { UptimeThemeContext } from '../../../contexts';
import { ResponsiveWrapperProps, withResponsiveWrapper } from '../../higher_order';
import { HistogramResult } from '../../../../common/domain_types';

Expand Down Expand Up @@ -53,7 +53,7 @@ export const SnapshotHistogramComponent: React.FC<Props> = ({
}: Props) => {
const {
colors: { danger, gray },
} = useContext(UptimeSettingsContext);
} = useContext(UptimeThemeContext);
if (!data || !data.queryResult)
/**
* TODO: the Fragment, EuiTitle, and EuiPanel should be extracted to a dumb component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { MAP_SAVED_OBJECT_TYPE } from '../../../../../../maps/common/constants';

import { MapEmbeddable } from './types';
import { getLayerList } from './map_config';
import { UptimeSettingsContext } from '../../../../contexts';
import { UptimeThemeContext } from '../../../../contexts';

export interface EmbeddedMapProps {
upPoints: LocationPoint[];
Expand Down Expand Up @@ -46,7 +46,7 @@ const EmbeddedPanel = styled.div`
`;

export const EmbeddedMap = ({ upPoints, downPoints }: EmbeddedMapProps) => {
const { colors } = useContext(UptimeSettingsContext);
const { colors } = useContext(UptimeThemeContext);
const [embeddable, setEmbeddable] = useState<MapEmbeddable>();
const embeddableRoot: React.RefObject<HTMLDivElement> = useRef<HTMLDivElement>(null);
const factory = start.getEmbeddableFactory(MAP_SAVED_OBJECT_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import React, { useContext } from 'react';
import styled from 'styled-components';
import { EuiBadge, EuiText } from '@elastic/eui';
import { UptimeSettingsContext } from '../../../contexts';
import { UptimeThemeContext } from '../../../contexts';
import { MonitorLocation } from '../../../../common/runtime_types';

const TextStyle = styled.div`
Expand All @@ -31,7 +31,7 @@ interface Props {
export const LocationStatusTags = ({ locations }: Props) => {
const {
colors: { gray, danger },
} = useContext(UptimeSettingsContext);
} = useContext(UptimeThemeContext);

const upLocs: string[] = [];
const downLocs: string[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import React, { useContext } from 'react';
import { EuiHealth, EuiSpacer } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';
import { UptimeSettingsContext } from '../../../../contexts';
import { UptimeThemeContext } from '../../../../contexts';
import { UNNAMED_LOCATION, UP } from './monitor_status_list';

interface MonitorStatusRowProps {
Expand All @@ -24,7 +24,7 @@ interface MonitorStatusRowProps {
export const MonitorStatusRow = ({ locationNames, status }: MonitorStatusRowProps) => {
const {
colors: { success, danger },
} = useContext(UptimeSettingsContext);
} = useContext(UptimeThemeContext);

const color = status === UP ? success : danger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { EuiSuperDatePicker } from '@elastic/eui';
import React, { useContext } from 'react';
import { useUrlParams } from '../../hooks';
import { CLIENT_DEFAULTS } from '../../../common/constants';
import { UptimeSettingsContext } from '../../contexts';
import { UptimeRefreshContext, UptimeSettingsContext } from '../../contexts';

// TODO: when EUI exports types for this, this should be replaced
interface SuperDateRangePickerRangeChangedEvent {
Expand All @@ -27,14 +27,11 @@ export interface CommonlyUsedRange {
display: string;
}

interface UptimeDatePickerProps {
refreshApp: () => void;
}

export const UptimeDatePicker = ({ refreshApp }: UptimeDatePickerProps) => {
export const UptimeDatePicker = () => {
const [getUrlParams, updateUrl] = useUrlParams();
const { autorefreshInterval, autorefreshIsPaused, dateRangeStart, dateRangeEnd } = getUrlParams();
const { commonlyUsedRanges } = useContext(UptimeSettingsContext);
const { refreshApp } = useContext(UptimeRefreshContext);

const euiCommonlyUsedRanges = commonlyUsedRanges
? commonlyUsedRanges.map(
Expand Down
9 changes: 7 additions & 2 deletions x-pack/legacy/plugins/uptime/public/contexts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { UptimeRefreshContext } from './uptime_refresh_context';
export { UMSettingsContextValues, UptimeSettingsContext } from './uptime_settings_context';
export { UptimeRefreshContext, UptimeRefreshContextProvider } from './uptime_refresh_context';
export {
UMSettingsContextValues,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably rename this UptimeSettingsContextValues.. I was prefixing lots of things with UM back in the early days but we refer to it internally and publicly as simply "Uptime" now. WDYT?

UptimeSettingsContext,
UptimeSettingsContextProvider,
} from './uptime_settings_context';
export { UptimeThemeContextProvider, UptimeThemeContext } from './uptime_theme_context';

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 React, { createContext, useMemo, useState } from 'react';
import { useHistory, useLocation } from 'react-router-dom';
import { store } from '../state';
import { triggerAppRefresh } from '../state/actions';

interface UMRefreshContext {
lastRefresh: number;
refreshApp: () => void;
}

const defaultContext: UMRefreshContext = {
lastRefresh: 0,
refreshApp: () => {
throw new Error('App refresh was not initialized, set it when you invoke the context');
},
};

export const UptimeRefreshContext = createContext(defaultContext);

export const UptimeRefreshContextProvider: React.FC = ({ children }) => {
const history = useHistory();
const location = useLocation();

const [lastRefresh, setLastRefresh] = useState<number>(Date.now());

const refreshApp = () => {
const refreshTime = Date.now();
setLastRefresh(refreshTime);
store.dispatch(triggerAppRefresh(refreshTime));
};

const value = useMemo(() => {
return { lastRefresh, history, location, refreshApp };
}, [history, location, lastRefresh]);

return <UptimeRefreshContext.Provider value={value} children={children} />;
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 React, { createContext, useMemo } from 'react';
import { useParams } from 'react-router-dom';
import { UptimeAppProps } from '../uptime_app';
import { CONTEXT_DEFAULTS } from '../../common/constants';
import { CommonlyUsedRange } from '../components/functional/uptime_date_picker';

export interface UMSettingsContextValues {
basePath: string;
dateRangeStart: string;
dateRangeEnd: string;
isApmAvailable: boolean;
isInfraAvailable: boolean;
isLogsAvailable: boolean;
commonlyUsedRanges?: CommonlyUsedRange[];
}

const { BASE_PATH, DATE_RANGE_START, DATE_RANGE_END } = CONTEXT_DEFAULTS;

/**
* These are default values for the context. These defaults are typically
* overwritten by the Uptime App upon its invocation.
*/
const defaultContext: UMSettingsContextValues = {
basePath: BASE_PATH,
dateRangeStart: DATE_RANGE_START,
dateRangeEnd: DATE_RANGE_END,
isApmAvailable: true,
isInfraAvailable: true,
isLogsAvailable: true,
};
export const UptimeSettingsContext = createContext(defaultContext);

export const UptimeSettingsContextProvider: React.FC<UptimeAppProps> = ({ children, ...props }) => {
const { basePath, isApmAvailable, isInfraAvailable, isLogsAvailable } = props;

const { dateRangeStart, dateRangeEnd } = useParams();

const value = useMemo(() => {
return {
basePath,
isApmAvailable,
isInfraAvailable,
isLogsAvailable,
dateRangeStart: dateRangeStart ?? DATE_RANGE_START,
dateRangeEnd: dateRangeEnd ?? DATE_RANGE_END,
};
}, [basePath, isApmAvailable, isInfraAvailable, isLogsAvailable, dateRangeStart, dateRangeEnd]);

return <UptimeSettingsContext.Provider value={value} children={children} />;
};
Loading