Skip to content

Commit

Permalink
fix(recommend): use Hit type instead of AlgoliaHit (#6540)
Browse files Browse the repository at this point in the history
* fix(recommend): in intantsearch.js use Hit instead of AlgoliaHit

* fix: lint

* fix: use hit everywhere

* not for transformItems

---------

Co-authored-by: Haroen Viaene <[email protected]>
  • Loading branch information
raed667 and Haroenv authored Jan 22, 2025
1 parent 3d0c6cb commit 6f2dc2f
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
Unmounter,
UnknownWidgetParams,
RecommendResponse,
Hit,
AlgoliaHit,
} from '../../types';
import type { PlainSearchParameters } from 'algoliasearch-helper';
Expand All @@ -33,7 +34,7 @@ export type FrequentlyBoughtTogetherRenderState<
/**
* The matched recommendations from Algolia API.
*/
items: Array<AlgoliaHit<THit>>;
items: Array<Hit<THit>>;

/**
* Sends an event to the Insights middleware.
Expand Down Expand Up @@ -78,7 +79,7 @@ export type FrequentlyBoughtTogetherConnectorParams<
* Function to transform the items passed to the templates.
*/
transformItems?: TransformItems<
AlgoliaHit<THit>,
Hit<THit>,
{ results: RecommendResponse<AlgoliaHit<THit>> }
>;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
Unmounter,
UnknownWidgetParams,
RecommendResponse,
Hit,
AlgoliaHit,
} from '../../types';
import type { PlainSearchParameters } from 'algoliasearch-helper';
Expand All @@ -33,7 +34,7 @@ export type LookingSimilarRenderState<
/**
* The matched recommendations from the Algolia API.
*/
items: Array<AlgoliaHit<THit>>;
items: Array<Hit<THit>>;
/**
* Sends an event to the Insights middleware.
*/
Expand Down Expand Up @@ -79,7 +80,7 @@ export type LookingSimilarConnectorParams<
* Function to transform the items passed to the templates.
*/
transformItems?: TransformItems<
AlgoliaHit<THit>,
Hit<THit>,
{ results: RecommendResponse<AlgoliaHit<THit>> }
>;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
Unmounter,
UnknownWidgetParams,
RecommendResponse,
Hit,
AlgoliaHit,
} from '../../types';
import type { PlainSearchParameters } from 'algoliasearch-helper';
Expand All @@ -33,7 +34,7 @@ export type RelatedProductsRenderState<
/**
* The matched recommendations from the Algolia API.
*/
items: Array<AlgoliaHit<THit>>;
items: Array<Hit<THit>>;

/**
* Sends an event to the Insights middleware.
Expand Down Expand Up @@ -80,7 +81,7 @@ export type RelatedProductsConnectorParams<
* Function to transform the items passed to the templates.
*/
transformItems?: TransformItems<
AlgoliaHit<THit>,
Hit<THit>,
{ results: RecommendResponse<AlgoliaHit<THit>> }
>;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
Unmounter,
UnknownWidgetParams,
RecommendResponse,
Hit,
AlgoliaHit,
} from '../../types';
import type { PlainSearchParameters } from 'algoliasearch-helper';
Expand All @@ -34,7 +35,7 @@ export type TrendingItemsRenderState<
/**
* The matched recommendations from the Algolia API.
*/
items: Array<AlgoliaHit<THit>>;
items: Array<Hit<THit>>;

/**
* Sends an event to the Insights middleware.
Expand Down Expand Up @@ -92,7 +93,7 @@ export type TrendingItemsConnectorParams<
* Function to transform the items passed to the templates.
*/
transformItems?: TransformItems<
AlgoliaHit<THit>,
Hit<THit>,
{ results: RecommendResponse<AlgoliaHit<THit>> }
>;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import type { PreparedTemplateProps } from '../../lib/templating';
import type {
Template,
WidgetFactory,
AlgoliaHit,
Renderer,
BaseHit,
RecommendResponse,
Expand Down Expand Up @@ -83,9 +82,9 @@ const renderer =
/>
)
: undefined
) as FrequentlyBoughtTogetherUiProps<AlgoliaHit>['headerComponent'];
) as FrequentlyBoughtTogetherUiProps<Hit>['headerComponent'];

const itemComponent: FrequentlyBoughtTogetherUiProps<AlgoliaHit>['itemComponent'] =
const itemComponent: FrequentlyBoughtTogetherUiProps<Hit>['itemComponent'] =
templates.item
? ({ item, sendEvent: _sendEvent, ...rootProps }) => {
return (
Expand All @@ -112,7 +111,7 @@ const renderer =
/>
)
: undefined
) as FrequentlyBoughtTogetherUiProps<AlgoliaHit>['emptyComponent'];
) as FrequentlyBoughtTogetherUiProps<Hit>['emptyComponent'];

const layoutComponent = (
templates.layout
Expand All @@ -126,7 +125,7 @@ const renderer =
items: data.items,
templates: {
item: templates.item
? ({ item }: { item: AlgoliaHit<THit> }) => (
? ({ item }: { item: Hit<THit> }) => (
<TemplateComponent
{...renderState.templateProps}
templateKey="item"
Expand All @@ -146,7 +145,7 @@ const renderer =
/>
)
: undefined
) as FrequentlyBoughtTogetherUiProps<AlgoliaHit<THit>>['layout'];
) as FrequentlyBoughtTogetherUiProps<Hit<THit>>['layout'];

render(
<FrequentlyBoughtTogether
Expand All @@ -171,7 +170,7 @@ export type FrequentlyBoughtTogetherTemplates<
/**
* Template to use when there are no results.
*/
empty: Template<RecommendResponse<AlgoliaHit<THit>>>;
empty: Template<RecommendResponse<Hit<THit>>>;

/**
* Template to use for the header of the widget.
Expand All @@ -180,7 +179,7 @@ export type FrequentlyBoughtTogetherTemplates<
Pick<
Parameters<
NonNullable<
FrequentlyBoughtTogetherUiProps<AlgoliaHit<THit>>['headerComponent']
FrequentlyBoughtTogetherUiProps<Hit<THit>>['headerComponent']
>
>[0],
'items'
Expand All @@ -190,22 +189,20 @@ export type FrequentlyBoughtTogetherTemplates<
/**
* Template to use for each result. This template will receive an object containing a single record.
*/
item: TemplateWithBindEvent<AlgoliaHit<THit>>;
item: TemplateWithBindEvent<Hit<THit>>;

/**
* Template to use to wrap all items.
*/
layout: Template<
Pick<
Parameters<
NonNullable<FrequentlyBoughtTogetherUiProps<AlgoliaHit<THit>>['layout']>
NonNullable<FrequentlyBoughtTogetherUiProps<Hit<THit>>['layout']>
>[0],
'items'
> & {
templates: {
item: FrequentlyBoughtTogetherUiProps<
AlgoliaHit<THit>
>['itemComponent'];
item: FrequentlyBoughtTogetherUiProps<Hit<THit>>['itemComponent'];
};
cssClasses: Pick<FrequentlyBoughtTogetherCSSClasses, 'list' | 'item'>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import type { PreparedTemplateProps } from '../../lib/templating';
import type {
Template,
WidgetFactory,
AlgoliaHit,
Renderer,
BaseHit,
RecommendResponse,
Expand Down Expand Up @@ -82,9 +81,9 @@ function createRenderer<THit extends NonNullable<object> = BaseHit>({
/>
)
: undefined
) as LookingSimilarUiProps<AlgoliaHit>['headerComponent'];
) as LookingSimilarUiProps<Hit>['headerComponent'];

const itemComponent: LookingSimilarUiProps<AlgoliaHit>['itemComponent'] =
const itemComponent: LookingSimilarUiProps<Hit>['itemComponent'] =
templates.item
? ({ item, sendEvent: _sendEvent, ...rootProps }) => {
return (
Expand All @@ -111,7 +110,7 @@ function createRenderer<THit extends NonNullable<object> = BaseHit>({
/>
)
: undefined
) as LookingSimilarUiProps<AlgoliaHit>['emptyComponent'];
) as LookingSimilarUiProps<Hit>['emptyComponent'];

const layoutComponent = (
templates.layout
Expand All @@ -125,7 +124,7 @@ function createRenderer<THit extends NonNullable<object> = BaseHit>({
items: data.items,
templates: {
item: templates.item
? ({ item }: { item: AlgoliaHit<THit> }) => (
? ({ item }: { item: Hit<THit> }) => (
<TemplateComponent
{...renderState.templateProps}
templateKey="item"
Expand All @@ -145,7 +144,7 @@ function createRenderer<THit extends NonNullable<object> = BaseHit>({
/>
)
: undefined
) as LookingSimilarUiProps<AlgoliaHit<THit>>['layout'];
) as LookingSimilarUiProps<Hit<THit>>['layout'];

render(
<LookingSimilar
Expand All @@ -171,15 +170,15 @@ export type LookingSimilarTemplates<
/**
* Template to use when there are no results.
*/
empty: Template<RecommendResponse<AlgoliaHit<THit>>>;
empty: Template<RecommendResponse<Hit<THit>>>;

/**
* Template to use for the header of the widget.
*/
header: Template<
Pick<
Parameters<
NonNullable<LookingSimilarUiProps<AlgoliaHit<THit>>['headerComponent']>
NonNullable<LookingSimilarUiProps<Hit<THit>>['headerComponent']>
>[0],
'items'
> & { cssClasses: RecommendClassNames }
Expand All @@ -188,20 +187,18 @@ export type LookingSimilarTemplates<
/**
* Template to use for each result. This template will receive an object containing a single record.
*/
item: TemplateWithBindEvent<AlgoliaHit<THit>>;
item: TemplateWithBindEvent<Hit<THit>>;

/**
* Template to use to wrap all items.
*/
layout: Template<
Pick<
Parameters<
NonNullable<LookingSimilarUiProps<AlgoliaHit<THit>>['layout']>
>[0],
Parameters<NonNullable<LookingSimilarUiProps<Hit<THit>>['layout']>>[0],
'items'
> & {
templates: {
item: LookingSimilarUiProps<AlgoliaHit<THit>>['itemComponent'];
item: LookingSimilarUiProps<Hit<THit>>['itemComponent'];
};
cssClasses: Pick<LookingSimilarCSSClasses, 'list' | 'item'>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ import type { PreparedTemplateProps } from '../../lib/templating';
import type {
Template,
WidgetFactory,
AlgoliaHit,
Hit,
Renderer,
BaseHit,
RecommendResponse,
Hit,
TemplateWithBindEvent,
} from '../../types';
import type {
Expand Down Expand Up @@ -89,9 +88,9 @@ function createRenderer<THit extends NonNullable<object> = BaseHit>({
/>
)
: undefined
) as RelatedProductsUiProps<AlgoliaHit>['headerComponent'];
) as RelatedProductsUiProps<Hit>['headerComponent'];

const itemComponent: RelatedProductsUiProps<AlgoliaHit>['itemComponent'] =
const itemComponent: RelatedProductsUiProps<Hit>['itemComponent'] =
templates.item
? ({ item, sendEvent: _sendEvent, ...rootProps }) => {
return (
Expand All @@ -118,7 +117,7 @@ function createRenderer<THit extends NonNullable<object> = BaseHit>({
/>
)
: undefined
) as RelatedProductsUiProps<AlgoliaHit>['emptyComponent'];
) as RelatedProductsUiProps<Hit>['emptyComponent'];

const layoutComponent = (
templates.layout
Expand All @@ -132,7 +131,7 @@ function createRenderer<THit extends NonNullable<object> = BaseHit>({
items: data.items,
templates: {
item: templates.item
? ({ item }: { item: AlgoliaHit<THit> }) => (
? ({ item }: { item: Hit<THit> }) => (
<TemplateComponent
{...renderState.templateProps}
templateKey="item"
Expand All @@ -152,7 +151,7 @@ function createRenderer<THit extends NonNullable<object> = BaseHit>({
/>
)
: undefined
) as RelatedProductsUiProps<AlgoliaHit<THit>>['layout'];
) as RelatedProductsUiProps<Hit<THit>>['layout'];

render(
<RelatedProducts
Expand All @@ -178,15 +177,15 @@ export type RelatedProductsTemplates<
/**
* Template to use when there are no results.
*/
empty: Template<RecommendResponse<AlgoliaHit<THit>>>;
empty: Template<RecommendResponse<Hit<THit>>>;

/**
* Template to use for the header of the widget.
*/
header: Template<
Pick<
Parameters<
NonNullable<RelatedProductsUiProps<AlgoliaHit<THit>>['headerComponent']>
NonNullable<RelatedProductsUiProps<Hit<THit>>['headerComponent']>
>[0],
'items'
> & { cssClasses: RecommendClassNames }
Expand All @@ -195,20 +194,18 @@ export type RelatedProductsTemplates<
/**
* Template to use for each result. This template will receive an object containing a single record.
*/
item: TemplateWithBindEvent<AlgoliaHit<THit>>;
item: TemplateWithBindEvent<Hit<THit>>;

/**
* Template to use to wrap all items.
*/
layout: Template<
Pick<
Parameters<
NonNullable<RelatedProductsUiProps<AlgoliaHit<THit>>['layout']>
>[0],
Parameters<NonNullable<RelatedProductsUiProps<Hit<THit>>['layout']>>[0],
'items'
> & {
templates: {
item: RelatedProductsUiProps<AlgoliaHit<THit>>['itemComponent'];
item: RelatedProductsUiProps<Hit<THit>>['itemComponent'];
};
cssClasses: Pick<RelatedProductsCSSClasses, 'list' | 'item'>;
}
Expand Down
Loading

0 comments on commit 6f2dc2f

Please sign in to comment.