From 3ece027fafbdd142f3628df48dd0810cf4624e0c Mon Sep 17 00:00:00 2001 From: Diego Pascual Date: Mon, 11 Mar 2024 17:50:10 +0100 Subject: [PATCH] refactor: move logic to wiring --- .../components/semantic-queries.vue | 23 ++---------- .../src/x-modules/tagging/wiring.ts | 35 +++++++++++++++++-- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/packages/x-components/src/x-modules/semantic-queries/components/semantic-queries.vue b/packages/x-components/src/x-modules/semantic-queries/components/semantic-queries.vue index da1d2dfb53..af89b51114 100644 --- a/packages/x-components/src/x-modules/semantic-queries/components/semantic-queries.vue +++ b/packages/x-components/src/x-modules/semantic-queries/components/semantic-queries.vue @@ -43,11 +43,11 @@ diff --git a/packages/x-components/src/x-modules/tagging/wiring.ts b/packages/x-components/src/x-modules/tagging/wiring.ts index 376bdc2f76..b3e84e4f1c 100644 --- a/packages/x-components/src/x-modules/tagging/wiring.ts +++ b/packages/x-components/src/x-modules/tagging/wiring.ts @@ -1,4 +1,4 @@ -import { Taggable, Tagging, TaggingRequest } from '@empathyco/x-types'; +import { SemanticQuery, Taggable, Tagging, TaggingRequest } from '@empathyco/x-types'; import { DefaultSessionService } from '@empathyco/x-utils'; import { namespacedWireCommit, @@ -206,6 +206,35 @@ export function createTrackWire(property: keyof Tagging): Wire { ); } +/** + * Performs a track of a query with no results that used semantics as fallback. + * The totalHits will be changed to -1 if semantic queries are found in order to differentiate + * it from scenarios where the user encounters a no-results page without any semantic queries. + * + * @public + */ +export const trackNoResultsQueryWithSemanticsWire = filter( + wireDispatch('track', ({ eventPayload, state }) => { + const { queryTaggingInfo } = state; + const totalHits = (eventPayload as SemanticQuery[]).length > 0 ? -1 : 0; + return { + params: { ...queryTaggingInfo?.params, totalHits }, + url: queryTaggingInfo?.url ?? '' + }; + }), + ({ store }) => Number(store.state.x.tagging.queryTaggingInfo?.params.totalHits)! === 0 +); + +/**. + * Debounced version of {@link trackNoResultsQueryWithSemanticsWire} + * + * @public + */ +export const trackNoResultsQueryWithSemanticsWireDebounced = moduleDebounce( + trackNoResultsQueryWithSemanticsWire, + ({ state }) => state.config.queryTaggingDebounceMs +); + /** * Factory helper to create a wire for the track of the display click. * @@ -298,8 +327,8 @@ export const taggingWiring = createWiring({ trackDisplayClickedWire, setQueryTaggingFromQueryPreview }, - QueryTaggingWithNoResults: { - setQueryTaggingWithNoResults + SemanticQueriesResponseChanged: { + trackNoResultsQueryWithSemanticsWireDebounced }, ModuleRegistered: { setHasSemantics