mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Nested Folders: Do not keep search state when navigating to a result item (#67749)
* Reset search state when navigating to a new search item * Reset query on search select * Spread initialState * Remove unused import * Only reset query * Add test * Fix spelling * Change functionality * Only applied saved filters when searching
This commit is contained in:
@@ -3,6 +3,7 @@ import { locationService } from '@grafana/runtime';
|
|||||||
|
|
||||||
import { DashboardQueryResult, getGrafanaSearcher } from '../service';
|
import { DashboardQueryResult, getGrafanaSearcher } from '../service';
|
||||||
import { SearchLayout } from '../types';
|
import { SearchLayout } from '../types';
|
||||||
|
import * as utils from '../utils';
|
||||||
|
|
||||||
import { getSearchStateManager } from './SearchStateManager';
|
import { getSearchStateManager } from './SearchStateManager';
|
||||||
|
|
||||||
@@ -44,5 +45,29 @@ describe('SearchStateManager', () => {
|
|||||||
stm.initStateFromUrl();
|
stm.initStateFromUrl();
|
||||||
expect(stm.state.folderUid).toBe(undefined);
|
expect(stm.state.folderUid).toBe(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should reset filters if state is updated and no URL params are present', () => {
|
||||||
|
const parseRouteParamsSpy = jest.spyOn(utils, 'parseRouteParams');
|
||||||
|
// Set initial values
|
||||||
|
parseRouteParamsSpy.mockImplementation(() => ({
|
||||||
|
query: 'hello',
|
||||||
|
sort: 'alpha-asc',
|
||||||
|
}));
|
||||||
|
const stm = getSearchStateManager();
|
||||||
|
stm.initStateFromUrl();
|
||||||
|
|
||||||
|
// Verify that they have been set
|
||||||
|
expect(stm.state.query).toBe('hello');
|
||||||
|
expect(stm.state.sort).toBe('alpha-asc');
|
||||||
|
expect(stm.state.folderUid).toBe(undefined);
|
||||||
|
|
||||||
|
// Changed to a view with no URL state.
|
||||||
|
parseRouteParamsSpy.mockImplementation(() => ({}));
|
||||||
|
stm.initStateFromUrl('abc');
|
||||||
|
|
||||||
|
expect(stm.state.query).toBe('');
|
||||||
|
expect(stm.state.sort).toBe(undefined);
|
||||||
|
expect(stm.state.folderUid).toBe('abc');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ export class SearchStateManager extends StateManagerBase<SearchState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
stateManager.setState({
|
stateManager.setState({
|
||||||
|
...initialState,
|
||||||
...stateFromUrl,
|
...stateFromUrl,
|
||||||
folderUid: folderUid,
|
folderUid: folderUid,
|
||||||
eventTrackingNamespace: folderUid ? 'manage_dashboards' : 'dashboard_search',
|
eventTrackingNamespace: folderUid ? 'manage_dashboards' : 'dashboard_search',
|
||||||
@@ -63,8 +64,10 @@ export class SearchStateManager extends StateManagerBase<SearchState> {
|
|||||||
* Updates internal and url state, then triggers a new search
|
* Updates internal and url state, then triggers a new search
|
||||||
*/
|
*/
|
||||||
setStateAndDoSearch(state: Partial<SearchState>) {
|
setStateAndDoSearch(state: Partial<SearchState>) {
|
||||||
|
const sort = state.sort || this.state.sort || localStorage.getItem(SEARCH_SELECTED_SORT) || undefined;
|
||||||
|
|
||||||
// Set internal state
|
// Set internal state
|
||||||
this.setState(state);
|
this.setState({ sort, ...state });
|
||||||
|
|
||||||
// Update url state
|
// Update url state
|
||||||
this.updateLocation({
|
this.updateLocation({
|
||||||
@@ -290,14 +293,13 @@ export function getSearchStateManager() {
|
|||||||
if (!stateManager) {
|
if (!stateManager) {
|
||||||
const selectedLayout = localStorage.getItem(SEARCH_SELECTED_LAYOUT) as SearchLayout;
|
const selectedLayout = localStorage.getItem(SEARCH_SELECTED_LAYOUT) as SearchLayout;
|
||||||
const layout = selectedLayout ?? initialState.layout;
|
const layout = selectedLayout ?? initialState.layout;
|
||||||
const sort = localStorage.getItem(SEARCH_SELECTED_SORT) ?? undefined;
|
|
||||||
|
|
||||||
let includePanels = store.getBool(SEARCH_PANELS_LOCAL_STORAGE_KEY, true);
|
let includePanels = store.getBool(SEARCH_PANELS_LOCAL_STORAGE_KEY, true);
|
||||||
if (includePanels) {
|
if (includePanels) {
|
||||||
includePanels = false;
|
includePanels = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
stateManager = new SearchStateManager({ ...initialState, layout, sort, includePanels });
|
stateManager = new SearchStateManager({ ...initialState, layout, includePanels });
|
||||||
}
|
}
|
||||||
|
|
||||||
return stateManager;
|
return stateManager;
|
||||||
|
|||||||
Reference in New Issue
Block a user