-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Uptime] Feature/refactor context initialization #54494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
24939eb
b3c517c
b9f9432
0b2661b
70e1760
1eedfc0
aa992cb
68d57ac
99fc6f1
e3bdfa2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
||
| 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} />; | ||
| }; |
There was a problem hiding this comment.
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.tsof the contexts directory, so we can remove the filename from the import.