Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-recommendations/src/Recommendations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
UseRecommendationsProps,
} from './useRecommendations';

export type RecommendationsProps<TObject> = UseRecommendationsProps & {
export type RecommendationsProps<TObject> = UseRecommendationsProps<TObject> & {
hitComponent: React.FunctionComponent<{ hit: TObject }>;
children?(props: {
recommendations: TObject[];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { UseRecommendationsProps } from '../useRecommendations';

export type UseRecommendationsInternalProps = Required<UseRecommendationsProps>;
export type UseRecommendationsInternalProps<TObject> = Required<
UseRecommendationsProps<TObject>
>;
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type UseFrequentlyBoughtTogetherProps = {
export function useFrequentlyBoughtTogether<TObject extends ProductBaseRecord>(
userProps: UseFrequentlyBoughtTogetherProps
) {
const props: UseRecommendationsProps = useMemo(
const props: UseRecommendationsProps<TObject> = useMemo(
() => ({
...userProps,
fallbackFilters: [],
Expand Down
14 changes: 8 additions & 6 deletions packages/react-recommendations/src/useRecommendations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
uniqBy,
} from './utils';

export type UseRecommendationsProps = {
export type UseRecommendationsProps<TObject> = {
model: RecommendationModel;
indexName: string;
objectIDs: string[];
Expand All @@ -26,15 +26,16 @@ export type UseRecommendationsProps = {
maxRecommendations?: number;
searchParameters?: SearchOptions;
threshold?: number;
transformItems?: (items: TObject[]) => TObject[];
};

type UseRecommendationReturn<TObject> = {
recommendations: TObject[];
};

function getDefaultedProps(
props: UseRecommendationsProps
): UseRecommendationsInternalProps {
function getDefaultedProps<TObject extends ProductBaseRecord>(
props: UseRecommendationsProps<TObject>
): UseRecommendationsInternalProps<TObject> {
return {
fallbackFilters: [],
maxRecommendations: 0,
Expand All @@ -53,12 +54,13 @@ function getDefaultedProps(
...props.searchParameters,
},
threshold: 0,
transformItems: (items) => items,
...props,
};
}

export function useRecommendations<TObject extends ProductBaseRecord>(
userProps: UseRecommendationsProps
userProps: UseRecommendationsProps<TObject>
): UseRecommendationReturn<TObject> {
const [products, setProducts] = useState<Array<ProductRecord<TObject>>>([]);
const props = useMemo(() => getDefaultedProps(userProps), [userProps]);
Expand Down Expand Up @@ -143,7 +145,7 @@ export function useRecommendations<TObject extends ProductBaseRecord>(
: undefined
);

setProducts(hits);
setProducts(props.transformItems(hits));
});
})
.catch(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-recommendations/src/useRelatedProducts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type UseRelatedProductsProps = {
export function useRelatedProducts<TObject extends ProductBaseRecord>(
userProps: UseRelatedProductsProps
) {
const props: UseRecommendationsProps = useMemo(
const props: UseRecommendationsProps<TObject> = useMemo(
() => ({
...userProps,
model: 'related-products',
Expand Down
12 changes: 6 additions & 6 deletions packages/react-recommendations/src/utils/getHitsPerPage.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { UseRecommendationsInternalProps } from '../types';
import { UseRecommendationsInternalProps, ProductBaseRecord } from '../types';

type GetHitsPerPageParams = {
fallbackFilters: UseRecommendationsInternalProps['fallbackFilters'];
maxRecommendations: UseRecommendationsInternalProps['maxRecommendations'];
type GetHitsPerPageParams<TObject> = {
fallbackFilters: UseRecommendationsInternalProps<TObject>['fallbackFilters'];
maxRecommendations: UseRecommendationsInternalProps<TObject>['maxRecommendations'];
recommendationsCount: number;
};

export function getHitsPerPage({
export function getHitsPerPage<TObject extends ProductBaseRecord>({
fallbackFilters,
maxRecommendations,
recommendationsCount,
}: GetHitsPerPageParams) {
}: GetHitsPerPageParams<TObject>) {
const hasFallback = fallbackFilters.length > 0;

if (recommendationsCount === 0) {
Expand Down
11 changes: 6 additions & 5 deletions packages/react-recommendations/src/utils/getOptionalFilters.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import {
UseRecommendationsInternalProps,
RecommendationRecord,
ProductBaseRecord,
} from '../types';

type GetOptionalFiltersParams = {
fallbackFilters: UseRecommendationsInternalProps['fallbackFilters'];
type GetOptionalFiltersParams<TObject> = {
fallbackFilters: UseRecommendationsInternalProps<TObject>['fallbackFilters'];
recommendations: RecommendationRecord[];
threshold: UseRecommendationsInternalProps['threshold'];
threshold: UseRecommendationsInternalProps<TObject>['threshold'];
};

export function getOptionalFilters({
export function getOptionalFilters<TObject extends ProductBaseRecord>({
fallbackFilters,
recommendations,
threshold,
}: GetOptionalFiltersParams) {
}: GetOptionalFiltersParams<TObject>) {
if (recommendations.length === 0) {
return fallbackFilters;
}
Expand Down