Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Vis Builder] Add app filter and query persistence without using state container #3100

Merged
Prev Previous commit
Next Next commit
error handling
Signed-off-by: abbyhu2000 <abigailhu2000@gmail.com>
  • Loading branch information
abbyhu2000 committed Jan 6, 2023
commit 0cf3695795837708a5e4a2d065ef36a8f67fdffa
126 changes: 65 additions & 61 deletions src/plugins/data/public/query/state_sync/connect_to_query_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,77 +60,81 @@ export const connectStorageToQueryState = (
query: boolean;
}
) => {
const syncKeys: Array<keyof QueryStateChange> = [];
if (syncConfig.query) {
syncKeys.push('query');
}
if (syncConfig.filters === FilterStateStore.APP_STATE) {
syncKeys.push('appFilters');
}

const initialStateFromURL: QueryState = OsdUrlStateStorage.get('_q') ?? {
query: queryString.getDefaultQuery(),
filters: filterManager.getAppFilters(),
};
try {
const syncKeys: Array<keyof QueryStateChange> = [];
if (syncConfig.query) {
syncKeys.push('query');
}
if (syncConfig.filters === FilterStateStore.APP_STATE) {
syncKeys.push('appFilters');
}

// set up initial '_q' flag in the URL to sync query and filter changes
if (!OsdUrlStateStorage.get('_q')) {
OsdUrlStateStorage.set('_q', initialStateFromURL, {
replace: true,
});
}
const initialStateFromURL: QueryState = OsdUrlStateStorage.get('_q') ?? {
query: queryString.getDefaultQuery(),
filters: filterManager.getAppFilters(),
};

if (syncConfig.query && !_.isEqual(initialStateFromURL.query, queryString.getQuery())) {
if (initialStateFromURL.query) {
queryString.setQuery(_.cloneDeep(initialStateFromURL.query));
// set up initial '_q' flag in the URL to sync query and filter changes
if (!OsdUrlStateStorage.get('_q')) {
OsdUrlStateStorage.set('_q', initialStateFromURL, {
replace: true,
});
}
}

if (syncConfig.filters === FilterStateStore.APP_STATE) {
if (
!initialStateFromURL.filters ||
!compareFilters(initialStateFromURL.filters, filterManager.getAppFilters(), {
...COMPARE_ALL_OPTIONS,
state: false,
})
) {
if (initialStateFromURL.filters) {
filterManager.setAppFilters(_.cloneDeep(initialStateFromURL.filters));
if (syncConfig.query && !_.isEqual(initialStateFromURL.query, queryString.getQuery())) {
if (initialStateFromURL.query) {
queryString.setQuery(_.cloneDeep(initialStateFromURL.query));
}
}

const subs: Subscription[] = [
state$
.pipe(
filter(({ changes }) => {
return syncKeys.some((syncKey) => changes[syncKey]);
}),
map(({ changes, state }) => {
const newState: QueryState = {
query: state.query,
filters: state.filters,
};
if (syncConfig.query && changes.query) {
newState.query = queryString.getQuery();
}
if (syncConfig.filters === FilterStateStore.APP_STATE) {
if (
!initialStateFromURL.filters ||
!compareFilters(initialStateFromURL.filters, filterManager.getAppFilters(), {
...COMPARE_ALL_OPTIONS,
state: false,
})
) {
if (initialStateFromURL.filters) {
filterManager.setAppFilters(_.cloneDeep(initialStateFromURL.filters));
}
}

if (syncConfig.filters === FilterStateStore.APP_STATE && changes.appFilters) {
newState.filters = filterManager.getAppFilters();
}
const subs: Subscription[] = [
state$
.pipe(
filter(({ changes }) => {
return syncKeys.some((syncKey) => changes[syncKey]);
}),
map(({ changes, state }) => {
const newState: QueryState = {
query: state.query,
filters: state.filters,
};
if (syncConfig.query && changes.query) {
newState.query = queryString.getQuery();
}

return newState;
})
)
.subscribe((newState) => {
OsdUrlStateStorage.set('_q', newState, {
replace: true,
});
}),
];
if (syncConfig.filters === FilterStateStore.APP_STATE && changes.appFilters) {
newState.filters = filterManager.getAppFilters();
}

return () => {
subs.forEach((s) => s.unsubscribe());
};
return newState;
})
)
.subscribe((newState) => {
OsdUrlStateStorage.set('_q', newState, {
replace: true,
});
}),
];

return () => {
subs.forEach((s) => s.unsubscribe());
};
}
} catch (err) {
return;
}
};

Expand Down