Skip to content

Commit

Permalink
keep appState in kibana storage
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykkopycinski committed Aug 19, 2020
1 parent 3f1c950 commit e0b07a6
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import { networkActions } from '../../../network/store';
import { timelineActions } from '../../../timelines/store/timeline';
import { useKibana } from '../../lib/kibana';

const APP_STATE_STORAGE_KEY = 'securitySolution.searchBar.appState';

interface SiemSearchBarProps {
id: InputsModelId;
indexPattern: IIndexPattern;
Expand Down Expand Up @@ -81,6 +83,7 @@ export const SearchBarComponent = memo<SiemSearchBarProps & PropsFromRedux>(
},
ui: { SearchBar },
},
storage,
} = useKibana().services;

useEffect(() => {
Expand Down Expand Up @@ -226,6 +229,15 @@ export const SearchBarComponent = memo<SiemSearchBarProps & PropsFromRedux>(
}
}, [savedQuery, updateSearch, id, toStr, end, fromStr, start, filterManager]);

const saveAppStateToStorage = useCallback(
(filters: Filter[]) => storage.set(APP_STATE_STORAGE_KEY, filters),
[storage]
);

const getAppStateFromStorage = useCallback(() => storage.get(APP_STATE_STORAGE_KEY) ?? [], [
storage,
]);

useEffect(() => {
let isSubscribed = true;
const subscriptions = new Subscription();
Expand All @@ -234,6 +246,7 @@ export const SearchBarComponent = memo<SiemSearchBarProps & PropsFromRedux>(
filterManager.getUpdates$().subscribe({
next: () => {
if (isSubscribed) {
saveAppStateToStorage(filterManager.getAppFilters());
setSearchBarFilter({
id,
filters: filterManager.getFilters(),
Expand All @@ -244,7 +257,7 @@ export const SearchBarComponent = memo<SiemSearchBarProps & PropsFromRedux>(
);

// for the initial state
setSearchBarFilter({ id, filters: filterManager.getFilters() });
filterManager.setAppFilters(getAppStateFromStorage());

return () => {
isSubscribed = false;
Expand Down

0 comments on commit e0b07a6

Please sign in to comment.