-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(recommend): introduce
connectTrendingItems
connector (#6169)
- Loading branch information
Showing
9 changed files
with
446 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
packages/instantsearch.js/src/connectors/trending-items/connectTrendingItems.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import { | ||
createDocumentationMessageGenerator, | ||
checkRendering, | ||
noop, | ||
} from '../../lib/utils'; | ||
|
||
import type { Connector, TransformItems, Hit, BaseHit } from '../../types'; | ||
import type { | ||
PlainSearchParameters, | ||
RecommendResultItem, | ||
} from 'algoliasearch-helper'; | ||
|
||
const withUsage = createDocumentationMessageGenerator({ | ||
name: 'trending-items', | ||
connector: true, | ||
}); | ||
|
||
export type TrendingItemsRenderState<THit extends BaseHit = BaseHit> = { | ||
/** | ||
* The matched recommendations from the Algolia API. | ||
*/ | ||
recommendations: Array<Hit<THit>>; | ||
}; | ||
|
||
export type TrendingItemsConnectorParams<THit extends BaseHit = BaseHit> = ( | ||
| { | ||
/** | ||
* The facet attribute to get recommendations for. | ||
*/ | ||
facetName: string; | ||
/** | ||
* The facet value to get recommendations for. | ||
*/ | ||
facetValue: string; | ||
} | ||
| { facetName?: never; facetValue?: never } | ||
) & { | ||
/** | ||
* The number of recommendations to retrieve. | ||
*/ | ||
maxRecommendations?: number; | ||
/** | ||
* The threshold for the recommendations confidence score (between 0 and 100). | ||
*/ | ||
threshold?: number; | ||
/** | ||
* List of search parameters to send. | ||
*/ | ||
fallbackParameters?: Omit< | ||
PlainSearchParameters, | ||
'page' | 'hitsPerPage' | 'offset' | 'length' | ||
>; | ||
/** | ||
* List of search parameters to send. | ||
*/ | ||
queryParameters?: Omit< | ||
PlainSearchParameters, | ||
'page' | 'hitsPerPage' | 'offset' | 'length' | ||
>; | ||
/** | ||
* Function to transform the items passed to the templates. | ||
*/ | ||
transformItems?: TransformItems<Hit<THit>, { results: RecommendResultItem }>; | ||
}; | ||
|
||
export type TrendingItemsWidgetDescription<THit extends BaseHit = BaseHit> = { | ||
$$type: 'ais.trendingItems'; | ||
renderState: TrendingItemsRenderState<THit>; | ||
}; | ||
|
||
export type TrendingItemsConnector<THit extends BaseHit = BaseHit> = Connector< | ||
TrendingItemsWidgetDescription<THit>, | ||
TrendingItemsConnectorParams<THit> | ||
>; | ||
|
||
const connectTrendingItems: TrendingItemsConnector = | ||
function connectTrendingItems(renderFn, unmountFn = noop) { | ||
checkRendering(renderFn, withUsage()); | ||
|
||
return function trendingItems(widgetParams) { | ||
const { | ||
facetName, | ||
facetValue, | ||
maxRecommendations, | ||
threshold, | ||
fallbackParameters, | ||
queryParameters, | ||
transformItems = ((items) => items) as NonNullable< | ||
TrendingItemsConnectorParams['transformItems'] | ||
>, | ||
} = widgetParams || {}; | ||
|
||
return { | ||
dependsOn: 'recommend', | ||
$$type: 'ais.trendingItems', | ||
|
||
init(initOptions) { | ||
renderFn( | ||
{ | ||
...this.getWidgetRenderState(initOptions), | ||
instantSearchInstance: initOptions.instantSearchInstance, | ||
}, | ||
true | ||
); | ||
}, | ||
|
||
render(renderOptions) { | ||
const renderState = this.getWidgetRenderState(renderOptions); | ||
|
||
renderFn( | ||
{ | ||
...renderState, | ||
instantSearchInstance: renderOptions.instantSearchInstance, | ||
}, | ||
false | ||
); | ||
}, | ||
|
||
getRenderState(renderState) { | ||
return renderState; | ||
}, | ||
|
||
getWidgetRenderState({ results }) { | ||
if (results === null || results === undefined) { | ||
return { recommendations: [], widgetParams }; | ||
} | ||
|
||
return { | ||
recommendations: transformItems(results.hits, { | ||
results: results as RecommendResultItem, | ||
}), | ||
widgetParams, | ||
}; | ||
}, | ||
|
||
dispose({ state }) { | ||
unmountFn(); | ||
|
||
return state; | ||
}, | ||
|
||
getWidgetParameters(state) { | ||
return state.addTrendingItems({ | ||
facetName, | ||
facetValue, | ||
maxRecommendations, | ||
threshold, | ||
fallbackParameters, | ||
queryParameters, | ||
$$id: this.$$id!, | ||
}); | ||
}, | ||
}; | ||
}; | ||
}; | ||
|
||
export default connectTrendingItems; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { fakeAct } from '../../common'; | ||
|
||
import { createOptionsTests } from './options'; | ||
|
||
import type { TestOptions, TestSetup } from '../../common'; | ||
import type { TrendingItemsConnectorParams } from 'instantsearch.js/src/connectors/trending-items/connectTrendingItems'; | ||
|
||
export type TrendingItemsConnectorSetup = TestSetup<{ | ||
widgetParams: TrendingItemsConnectorParams; | ||
}>; | ||
|
||
export function createTrendingItemsConnectorTests( | ||
setup: TrendingItemsConnectorSetup, | ||
{ act = fakeAct, skippedTests = {} }: TestOptions = {} | ||
) { | ||
beforeAll(() => { | ||
document.body.innerHTML = ''; | ||
}); | ||
|
||
describe('TrendingItems connector common tests', () => { | ||
createOptionsTests(setup, { act, skippedTests }); | ||
}); | ||
} |
Oops, something went wrong.