Skip to content

Commit

Permalink
wip: index pattern saved in LS
Browse files Browse the repository at this point in the history
  • Loading branch information
mbondyra committed Jun 25, 2020
1 parent 0ef7bb8 commit ce8fcd0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export function getIndexPatternDatasource({
state,
savedObjectsClient: await savedObjectsClient,
defaultIndexPatternId: core.uiSettings.get('defaultIndex'),
storage,
});
},

Expand Down Expand Up @@ -207,6 +208,7 @@ export function getIndexPatternDatasource({
setState,
savedObjectsClient,
onError: onIndexPatternLoadError,
storage,
});
}}
data={data}
Expand Down
17 changes: 17 additions & 0 deletions x-pack/plugins/lens/public/indexpattern_datasource/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,36 @@ export async function loadIndexPatterns({
);
}

const STORAGE_KEY = 'lens-settings';

const readFromStorage = (storage) => {
const data = storage.get(STORAGE_KEY);
return data?.indexPattern;
};
const writeToStorage = (storage, value: string) => {
storage.set(STORAGE_KEY, { indexPattern: value });
};

export async function loadInitialState({
state,
savedObjectsClient,
defaultIndexPatternId,
storage,
}: {
state?: IndexPatternPersistedState;
savedObjectsClient: SavedObjectsClient;
defaultIndexPatternId?: string;
}): Promise<IndexPatternPrivateState> {
// check if the index exists in storage and then if still in kibana
const savedInStorage = readFromStorage(storage);
const indexPatternRefs = await loadIndexPatternRefs(savedObjectsClient);
const requiredPatterns = _.unique(
state
? Object.values(state.layers)
.map((l) => l.indexPatternId)
.concat(state.currentIndexPatternId)
: indexPatternRefs.find((i) => i.id === savedInStorage)
? [savedInStorage]
: [defaultIndexPatternId || indexPatternRefs[0].id]
);

Expand Down Expand Up @@ -120,6 +135,7 @@ export async function changeIndexPattern({
state,
setState,
onError,
storage,
}: {
id: string;
savedObjectsClient: SavedObjectsClient;
Expand Down Expand Up @@ -148,6 +164,7 @@ export async function changeIndexPattern({
} catch (err) {
onError(err);
}
writeToStorage(storage, id);
}

export async function changeLayerIndexPattern({
Expand Down

0 comments on commit ce8fcd0

Please sign in to comment.