Skip to content

Commit

Permalink
Store last selected view, group and sort options
Browse files Browse the repository at this point in the history
  • Loading branch information
kdelemme committed Dec 31, 2024
1 parent 2e0f3f7 commit 7a29fed
Showing 1 changed file with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
*/

import type { Filter } from '@kbn/es-query';
import { createKbnUrlStateStorage } from '@kbn/kibana-utils-plugin/public';
import {
createKbnUrlStateStorage,
createSessionStorageStateStorage,
} from '@kbn/kibana-utils-plugin/public';
import deepmerge from 'deepmerge';
import { pick } from 'lodash';
import { useCallback, useEffect, useRef, useState } from 'react';
import { useHistory } from 'react-router-dom';
import { DEFAULT_SLO_PAGE_SIZE } from '../../../../common/constants';
import type { GroupByField, SortDirection, SortField, ViewType } from '../types';

export const SLO_LIST_SEARCH_URL_STORAGE_KEY = 'search';
export const SLO_LIST_SEARCH_SESSION_STORAGE_KEY = 'slo_list_search_state';

export interface SearchState {
kqlQuery: string;
Expand Down Expand Up @@ -56,6 +61,8 @@ export function useUrlSearchState(): {
})
);

const sessionStorage = useRef(createSessionStorageStateStorage());

useEffect(() => {
const sub = urlStateStorage.current
?.change$<SearchState>(SLO_LIST_SEARCH_URL_STORAGE_KEY)
Expand All @@ -66,21 +73,30 @@ export function useUrlSearchState(): {
});

setState(
urlStateStorage.current?.get<SearchState>(SLO_LIST_SEARCH_URL_STORAGE_KEY) ?? DEFAULT_STATE
urlStateStorage.current?.get<SearchState>(SLO_LIST_SEARCH_URL_STORAGE_KEY) ??
sessionStorage.current?.get<SearchState>(SLO_LIST_SEARCH_SESSION_STORAGE_KEY) ??
DEFAULT_STATE
);

return () => {
sub?.unsubscribe();
};
}, [urlStateStorage]);
}, [urlStateStorage, sessionStorage]);

const onStateChange = useCallback(
(newState: Partial<SearchState>) => {
const updatedState = { ...state, page: 0, ...newState };
setState(() => updatedState);

urlStateStorage.current?.set(SLO_LIST_SEARCH_URL_STORAGE_KEY, updatedState, {
replace: true,
});

// Discard search itself from session storage. Keep only view preferences
sessionStorage.current?.set(
SLO_LIST_SEARCH_SESSION_STORAGE_KEY,
pick(updatedState, 'sort', 'view', 'groupBy')
);
},
[state]
);
Expand Down

0 comments on commit 7a29fed

Please sign in to comment.