Skip to content

Commit

Permalink
Attempt to fix state
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal committed Oct 11, 2021
1 parent 6144b49 commit 4a97aec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
DataPublicPluginStart,
esFilters,
Filter,
FilterManager,
IndexPattern,
Query,
SearchSessionInfoProvider,
Expand Down Expand Up @@ -107,11 +106,7 @@ export interface GetStateReturn {
/**
* Initialize state with filters and query, start state syncing
*/
initializeAndSync: (
savedSearch: SavedSearch,
filterManager: FilterManager,
data: DataPublicPluginStart
) => () => void;
initializeAndSync: (savedSearch: SavedSearch) => () => void;
/**
* Start sync between state and URL
*/
Expand Down Expand Up @@ -158,8 +153,7 @@ const APP_STATE_URL_KEY = '_a';
* Used to sync URL with UI state
*/
export function getState({ history, services }: GetStateParams): GetStateReturn {
const { uiSettings } = services;
const { data, uiSettings: config } = services;
const { uiSettings, filterManager, data, uiSettings: config } = services;
const storeInSessionStorage = uiSettings.get('state:storeInSessionStorage');
const toasts = services.core.notifications.toasts;
const defaultAppState = {};
Expand Down Expand Up @@ -240,21 +234,25 @@ export function getState({ history, services }: GetStateParams): GetStateReturn
getPreviousAppState: () => previousAppState,
flushToUrl: () => stateStorage.kbnUrlControls.flush(),
isAppStateDirty: () => !isEqualState(initialAppState, appStateContainer.getState()),
initializeAndSync: (savedSearch: SavedSearch, filterManager: FilterManager) => {
initializeAndSync: (savedSearch: SavedSearch) => {
const indexPattern = savedSearch.searchSource.getField('index')!;
const appState = getStateDefaults({
config,
data,
savedSearch,
});
const mergedState = handleSourceColumnState(
{
...appState,
...appStateFromUrl,
},
uiSettings
);
setState(appStateContainerModified, mergedState);
if (savedSearch.id) {
setState(appStateContainerModified, appState);
} else {
const mergedState = handleSourceColumnState(
{
...appState,
...appStateFromUrl,
},
uiSettings
);
setState(appStateContainerModified, mergedState);
}

if (appStateContainer.getState().index !== indexPattern.id) {
// used index pattern is different than the given by url/state which is invalid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function useDiscoverState({
savedSearch: SavedSearch;
history: History;
}) {
const { uiSettings: config, data, filterManager } = services;
const { uiSettings: config, data } = services;
const useNewFieldsApi = useMemo(() => !config.get(SEARCH_FIELDS_FROM_SOURCE), [config]);
const { timefilter } = data.query.timefilter;
const [indexPattern, setIndexPattern] = useState(savedSearch.searchSource.getField('index'));
Expand All @@ -39,6 +39,7 @@ export function useDiscoverState({
const searchSource = useMemo(() => savedSearch.searchSource.createChild(), [savedSearch]);

const stateContainer = useMemo(() => getState({ history, services }), [services, history]);
const [state, setState] = useState(stateContainer.appStateContainer.getState());

const { appStateContainer } = stateContainer;

Expand All @@ -59,8 +60,6 @@ export function useDiscoverState({
load();
}, [appStateContainer, get, savedSearch.searchSource, services.uiSettings]);

const [state, setState] = useState(appStateContainer.getState());

/**
* Search session logic
*/
Expand Down Expand Up @@ -94,10 +93,15 @@ export function useDiscoverState({
*/
useEffect(() => {
if (savedSearch.searchSource.getField('index')) {
const stopSync = stateContainer.initializeAndSync(savedSearch, filterManager, data);
const prevState = stateContainer.appStateContainer.getState();
const stopSync = stateContainer.initializeAndSync(savedSearch);
const nextState = stateContainer.appStateContainer.getState();
if (!isEqual(prevState, nextState)) {
setState(nextState);
}
return () => stopSync();
}
}, [stateContainer, filterManager, data, config, savedSearch]);
}, [stateContainer, savedSearch, setState, indexPattern]);

/**
* Track state changes that should trigger a fetch
Expand Down

0 comments on commit 4a97aec

Please sign in to comment.