-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[State Management] State syncing helpers for query service I #56128
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
Merged
Dosant
merged 44 commits into
elastic:master
from
Dosant:dev/experiment-with-state-setup
Feb 27, 2020
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
e16961d
wip
Dosant 8201cbd
wip
Dosant e855db8
improve example
Dosant 0ddbfe5
Merge branch 'master' of github.com:elastic/kibana into dev/experimen…
Dosant 56895b7
improve
Dosant 224841b
fix
Dosant 98703dc
improve
Dosant 606bfc4
Merge branch 'master' of github.com:elastic/kibana into dev/experimen…
Dosant bc07de4
merge
Dosant 8be16b3
top nav ts arg support
1c7160e
Merge branch 'newplatform/data/stateful-top-nav-ts' of github.com:liz…
Dosant 8b71f90
Merge branch 'newplatform/data/stateful-top-nav-ts' of github.com:liz…
Dosant 04fc4d5
wip
Dosant ac55f33
snapshots
Dosant ce59461
use-callback
Dosant f4fb6b8
wip
Dosant e656578
wip
Dosant 962f84f
wip
Dosant f0b04da
Merge branch 'dev/experiment-with-state-setup' of github.com:Dosant/k…
Dosant c70e8ac
Merge branch 'master' of github.com:elastic/kibana into dev/experimen…
Dosant 29516bd
wip
Dosant 72bdaf5
Merge branch 'master' of github.com:elastic/kibana into dev/experimen…
Dosant 9b70221
fix
Dosant 7be0613
jest -u
Dosant 5bfd1e9
Merge branch 'master' of github.com:elastic/kibana into dev/experimen…
Dosant 19770c2
improve discover and visualise sub url tracking
Dosant 63f7c8c
update np karma mock
Dosant 3e57279
fix mock
Dosant d39d491
wip
Dosant 1f2c5d1
Merge branch 'master' of github.com:elastic/kibana into dev/experimen…
Dosant 36f3036
Merge branch 'master' of github.com:elastic/kibana into dev/experimen…
Dosant 3add348
improve
Dosant d376122
jset -u
Dosant 1b53f05
Merge branch 'master' of github.com:elastic/kibana into dev/experimen…
Dosant 3f24114
@lizozom review
Dosant cc1aaff
Merge branch 'master' into dev/experiment-with-state-setup
elasticmachine fa61db9
Merge branch 'master' into dev/experiment-with-state-setup
elasticmachine 15bd129
fix query state in create search bar
c816281
removed useCallback
83e16c0
Enabled saved queries
5b96209
Merge branch 'master' into dev/experiment-with-state-setup
elasticmachine e3513e0
fix setting saved search
Dosant a08ec7e
Merge branch 'master' into dev/experiment-with-state-setup
elasticmachine cc91afb
jest -u
Dosant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,21 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| export const PLUGIN_ID = 'stateContainersExampleWithDataServices'; | ||
| export const PLUGIN_NAME = 'State containers example - with data services'; |
This file contains hidden or 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
This file contains hidden or 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
File renamed without changes.
This file contains hidden or 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
49 changes: 49 additions & 0 deletions
49
examples/state_containers_examples/public/with_data_services/application.tsx
This file contains hidden or 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,49 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import React from 'react'; | ||
| import ReactDOM from 'react-dom'; | ||
| import { createBrowserHistory } from 'history'; | ||
| import { AppMountParameters, CoreStart } from '../../../../src/core/public'; | ||
| import { AppPluginDependencies } from './types'; | ||
| import { StateDemoApp } from './components/app'; | ||
| import { createKbnUrlStateStorage } from '../../../../src/plugins/kibana_utils/public/'; | ||
|
|
||
| export const renderApp = ( | ||
| { notifications, http }: CoreStart, | ||
| { navigation, data }: AppPluginDependencies, | ||
| { appBasePath, element }: AppMountParameters | ||
| ) => { | ||
| const history = createBrowserHistory({ basename: appBasePath }); | ||
| const kbnUrlStateStorage = createKbnUrlStateStorage({ useHash: false, history }); | ||
|
|
||
| ReactDOM.render( | ||
| <StateDemoApp | ||
| notifications={notifications} | ||
| http={http} | ||
| navigation={navigation} | ||
| data={data} | ||
| history={history} | ||
| kbnUrlStateStorage={kbnUrlStateStorage} | ||
| />, | ||
| element | ||
| ); | ||
|
|
||
| return () => ReactDOM.unmountComponentAtNode(element); | ||
| }; |
243 changes: 243 additions & 0 deletions
243
examples/state_containers_examples/public/with_data_services/components/app.tsx
This file contains hidden or 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,243 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import React, { useEffect, useRef, useState, useCallback } from 'react'; | ||
| import { History } from 'history'; | ||
| import { FormattedMessage, I18nProvider } from '@kbn/i18n/react'; | ||
| import { Router } from 'react-router-dom'; | ||
|
|
||
| import { | ||
| EuiFieldText, | ||
| EuiPage, | ||
| EuiPageBody, | ||
| EuiPageContent, | ||
| EuiPageHeader, | ||
| EuiTitle, | ||
| } from '@elastic/eui'; | ||
|
|
||
| import { CoreStart } from '../../../../../src/core/public'; | ||
| import { NavigationPublicPluginStart } from '../../../../../src/plugins/navigation/public'; | ||
| import { | ||
| connectToQueryState, | ||
| syncQueryStateWithUrl, | ||
| DataPublicPluginStart, | ||
| IIndexPattern, | ||
| QueryState, | ||
| Filter, | ||
| esFilters, | ||
| Query, | ||
| } from '../../../../../src/plugins/data/public'; | ||
| import { | ||
| BaseState, | ||
| BaseStateContainer, | ||
| createStateContainer, | ||
| createStateContainerReactHelpers, | ||
| IKbnUrlStateStorage, | ||
| ReduxLikeStateContainer, | ||
| syncState, | ||
| } from '../../../../../src/plugins/kibana_utils/public'; | ||
| import { PLUGIN_ID, PLUGIN_NAME } from '../../../common'; | ||
|
|
||
| interface StateDemoAppDeps { | ||
| notifications: CoreStart['notifications']; | ||
| http: CoreStart['http']; | ||
| navigation: NavigationPublicPluginStart; | ||
| data: DataPublicPluginStart; | ||
| history: History; | ||
| kbnUrlStateStorage: IKbnUrlStateStorage; | ||
| } | ||
|
|
||
| interface AppState { | ||
| name: string; | ||
| filters: Filter[]; | ||
| query?: Query; | ||
| } | ||
| const defaultAppState: AppState = { | ||
| name: '', | ||
| filters: [], | ||
| }; | ||
| const { | ||
| Provider: AppStateContainerProvider, | ||
| useState: useAppState, | ||
| useContainer: useAppStateContainer, | ||
| } = createStateContainerReactHelpers<ReduxLikeStateContainer<AppState>>(); | ||
|
|
||
| const App = ({ | ||
| notifications, | ||
| http, | ||
| navigation, | ||
| data, | ||
| history, | ||
| kbnUrlStateStorage, | ||
| }: StateDemoAppDeps) => { | ||
| const appStateContainer = useAppStateContainer(); | ||
| const appState = useAppState(); | ||
|
|
||
| useGlobalStateSyncing(data.query, kbnUrlStateStorage); | ||
| useAppStateSyncing(appStateContainer, data.query, kbnUrlStateStorage); | ||
|
|
||
| const onQuerySubmit = useCallback( | ||
| ({ query }) => { | ||
| appStateContainer.set({ ...appState, query }); | ||
| }, | ||
| [appStateContainer, appState] | ||
| ); | ||
|
|
||
| const indexPattern = useIndexPattern(data); | ||
| if (!indexPattern) | ||
| return <div>No index pattern found. Please create an intex patter before loading...</div>; | ||
|
|
||
| // Render the application DOM. | ||
| // Note that `navigation.ui.TopNavMenu` is a stateful component exported on the `navigation` plugin's start contract. | ||
| return ( | ||
| <Router history={history}> | ||
| <I18nProvider> | ||
| <> | ||
| <navigation.ui.TopNavMenu | ||
| appName={PLUGIN_ID} | ||
| showSearchBar={true} | ||
| indexPatterns={[indexPattern]} | ||
| useDefaultBehaviors={true} | ||
| onQuerySubmit={onQuerySubmit} | ||
| query={appState.query} | ||
| showSaveQuery={true} | ||
| /> | ||
| <EuiPage restrictWidth="1000px"> | ||
| <EuiPageBody> | ||
| <EuiPageHeader> | ||
| <EuiTitle size="l"> | ||
| <h1> | ||
| <FormattedMessage | ||
| id="stateDemo.helloWorldText" | ||
| defaultMessage="{name}!" | ||
| values={{ name: PLUGIN_NAME }} | ||
| /> | ||
| </h1> | ||
| </EuiTitle> | ||
| </EuiPageHeader> | ||
| <EuiPageContent> | ||
| <EuiFieldText | ||
Dosant marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| placeholder="Additional application state: My name is..." | ||
| value={appState.name} | ||
| onChange={e => appStateContainer.set({ ...appState, name: e.target.value })} | ||
| aria-label="My name" | ||
| /> | ||
| </EuiPageContent> | ||
| </EuiPageBody> | ||
| </EuiPage> | ||
| </> | ||
| </I18nProvider> | ||
| </Router> | ||
| ); | ||
| }; | ||
|
|
||
| export const StateDemoApp = (props: StateDemoAppDeps) => { | ||
| const appStateContainer = useCreateStateContainer(defaultAppState); | ||
|
|
||
| return ( | ||
| <AppStateContainerProvider value={appStateContainer}> | ||
| <App {...props} /> | ||
| </AppStateContainerProvider> | ||
| ); | ||
| }; | ||
|
|
||
| function useCreateStateContainer<State extends BaseState>( | ||
| defaultState: State | ||
| ): ReduxLikeStateContainer<State> { | ||
| const stateContainerRef = useRef<ReduxLikeStateContainer<State> | null>(null); | ||
| if (!stateContainerRef.current) { | ||
| stateContainerRef.current = createStateContainer(defaultState); | ||
| } | ||
| return stateContainerRef.current; | ||
| } | ||
|
|
||
| function useIndexPattern(data: DataPublicPluginStart) { | ||
| const [indexPattern, setIndexPattern] = useState<IIndexPattern>(); | ||
| useEffect(() => { | ||
| const fetchIndexPattern = async () => { | ||
| const defaultIndexPattern = await data.indexPatterns.getDefault(); | ||
| if (defaultIndexPattern) { | ||
| setIndexPattern(defaultIndexPattern); | ||
| } | ||
| }; | ||
| fetchIndexPattern(); | ||
| }, [data.indexPatterns]); | ||
|
|
||
| return indexPattern; | ||
| } | ||
|
|
||
| function useGlobalStateSyncing( | ||
| query: DataPublicPluginStart['query'], | ||
| kbnUrlStateStorage: IKbnUrlStateStorage | ||
| ) { | ||
| // setup sync state utils | ||
| useEffect(() => { | ||
| // sync global filters, time filters, refresh interval from data.query to url '_g' | ||
| const { stop } = syncQueryStateWithUrl(query, kbnUrlStateStorage); | ||
| return () => { | ||
| stop(); | ||
| }; | ||
| }, [query, kbnUrlStateStorage]); | ||
| } | ||
|
|
||
| function useAppStateSyncing<AppState extends QueryState>( | ||
| appStateContainer: BaseStateContainer<AppState>, | ||
| query: DataPublicPluginStart['query'], | ||
| kbnUrlStateStorage: IKbnUrlStateStorage | ||
| ) { | ||
| // setup sync state utils | ||
| useEffect(() => { | ||
| // sync app filters with app state container from data.query to state container | ||
| const stopSyncingQueryAppStateWithStateContainer = connectToQueryState( | ||
| query, | ||
| appStateContainer, | ||
| { filters: esFilters.FilterStateStore.APP_STATE } | ||
| ); | ||
|
|
||
| // sets up syncing app state container with url | ||
| const { start: startSyncingAppStateWithUrl, stop: stopSyncingAppStateWithUrl } = syncState({ | ||
| storageKey: '_a', | ||
| stateStorage: kbnUrlStateStorage, | ||
| stateContainer: { | ||
| ...appStateContainer, | ||
| // stateSync utils requires explicit handling of default state ("null") | ||
| set: state => state && appStateContainer.set(state), | ||
| }, | ||
| }); | ||
|
|
||
| // merge initial state from app state container and current state in url | ||
| const initialAppState: AppState = { | ||
| ...appStateContainer.get(), | ||
| ...kbnUrlStateStorage.get<AppState>('_a'), | ||
| }; | ||
| // trigger state update. actually needed in case some data was in url | ||
| appStateContainer.set(initialAppState); | ||
|
|
||
| // set current url to whatever is in app state container | ||
| kbnUrlStateStorage.set<AppState>('_a', initialAppState); | ||
|
|
||
| // finally start syncing state containers with url | ||
| startSyncingAppStateWithUrl(); | ||
|
|
||
| return () => { | ||
| stopSyncingQueryAppStateWithStateContainer(); | ||
| stopSyncingAppStateWithUrl(); | ||
| }; | ||
| }, [query, kbnUrlStateStorage, appStateContainer]); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.