-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Ingest Manager][Endpoint] Add Endpoint Create Policy flow with Ingest (
#68955) * Ingest: add data-test-subj prop support to Ingest components * Ingest: Add Context Provider to bridge History from Kibana to Hash router * Ingest: Added support for route state in Create Datasource page * Endpoint: Add Create button to Polices List header * Endpoint: Added support for passing of state from endpoint to ingest on policy * Endpoint: additional functional test cases
- Loading branch information
1 parent
e04624a
commit abdc0f1
Showing
21 changed files
with
646 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...k/plugins/ingest_manager/public/applications/ingest_manager/hooks/use_intra_app_state.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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, { memo, useContext, useMemo } from 'react'; | ||
import { AppMountParameters } from 'kibana/public'; | ||
import { useLocation } from 'react-router-dom'; | ||
import { AnyIntraAppRouteState } from '../types'; | ||
|
||
interface IntraAppState<S extends AnyIntraAppRouteState = AnyIntraAppRouteState> { | ||
forRoute: string; | ||
routeState?: S; | ||
} | ||
|
||
const IntraAppStateContext = React.createContext<IntraAppState>({ forRoute: '' }); | ||
const wasHandled = new WeakSet<IntraAppState>(); | ||
|
||
/** | ||
* Provides a bridget between Kibana's ScopedHistory instance (normally used with BrowserRouter) | ||
* and the Hash router used within the app in order to enable state to be used between kibana | ||
* apps | ||
*/ | ||
export const IntraAppStateProvider = memo<{ | ||
kibanaScopedHistory: AppMountParameters['history']; | ||
children: React.ReactNode; | ||
}>(({ kibanaScopedHistory, children }) => { | ||
const internalAppToAppState = useMemo<IntraAppState>(() => { | ||
return { | ||
forRoute: kibanaScopedHistory.location.hash.substr(1), | ||
routeState: kibanaScopedHistory.location.state as AnyIntraAppRouteState, | ||
}; | ||
}, [kibanaScopedHistory.location.hash, kibanaScopedHistory.location.state]); | ||
return ( | ||
<IntraAppStateContext.Provider value={internalAppToAppState}> | ||
{children} | ||
</IntraAppStateContext.Provider> | ||
); | ||
}); | ||
|
||
/** | ||
* Retrieve UI Route state from the React Router History for the current URL location. | ||
* This state can be used by other Kibana Apps to influence certain behaviours in Ingest, for example, | ||
* redirecting back to an given Application after a craete action. | ||
*/ | ||
export function useIntraAppState<S = AnyIntraAppRouteState>(): | ||
| IntraAppState<S>['routeState'] | ||
| undefined { | ||
const location = useLocation(); | ||
const intraAppState = useContext(IntraAppStateContext); | ||
if (!intraAppState) { | ||
throw new Error('Hook called outside of IntraAppStateContext'); | ||
} | ||
return useMemo(() => { | ||
// Due to the use of HashRouter in Ingest, we only want state to be returned | ||
// once so that it does not impact navigation to the page from within the | ||
// ingest app. side affect is that the browser back button would not work | ||
// consistently either. | ||
if (location.pathname === intraAppState.forRoute && !wasHandled.has(intraAppState)) { | ||
wasHandled.add(intraAppState); | ||
return intraAppState.routeState as S; | ||
} | ||
}, [intraAppState, location.pathname]); | ||
} |
Oops, something went wrong.