From 747a4de9bf18fd4e6a0cbb0988dbe9f5f8b13b6d Mon Sep 17 00:00:00 2001 From: acondal Date: Thu, 16 Nov 2023 17:55:08 +0100 Subject: [PATCH 01/27] feat(query-preview): Add updateQueryPreviewHistory action/commits/types EMP-2867 --- .../x-modules/queries-preview/config.types.ts | 4 ++ .../update-query-preview-history.action.ts | 41 +++++++++++++++++++ .../x-modules/queries-preview/store/module.ts | 19 +++++++-- .../x-modules/queries-preview/store/types.ts | 26 ++++++++++++ 4 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 packages/x-components/src/x-modules/queries-preview/store/actions/update-query-preview-history.action.ts diff --git a/packages/x-components/src/x-modules/queries-preview/config.types.ts b/packages/x-components/src/x-modules/queries-preview/config.types.ts index 90e2934540..6c9ce417fe 100644 --- a/packages/x-components/src/x-modules/queries-preview/config.types.ts +++ b/packages/x-components/src/x-modules/queries-preview/config.types.ts @@ -8,4 +8,8 @@ export interface QueriesPreviewConfig { * Maximum number of items to request. */ maxItemsToRequest: number; + /** + * Maximum number of most recent used queries preview. + */ + maxQueryPreviewHistoryLength: number; } diff --git a/packages/x-components/src/x-modules/queries-preview/store/actions/update-query-preview-history.action.ts b/packages/x-components/src/x-modules/queries-preview/store/actions/update-query-preview-history.action.ts new file mode 100644 index 0000000000..2a148cddaf --- /dev/null +++ b/packages/x-components/src/x-modules/queries-preview/store/actions/update-query-preview-history.action.ts @@ -0,0 +1,41 @@ +import { QueriesPreviewXStoreModule } from '../types'; + +/** + * Default implementation for the {@link QueriesPreviewActions.updateQueryPreviewHistory}. + * + * @param _context - The {@link https://vuex.vuejs.org/guide/actions.html | context} + * of the actions, provided by Vuex. + * @param request - The query preview request. + * + * @public + */ +// eslint-disable-next-line max-len +export const updateQueryPreviewHistory: QueriesPreviewXStoreModule['actions']['updateQueryPreviewHistory'] = + ({ commit, state, getters }, request) => { + const { query } = request; + const loadedQueryPreview = getters.loadedQueriesPreview[query]; + + // If the query preview item was already stored, remove the old one. + if (state.queryPreviewHistory.some(item => item === loadedQueryPreview)) { + commit('removeFromQueryPreviewHistory', { + request, + results: loadedQueryPreview.results, + status: loadedQueryPreview.status, + totalResults: loadedQueryPreview.totalResults + }); + } + + // If the queryPreviewHistory list exceeds the configured max.length to store, + // pop the last item + if (state.queryPreviewHistory.length === state.config.maxQueryPreviewHistoryLength) { + commit('popQueryPreviewHistory'); + } + + // Add query preview item to the queryPreviewHistory. + commit('setQueryPreviewHistory', { + request, + results: loadedQueryPreview.results, + status: loadedQueryPreview.status, + totalResults: loadedQueryPreview.totalResults + }); + }; diff --git a/packages/x-components/src/x-modules/queries-preview/store/module.ts b/packages/x-components/src/x-modules/queries-preview/store/module.ts index 17a42791a1..e9afce5bac 100644 --- a/packages/x-components/src/x-modules/queries-preview/store/module.ts +++ b/packages/x-components/src/x-modules/queries-preview/store/module.ts @@ -4,6 +4,7 @@ import { QueriesPreviewXStoreModule } from './types'; import { fetchQueryPreview } from './actions/fetch-query-preview.action'; import { fetchAndSaveQueryPreview } from './actions/fetch-and-save-query-preview.action'; import { loadedQueriesPreview } from './getters/loaded-queries-preview.getter'; +import { updateQueryPreviewHistory } from './actions/update-query-preview-history.action'; /** * {@link XStoreModule} For the `queries-preview` module. @@ -13,9 +14,11 @@ import { loadedQueriesPreview } from './getters/loaded-queries-preview.getter'; export const queriesPreviewXStoreModule: QueriesPreviewXStoreModule = { state: () => ({ config: { - maxItemsToRequest: 24 + maxItemsToRequest: 24, + maxQueryPreviewHistoryLength: 5 }, queriesPreview: {}, + queryPreviewHistory: [], selectedQueryPreview: { query: '', extraParams: undefined, @@ -41,10 +44,20 @@ export const queriesPreviewXStoreModule: QueriesPreviewXStoreModule = { state.selectedQueryPreview = selectedQueryPreview; }, setConfig, - mergeConfig + mergeConfig, + setQueryPreviewHistory(state, queryPreview) { + state.queryPreviewHistory.push(queryPreview); + }, + removeFromQueryPreviewHistory(state, queryPreview) { + state.queryPreviewHistory.filter(item => item === queryPreview); + }, + popQueryPreviewHistory(state) { + state.queryPreviewHistory.pop(); + } }, actions: { fetchQueryPreview, - fetchAndSaveQueryPreview + fetchAndSaveQueryPreview, + updateQueryPreviewHistory } }; diff --git a/packages/x-components/src/x-modules/queries-preview/store/types.ts b/packages/x-components/src/x-modules/queries-preview/store/types.ts index 0b78414632..54ebfba5ba 100644 --- a/packages/x-components/src/x-modules/queries-preview/store/types.ts +++ b/packages/x-components/src/x-modules/queries-preview/store/types.ts @@ -51,6 +51,8 @@ export interface QueryPreviewInfo { export interface QueriesPreviewState { /* The request and results */ queriesPreview: Dictionary; + /* List of the most recent used queries preview */ + queryPreviewHistory: QueryPreviewItem[]; /** The configuration of the queries preview module. */ config: QueriesPreviewConfig; /** The extra params property of the state. */ @@ -107,6 +109,22 @@ export interface QueriesPreviewMutations extends ConfigMutations Date: Thu, 16 Nov 2023 18:15:00 +0100 Subject: [PATCH 02/27] feat(query-preview): Call updateQueryPreviewHistory action & fetch only when no loadedQueriesPreview EMP-2867 --- .../fetch-and-save-query-preview.action.ts | 48 +++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/packages/x-components/src/x-modules/queries-preview/store/actions/fetch-and-save-query-preview.action.ts b/packages/x-components/src/x-modules/queries-preview/store/actions/fetch-and-save-query-preview.action.ts index dd001e8dda..390ba2fa96 100644 --- a/packages/x-components/src/x-modules/queries-preview/store/actions/fetch-and-save-query-preview.action.ts +++ b/packages/x-components/src/x-modules/queries-preview/store/actions/fetch-and-save-query-preview.action.ts @@ -12,29 +12,37 @@ import { QueriesPreviewXStoreModule } from '../types'; */ // eslint-disable-next-line max-len export const fetchAndSaveQueryPreview: QueriesPreviewXStoreModule['actions']['fetchAndSaveQueryPreview'] = - ({ dispatch, commit }, request) => { + ({ dispatch, commit, getters }, request) => { const { query } = request; + if (!query) { return; } - commit('setQueryPreview', { - request, - results: [], - status: 'loading', - totalResults: 0 - }); - return dispatch('fetchQueryPreview', request) - .then(response => { - commit('setQueryPreview', { - request, - results: response?.results ?? [], - status: 'success', - totalResults: response?.totalResults ?? 0 - }); - }) - .catch(error => { - // eslint-disable-next-line no-console - console.error(error); - commit('setStatus', { query, status: 'error' }); + + if (!getters.loadedQueriesPreview[query]) { + commit('setQueryPreview', { + request, + results: [], + status: 'loading', + totalResults: 0 }); + + return dispatch('fetchQueryPreview', request) + .then(response => { + commit('setQueryPreview', { + request, + results: response?.results ?? [], + status: 'success', + totalResults: response?.totalResults ?? 0 + }); + }) + .catch(error => { + // eslint-disable-next-line no-console + console.error(error); + commit('setStatus', { query, status: 'error' }); + }) + .then(() => { + return dispatch('updateQueryPreviewHistory', request); + }); + } }; From 101c6cece5ec1dd2900c6d6edcab0f474f9bf4b8 Mon Sep 17 00:00:00 2001 From: acondal Date: Fri, 17 Nov 2023 11:47:25 +0100 Subject: [PATCH 03/27] feat(query-preview): Use deepEqual in updateQueryPreviewHistory action EMP-2867 --- .../store/actions/update-query-preview-history.action.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/x-components/src/x-modules/queries-preview/store/actions/update-query-preview-history.action.ts b/packages/x-components/src/x-modules/queries-preview/store/actions/update-query-preview-history.action.ts index 2a148cddaf..25b944b7ad 100644 --- a/packages/x-components/src/x-modules/queries-preview/store/actions/update-query-preview-history.action.ts +++ b/packages/x-components/src/x-modules/queries-preview/store/actions/update-query-preview-history.action.ts @@ -1,3 +1,4 @@ +import { deepEqual } from '@empathyco/x-utils'; import { QueriesPreviewXStoreModule } from '../types'; /** @@ -16,7 +17,7 @@ export const updateQueryPreviewHistory: QueriesPreviewXStoreModule['actions']['u const loadedQueryPreview = getters.loadedQueriesPreview[query]; // If the query preview item was already stored, remove the old one. - if (state.queryPreviewHistory.some(item => item === loadedQueryPreview)) { + if (state.queryPreviewHistory.some(item => deepEqual(item, loadedQueryPreview))) { commit('removeFromQueryPreviewHistory', { request, results: loadedQueryPreview.results, From 492815bd602ac4568e2784ee61092b709d57cab9 Mon Sep 17 00:00:00 2001 From: acondal Date: Fri, 17 Nov 2023 12:30:29 +0100 Subject: [PATCH 04/27] feat(query-preview): wip render query preview from history EMP-2867 --- .../components/query-preview.vue | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/x-components/src/x-modules/queries-preview/components/query-preview.vue b/packages/x-components/src/x-modules/queries-preview/components/query-preview.vue index 10d7f7dc43..eda647856d 100644 --- a/packages/x-components/src/x-modules/queries-preview/components/query-preview.vue +++ b/packages/x-components/src/x-modules/queries-preview/components/query-preview.vue @@ -104,7 +104,7 @@ * * @public */ - @Prop({ default: true }) + @Prop({ default: false }) public clearOnDestroy!: boolean; /** @@ -128,6 +128,9 @@ @State('queriesPreview', 'config') public config!: QueriesPreviewConfig; + @State('queriesPreview', 'queryPreviewHistory') + public queryPreviewHistory!: QueryPreviewItem[]; + /** * The results to render from the state. * @@ -225,6 +228,19 @@ this.emitQueryPreviewRequestUpdated(this.queryPreviewRequest); } + /** + * Checks whether the current queryPreviewItem has been saved + * in the queryPreviewHistory in the state and emits load accordingly. + * + * @internal + */ + protected beforeMount(): void { + const previewItem = this.previewResults[this.queryPreviewInfo.query]; + if (this.queryPreviewHistory.some(item => deepEqual(item, previewItem))) { + this.$emit('load', this.queryPreviewInfo.query); + } + } + /** * Cancels the (remaining) requests when the component is destroyed * via the `debounce.cancel()` method. From f398456e732250e6410eae7e42aa872e2cfaa3be Mon Sep 17 00:00:00 2001 From: acondal Date: Mon, 20 Nov 2023 15:52:59 +0100 Subject: [PATCH 05/27] feat(query-preview): fix removeFromQueryPreviewHistory EMP-2867 --- .../src/x-modules/queries-preview/store/module.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/x-components/src/x-modules/queries-preview/store/module.ts b/packages/x-components/src/x-modules/queries-preview/store/module.ts index e9afce5bac..916cfc5297 100644 --- a/packages/x-components/src/x-modules/queries-preview/store/module.ts +++ b/packages/x-components/src/x-modules/queries-preview/store/module.ts @@ -48,8 +48,8 @@ export const queriesPreviewXStoreModule: QueriesPreviewXStoreModule = { setQueryPreviewHistory(state, queryPreview) { state.queryPreviewHistory.push(queryPreview); }, - removeFromQueryPreviewHistory(state, queryPreview) { - state.queryPreviewHistory.filter(item => item === queryPreview); + removeFromQueryPreviewHistory(state, queryPreviewItem) { + state.queryPreviewHistory.splice(state.queryPreviewHistory.indexOf(queryPreviewItem), 1); }, popQueryPreviewHistory(state) { state.queryPreviewHistory.pop(); From 3b2ccd33c329a9c19496a3fbf3580e73883227d4 Mon Sep 17 00:00:00 2001 From: acondal Date: Mon, 20 Nov 2023 16:14:45 +0100 Subject: [PATCH 06/27] feat(query-preview): rm popQueryPreviewHistory as it doesn't work properly with semantic-queries EMP-2867 --- .../src/x-modules/queries-preview/config.types.ts | 4 ---- .../store/actions/update-query-preview-history.action.ts | 6 ------ .../src/x-modules/queries-preview/store/module.ts | 6 +----- .../src/x-modules/queries-preview/store/types.ts | 5 ----- 4 files changed, 1 insertion(+), 20 deletions(-) diff --git a/packages/x-components/src/x-modules/queries-preview/config.types.ts b/packages/x-components/src/x-modules/queries-preview/config.types.ts index 6c9ce417fe..90e2934540 100644 --- a/packages/x-components/src/x-modules/queries-preview/config.types.ts +++ b/packages/x-components/src/x-modules/queries-preview/config.types.ts @@ -8,8 +8,4 @@ export interface QueriesPreviewConfig { * Maximum number of items to request. */ maxItemsToRequest: number; - /** - * Maximum number of most recent used queries preview. - */ - maxQueryPreviewHistoryLength: number; } diff --git a/packages/x-components/src/x-modules/queries-preview/store/actions/update-query-preview-history.action.ts b/packages/x-components/src/x-modules/queries-preview/store/actions/update-query-preview-history.action.ts index 25b944b7ad..275e39f1c9 100644 --- a/packages/x-components/src/x-modules/queries-preview/store/actions/update-query-preview-history.action.ts +++ b/packages/x-components/src/x-modules/queries-preview/store/actions/update-query-preview-history.action.ts @@ -26,12 +26,6 @@ export const updateQueryPreviewHistory: QueriesPreviewXStoreModule['actions']['u }); } - // If the queryPreviewHistory list exceeds the configured max.length to store, - // pop the last item - if (state.queryPreviewHistory.length === state.config.maxQueryPreviewHistoryLength) { - commit('popQueryPreviewHistory'); - } - // Add query preview item to the queryPreviewHistory. commit('setQueryPreviewHistory', { request, diff --git a/packages/x-components/src/x-modules/queries-preview/store/module.ts b/packages/x-components/src/x-modules/queries-preview/store/module.ts index 916cfc5297..830b9e8e57 100644 --- a/packages/x-components/src/x-modules/queries-preview/store/module.ts +++ b/packages/x-components/src/x-modules/queries-preview/store/module.ts @@ -14,8 +14,7 @@ import { updateQueryPreviewHistory } from './actions/update-query-preview-histor export const queriesPreviewXStoreModule: QueriesPreviewXStoreModule = { state: () => ({ config: { - maxItemsToRequest: 24, - maxQueryPreviewHistoryLength: 5 + maxItemsToRequest: 24 }, queriesPreview: {}, queryPreviewHistory: [], @@ -50,9 +49,6 @@ export const queriesPreviewXStoreModule: QueriesPreviewXStoreModule = { }, removeFromQueryPreviewHistory(state, queryPreviewItem) { state.queryPreviewHistory.splice(state.queryPreviewHistory.indexOf(queryPreviewItem), 1); - }, - popQueryPreviewHistory(state) { - state.queryPreviewHistory.pop(); } }, actions: { diff --git a/packages/x-components/src/x-modules/queries-preview/store/types.ts b/packages/x-components/src/x-modules/queries-preview/store/types.ts index 54ebfba5ba..28d1db86e2 100644 --- a/packages/x-components/src/x-modules/queries-preview/store/types.ts +++ b/packages/x-components/src/x-modules/queries-preview/store/types.ts @@ -121,10 +121,6 @@ export interface QueriesPreviewMutations extends ConfigMutations Date: Mon, 20 Nov 2023 16:19:21 +0100 Subject: [PATCH 07/27] feat(query-preview): update event payload to suit deleting both from the queriesPreview and the queryPreviewHistory EMP-2867 --- .../queries-preview/components/query-preview.vue | 3 ++- .../src/x-modules/queries-preview/events.types.ts | 6 +++--- .../update-query-preview-history.action.ts | 2 +- .../src/x-modules/queries-preview/store/module.ts | 2 +- .../src/x-modules/queries-preview/store/types.ts | 2 +- .../src/x-modules/queries-preview/wiring.ts | 15 +++++++++++++-- 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/x-components/src/x-modules/queries-preview/components/query-preview.vue b/packages/x-components/src/x-modules/queries-preview/components/query-preview.vue index eda647856d..7cbe126452 100644 --- a/packages/x-components/src/x-modules/queries-preview/components/query-preview.vue +++ b/packages/x-components/src/x-modules/queries-preview/components/query-preview.vue @@ -253,7 +253,8 @@ this.emitQueryPreviewRequestUpdated.cancel(); if (this.clearOnDestroy) { - this.$x.emit('QueryPreviewUnmountedHook', this.queryPreviewInfo.query, { + const previewItem = this.previewResults[this.queryPreviewInfo.query]; + this.$x.emit('QueryPreviewUnmountedHook', previewItem, { priority: 0, replaceable: false }); diff --git a/packages/x-components/src/x-modules/queries-preview/events.types.ts b/packages/x-components/src/x-modules/queries-preview/events.types.ts index eab70ddeb8..5dd88632f0 100644 --- a/packages/x-components/src/x-modules/queries-preview/events.types.ts +++ b/packages/x-components/src/x-modules/queries-preview/events.types.ts @@ -1,6 +1,6 @@ import { SearchRequest } from '@empathyco/x-types'; import { Dictionary } from '@empathyco/x-utils'; -import { QueryPreviewInfo } from './store/index'; +import { QueryPreviewInfo, QueryPreviewItem } from './store/index'; /** * Dictionary of the events of QueriesPreview XModule, where each key is the event name, and the @@ -16,9 +16,9 @@ export interface QueriesPreviewXEvents { QueryPreviewRequestUpdated: SearchRequest; /** * The component that shows a Query preview has been unmounted. - * Payload: The query whose preview has been removed. + * Payload: The query preview item. */ - QueryPreviewUnmountedHook: string; + QueryPreviewUnmountedHook: QueryPreviewItem; /** * User has clicked on a query preview. * Payload: The {@link QueryPreviewInfo | query preview info} diff --git a/packages/x-components/src/x-modules/queries-preview/store/actions/update-query-preview-history.action.ts b/packages/x-components/src/x-modules/queries-preview/store/actions/update-query-preview-history.action.ts index 275e39f1c9..f256fda040 100644 --- a/packages/x-components/src/x-modules/queries-preview/store/actions/update-query-preview-history.action.ts +++ b/packages/x-components/src/x-modules/queries-preview/store/actions/update-query-preview-history.action.ts @@ -18,7 +18,7 @@ export const updateQueryPreviewHistory: QueriesPreviewXStoreModule['actions']['u // If the query preview item was already stored, remove the old one. if (state.queryPreviewHistory.some(item => deepEqual(item, loadedQueryPreview))) { - commit('removeFromQueryPreviewHistory', { + commit('clearFromQueryPreviewHistory', { request, results: loadedQueryPreview.results, status: loadedQueryPreview.status, diff --git a/packages/x-components/src/x-modules/queries-preview/store/module.ts b/packages/x-components/src/x-modules/queries-preview/store/module.ts index 830b9e8e57..3746b68632 100644 --- a/packages/x-components/src/x-modules/queries-preview/store/module.ts +++ b/packages/x-components/src/x-modules/queries-preview/store/module.ts @@ -47,7 +47,7 @@ export const queriesPreviewXStoreModule: QueriesPreviewXStoreModule = { setQueryPreviewHistory(state, queryPreview) { state.queryPreviewHistory.push(queryPreview); }, - removeFromQueryPreviewHistory(state, queryPreviewItem) { + clearFromQueryPreviewHistory(state, queryPreviewItem) { state.queryPreviewHistory.splice(state.queryPreviewHistory.indexOf(queryPreviewItem), 1); } }, diff --git a/packages/x-components/src/x-modules/queries-preview/store/types.ts b/packages/x-components/src/x-modules/queries-preview/store/types.ts index 28d1db86e2..9cf7123671 100644 --- a/packages/x-components/src/x-modules/queries-preview/store/types.ts +++ b/packages/x-components/src/x-modules/queries-preview/store/types.ts @@ -120,7 +120,7 @@ export interface QueriesPreviewMutations extends ConfigMutations request['query'] +); + +/** + * Clears a query preview from the query preview history list. + * + * @public + */ +export const clearFromQueryPreviewHistoryWire = wireCommit('clearFromQueryPreviewHistory'); /** * Sets the queries preview state `params`. @@ -70,7 +80,8 @@ export const queriesPreviewWiring = createWiring({ fetchAndSaveQueryPreviewWire }, QueryPreviewUnmountedHook: { - clearQueryPreviewWire + clearQueryPreviewWire, + clearFromQueryPreviewHistoryWire }, UserAcceptedAQueryPreview: { setSelectedQueryPreviewWire From b1b0394ae80fc6ded814dafa782de75dea519d20 Mon Sep 17 00:00:00 2001 From: acondal Date: Mon, 20 Nov 2023 16:21:23 +0100 Subject: [PATCH 08/27] feat(query-preview-list): make clearOnDestroy accessible to pass it as true in the queryPreviewList used in semanticQueries EMP-2867 --- packages/x-components/src/views/home/Home.vue | 1 + .../queries-preview/components/query-preview-list.vue | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/packages/x-components/src/views/home/Home.vue b/packages/x-components/src/views/home/Home.vue index 1e6d7b173c..d6f1091e7b 100644 --- a/packages/x-components/src/views/home/Home.vue +++ b/packages/x-components/src/views/home/Home.vue @@ -357,6 +357,7 @@ :queries-preview-info="queries.map(q => ({ query: q }))" #default="{ queryPreviewInfo: { query }, results }" queryFeature="semantics" + :clearOnDestroy="true" >