@algolia/recommend-react
Advanced tools
Comparing version 1.14.0 to 1.15.0
@@ -1,1 +0,1 @@ | ||
export declare const version = "1.14.0"; | ||
export declare const version = "1.15.0"; |
@@ -1,1 +0,1 @@ | ||
export const version = '1.14.0'; | ||
export const version = '1.15.0'; |
@@ -1,3 +0,3 @@ | ||
/*! @algolia/recommend-react 1.14.0 | MIT License | © Algolia, Inc. and contributors | https://github.com/algolia/recommend */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@algolia/recommend-react"]={},e.React)}(this,(function(e,t){"use strict";function n(){return n=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},n.apply(this,arguments)}function r(...e){return e.filter(Boolean).join(" ")}function a({createElement:e,Fragment:t}){return function(n){return 0===n.recommendations.length&&"idle"===n.status?e(n.Fallback,null):e("section",{className:r("auc-Recommend",n.classNames.root)},e(n.Header,{classNames:n.classNames,recommendations:n.recommendations,translations:n.translations,createElement:e,Fragment:t}),e(n.View,null))}}function s({createElement:e}){return function(t){return!t.recommendations||t.recommendations.length<1?null:t.translations.title?e("h3",{className:r("auc-Recommend-title",t.classNames.title)},t.translations.title):null}}function o({createElement:e,Fragment:t}){return function(n){return e("div",{className:r("auc-Recommend-container",n.classNames.container)},e("ol",{className:r("auc-Recommend-list",n.classNames.list)},n.items.map((a=>e("li",{key:a.objectID,className:r("auc-Recommend-item",n.classNames.item)},e(n.itemComponent,{createElement:e,Fragment:t,item:a}))))))}}function i({hits:e,maxRecommendations:t,nrOfObjs:n}){const r={};e.forEach((e=>{e.forEach(((e,t)=>{r[e.objectID]?r[e.objectID]={indexSum:r[e.objectID].indexSum+t,nr:r[e.objectID].nr+1}:r[e.objectID]={indexSum:t,nr:1}}))}));const a=((e,t)=>{const n=[];for(const r of Object.keys(e))e[r].nr<2&&(e[r].indexSum+=100),n.push({objectID:r,avgOfIndices:e[r].indexSum/t});return n.sort(((e,t)=>e.avgOfIndices>t.avgOfIndices?1:-1))})(r,n);return a.reduce(((t,n)=>{const r=e.flat().find((e=>e.objectID===n.objectID));return r?t.concat(r):t}),[]).slice(0,t&&t>0?t:void 0)}function m({hits:e,maxRecommendations:t}){return function(e,t){const n=[...t];return n.sort(e),n}(((e,t)=>(e._score||0)>(t._score||0)?-1:1),e).slice(0,t&&t>0?t:void 0)}const c="1.14.0";function l({objectIDs:e,recommendClient:t,transformItems:n=(e=>e),indexName:r,maxRecommendations:a,queryParameters:s,threshold:o}){const m=e.map((e=>({indexName:r,maxRecommendations:a,objectID:e,queryParameters:s,threshold:o})));return t.addAlgoliaAgent("recommend-core",c),0===m.length?Promise.resolve({recommendations:[]}):t.getFrequentlyBoughtTogether(m).then((t=>i({maxRecommendations:a,hits:t.results.map((e=>e.hits)),nrOfObjs:e.length}))).then((e=>({recommendations:n(e)})))}function u({objectIDs:e,recommendClient:t,transformItems:n=(e=>e),fallbackParameters:r,indexName:a,maxRecommendations:s,model:o,queryParameters:m,threshold:l}){const u=e.map((e=>({fallbackParameters:r,indexName:a,maxRecommendations:s,model:o,objectID:e,queryParameters:m,threshold:l})));return t.addAlgoliaAgent("recommend-core",c),0===u.length?Promise.resolve({recommendations:[]}):t.getRecommendations(u).then((t=>i({maxRecommendations:s,hits:t.results.map((e=>e.hits)),nrOfObjs:e.length}))).then((e=>({recommendations:n(e)})))}function d({objectIDs:e,recommendClient:t,transformItems:n=(e=>e),fallbackParameters:r,indexName:a,maxRecommendations:s,queryParameters:o,threshold:m}){const l=e.map((e=>({fallbackParameters:r,indexName:a,maxRecommendations:s,objectID:e,queryParameters:o,threshold:m})));return t.addAlgoliaAgent("recommend-core",c),0===l.length?Promise.resolve({recommendations:[]}):t.getRelatedProducts(l).then((t=>i({maxRecommendations:s,hits:t.results.map((e=>e.hits)),nrOfObjs:e.length}))).then((e=>({recommendations:n(e)})))}function f({objectIDs:e,recommendClient:t,transformItems:n=(e=>e),fallbackParameters:r,indexName:a,maxRecommendations:s,queryParameters:o,threshold:m}){const l=e.map((e=>({fallbackParameters:r,indexName:a,maxRecommendations:s,objectID:e,queryParameters:o,threshold:m})));return t.addAlgoliaAgent("recommend-core",c),0===l.length?Promise.resolve({recommendations:[]}):t.getLookingSimilar(l).then((t=>i({maxRecommendations:s,hits:t.results.map((e=>e.hits)),nrOfObjs:e.length}))).then((e=>({recommendations:n(e)})))}function g({recommendClient:e,transformItems:t=(e=>e),fallbackParameters:n,indexName:r,maxRecommendations:a,queryParameters:s,threshold:o,facetName:i,facetValue:l}){const u={fallbackParameters:n,indexName:r,maxRecommendations:a,queryParameters:s,threshold:o,facetName:i,facetValue:l};return e.addAlgoliaAgent("recommend-core",c),r?e.getTrendingItems([u]).then((e=>{return m({maxRecommendations:a,hits:(t="objectID",n=e.results.map((e=>e.hits)).flat(),[...new Map(n.map((e=>[e[t],e]))).values()])});var t,n})).then((e=>({recommendations:t(e)}))):Promise.resolve({recommendations:[]})}const p=async({userToken:e,region:t,apiKey:n,appId:r})=>{const a=await fetch(`https://personalization.${t}.algolia.com/1/profiles/personalization/${encodeURIComponent(e)}`,{headers:{"Content-Type":"application/json","X-Algolia-Application-Id":r,"X-Algolia-API-Key":n}});if(!a.ok)throw new Error(`Failed to fetch personalization affinities. Status: ${a.status}`);return await a.json()},h=async({region:e,apiKey:t,appId:n})=>{const r=await fetch(`https://personalization.${e}.algolia.com/1/strategies/personalization`,{headers:{"Content-Type":"application/json","X-Algolia-Application-Id":n,"X-Algolia-API-Key":t}});if(!r.ok)throw new Error(`Failed to fetch personalization strategy. Status: ${r.status}`);return await r.json()},y=async({userToken:e,region:t,apiKey:n,appId:r})=>{if(!t)throw new Error("[Algolia Recommend] parameter `region` is required to enable personalization.");if(!e)return console.warn("[Algolia Recommend] Personalization couldn't be enabled because `userToken` is missing. Falling back to non-personalized recommendations."),[];try{const[a,s]=await Promise.all([p({userToken:e,apiKey:n,appId:r,region:t}),h({apiKey:n,appId:r,region:t})]),o=100,i=new Map(s.facetsScoring.map((e=>[e.facetName,e.score])));return Object.entries(a.scores).flatMap((([e,t])=>Object.entries(t).map((([t,n])=>{const r=i.get(e)??o,a=Math.floor(n*(r/100));return`${e}:${t}<score=${a}>`}))))}catch(e){const t=e instanceof Error?e.message:String(e);return console.error(`[Algolia Recommend] Personalization couldn't be enabled. Falling back to non-personalized recommendations. Error: ${t}`),[]}},k=e=>void 0!==e.region&&void 0!==e.userToken,x=e=>k(e)?{region:e.region,userToken:e.userToken,suppressExperimentalWarning:Boolean(e.suppressExperimentalWarning)}:{region:void 0,userToken:void 0,suppressExperimentalWarning:Boolean(e.suppressExperimentalWarning)};function P(e){t.useEffect((()=>{e.recommendClient.addAlgoliaAgent("recommend-react","1.14.0")}),[e.recommendClient])}var b=Object.prototype.hasOwnProperty;function E(e,t){var n,r;if(e===t)return!0;if(e&&t&&(n=e.constructor)===t.constructor){if(n===Date)return e.getTime()===t.getTime();if(n===RegExp)return e.toString()===t.toString();if(n===Array){if((r=e.length)===t.length)for(;r--&&E(e[r],t[r]););return-1===r}if(!n||"object"==typeof e){for(n in r=0,e){if(b.call(e,n)&&++r&&!b.call(t,n))return!1;if(!(n in t)||!E(e[n],t[n]))return!1}return Object.keys(t).length===r}}return e!=e&&t!=t}function R(e){const[n,r]=t.useState((()=>e));return t.useEffect((()=>{!1===E(n,e)&&r(e)}),[e]),n}function C(e){const n=t.useRef(void 0),[r,a]=t.useState(e);return t.useEffect((()=>{"stalled"!==r&&n.current&&clearTimeout(n.current),"loading"===r&&(n.current=setTimeout((()=>{a("stalled")}),300))}),[r]),{status:r,setStatus:a}}const F=(e,n)=>{t.useEffect((()=>{}),[e,n])},N=t.createContext({hasProvider:!1}),I=()=>t.useContext(N),q=e=>{const n=t.useContext(N);if(e)return{client:e,isContextClient:!1};if(n.recommendClient)return{client:n.recommendClient,isContextClient:!0};throw new Error("Pass an Algolia `recommendClient` instance either to the Recommend React context, a component or hook.")};function T({indexName:e,maxRecommendations:n,objectIDs:r,queryParameters:a,recommendClient:s,threshold:o,transformItems:i=(e=>e),...m}){const{userToken:c,region:u,suppressExperimentalWarning:d}=x(m),[f,g]=t.useState({recommendations:[]}),{status:p,setStatus:h}=C("loading"),k=R(r),b=R(a),{hasProvider:E,register:N}=I(),{client:T,isContextClient:j}=q(s);P({recommendClient:T});const v=t.useRef(i);return t.useEffect((()=>{v.current=i}),[i]),F(d,"FrequentlyBoughtTogether"),t.useEffect((()=>{const t={indexName:e,maxRecommendations:n,objectIDs:k,queryParameters:b,threshold:o,transformItems:v.current};if(E&&j){const r=JSON.stringify(t),a=k.map((t=>({indexName:e,model:"bought-together",threshold:o,maxRecommendations:n,objectID:t,queryParameters:b,userToken:c,region:u})));return N({key:r,getParameters:()=>({queries:a,keyPair:{key:r,value:k.length}}),onRequest(){h("loading")},onResult(e){g(e),h("idle")}})}return h("loading"),c&&u?(T.addAlgoliaAgent("experimental-personalization"),y({apiKey:T.transporter.queryParameters["x-algolia-api-key"],appId:T.appId,region:u,userToken:c}).then((e=>{l({...t,queryParameters:{...t.queryParameters,optionalFilters:[...e,...t.queryParameters?.optionalFilters??[]]},recommendClient:T}).then((e=>{g(e),h("idle")}))}))):l({...t,recommendClient:T}).then((e=>{g(e),h("idle")})),()=>{}}),[e,n,k,b,T,h,o,E,j,N,c,u]),{...f,status:p}}const j=function({createElement:e,Fragment:t}){return function(r){const i={title:"Frequently bought together",sliderLabel:"Frequently bought together products",...r.translations},m=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??o({createElement:e,Fragment:t});return c({classNames:m,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:i,View:a=>e(d,n({classNames:m,itemComponent:r.itemComponent,items:r.items,translations:i,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});function v({fallbackParameters:e,indexName:n,maxRecommendations:r,objectIDs:a,queryParameters:s,recommendClient:o,threshold:i,transformItems:m=(e=>e),...c}){const{userToken:l,region:u,suppressExperimentalWarning:f}=x(c),[g,p]=t.useState({recommendations:[]}),{status:h,setStatus:k}=C("loading"),b=R(a),E=R(s),N=R(e),{hasProvider:T,register:j}=I(),{client:v,isContextClient:w}=q(o);P({recommendClient:v});const A=t.useRef(m);return t.useEffect((()=>{A.current=m}),[m]),F(f,"RelatedProducts"),t.useEffect((()=>{const e={fallbackParameters:N,indexName:n,maxRecommendations:r,objectIDs:b,queryParameters:E,threshold:i,transformItems:A.current};if(T&&w){const t=JSON.stringify(e),a=b.map((e=>({indexName:n,model:"related-products",threshold:i,maxRecommendations:r,objectID:e,queryParameters:E,fallbackParameters:N,userToken:l,region:u})));return j({key:t,getParameters:()=>({queries:a,keyPair:{key:t,value:b.length}}),onRequest(){k("loading")},onResult(e){p(e),k("idle")}})}return k("loading"),l&&u?(v.addAlgoliaAgent("experimental-personalization"),y({apiKey:v.transporter.queryParameters["x-algolia-api-key"],appId:v.appId,region:u,userToken:l}).then((t=>{d({...e,queryParameters:{...e.queryParameters,optionalFilters:[...t,...e.queryParameters?.optionalFilters??[]]},recommendClient:v}).then((e=>{p(e),k("idle")}))}))):d({...e,recommendClient:v}).then((e=>{p(e),k("idle")})),()=>{}}),[v,N,T,n,w,r,b,E,j,k,i,l,u]),{...g,status:h}}const w=function({createElement:e,Fragment:t}){return function(r){const i={title:"Related products",sliderLabel:"Related products",...r.translations},m=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??o({createElement:e,Fragment:t});return c({classNames:m,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:i,View:a=>e(d,n({classNames:m,itemComponent:r.itemComponent,items:r.items,translations:i,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});function A({fallbackParameters:e,indexName:n,maxRecommendations:r,objectIDs:a,queryParameters:s,recommendClient:o,threshold:i,transformItems:m=(e=>e),...c}){const{userToken:l,region:u,suppressExperimentalWarning:d}=x(c),[g,p]=t.useState({recommendations:[]}),{status:h,setStatus:k}=C("loading"),b=R(a),E=R(s),N=R(e),{hasProvider:T,register:j}=I(),{client:v,isContextClient:w}=q(o);P({recommendClient:v});const A=t.useRef(m);return t.useEffect((()=>{A.current=m}),[m]),F(d,"LookingSimilar"),t.useEffect((()=>{const e={fallbackParameters:N,indexName:n,maxRecommendations:r,objectIDs:b,queryParameters:E,threshold:i,transformItems:A.current};if(T&&w){const t=JSON.stringify(e),a=b.map((e=>({indexName:n,model:"looking-similar",threshold:i,maxRecommendations:r,objectID:e,queryParameters:E,fallbackParameters:N,userToken:l,region:u})));return j({key:t,getParameters:()=>({queries:a,keyPair:{key:t,value:b.length}}),onRequest(){k("loading")},onResult(e){p(e),k("idle")}})}return k("loading"),l&&u?(v.addAlgoliaAgent("experimental-personalization"),y({apiKey:v.transporter.queryParameters["x-algolia-api-key"],appId:v.appId,region:u,userToken:l}).then((t=>{f({...e,queryParameters:{...e.queryParameters,optionalFilters:[...t,...e.queryParameters?.optionalFilters??[]]},recommendClient:v}).then((e=>{p(e),k("idle")}))}))):f({...e,recommendClient:v}).then((e=>{p(e),k("idle")})),()=>{}}),[v,N,T,n,w,r,b,E,j,k,i,l,u]),{...g,status:h}}const S=function({createElement:e,Fragment:t}){return function(r){const i={title:"Looking Similar",sliderLabel:"Looking Similar",...r.translations},m=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??o({createElement:e,Fragment:t});return c({classNames:m,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:i,View:a=>e(d,n({classNames:m,itemComponent:r.itemComponent,items:r.items,translations:i,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});function D({fallbackParameters:e,indexName:n,maxRecommendations:r,queryParameters:a,recommendClient:s,threshold:o,facetName:i,facetValue:m,transformItems:c=(e=>e),...l}){const{userToken:u,region:d,suppressExperimentalWarning:f}=x(l),[p,h]=t.useState({recommendations:[]}),{status:k,setStatus:b}=C("loading"),E=R(a),N=R(e),{hasProvider:T,register:j}=I(),{client:v,isContextClient:w}=q(s);P({recommendClient:v});const A=t.useRef(c);return t.useEffect((()=>{A.current=c}),[c]),F(f,"TrendingItems"),t.useEffect((()=>{const e={model:"trending-items",fallbackParameters:N,indexName:n,maxRecommendations:r,queryParameters:E,threshold:o,facetName:i,facetValue:m,transformItems:A.current};if(T&&w){const t=[{...e,userToken:u,region:d}],n=JSON.stringify(e);return j({key:n,getParameters:()=>({queries:t,keyPair:{key:n,value:1}}),onRequest(){b("loading")},onResult(e){h(e),b("idle")}})}return b("loading"),u&&d?(v.addAlgoliaAgent("experimental-personalization"),y({apiKey:v.transporter.queryParameters["x-algolia-api-key"],appId:v.appId,region:d,userToken:u}).then((t=>{g({...e,queryParameters:{...e.queryParameters,optionalFilters:[...t,...e.queryParameters?.optionalFilters??[]]},recommendClient:v}).then((e=>{h(e),b("idle")}))}))):g({...e,recommendClient:v,transformItems:A.current}).then((e=>{h(e),b("idle")})),()=>{}}),[N,n,r,E,v,b,o,i,m,T,w,j,u,d]),{...p,status:k}}const O=function({createElement:e,Fragment:t}){return function(r){const i={title:"Trending items",sliderLabel:"Trending items",...r.translations},m=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??o({createElement:e,Fragment:t});return c({classNames:m,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:i,View:a=>e(d,n({classNames:m,itemComponent:r.itemComponent,items:r.items,translations:i,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});var z=Object.prototype.hasOwnProperty;function L(e,t,n){for(n of e.keys())if(V(n,t))return n}function V(e,t){var n,r,a;if(e===t)return!0;if(e&&t&&(n=e.constructor)===t.constructor){if(n===Date)return e.getTime()===t.getTime();if(n===RegExp)return e.toString()===t.toString();if(n===Array){if((r=e.length)===t.length)for(;r--&&V(e[r],t[r]););return-1===r}if(n===Set){if(e.size!==t.size)return!1;for(r of e){if((a=r)&&"object"==typeof a&&!(a=L(t,a)))return!1;if(!t.has(a))return!1}return!0}if(n===Map){if(e.size!==t.size)return!1;for(r of e){if((a=r[0])&&"object"==typeof a&&!(a=L(t,a)))return!1;if(!V(r[1],t.get(a)))return!1}return!0}if(n===ArrayBuffer)e=new Uint8Array(e),t=new Uint8Array(t);else if(n===DataView){if((r=e.byteLength)===t.byteLength)for(;r--&&e.getInt8(r)===t.getInt8(r););return-1===r}if(ArrayBuffer.isView(e)){if((r=e.byteLength)===t.byteLength)for(;r--&&e[r]===t[r];);return-1===r}if(!n||"object"==typeof e){for(n in r=0,e){if(z.call(e,n)&&++r&&!z.call(t,n))return!1;if(!(n in t)||!V(e[n],t[n]))return!1}return Object.keys(t).length===r}}return e!=e&&t!=t}function K(e){return null!=e}function $(e){return JSON.stringify(e)}function _(e,t){return e.map((e=>{if(function(e,t){return Boolean(e[$(t)])}(t,e.param.queries)){const n=t[$(e.param.queries)];return e.onResult(n),null}return e.param})).filter(K)}const B=(e,t)=>{const{type:n}=t;switch(n){case"register":{const{widget:n}=t,r=n.getParameters(),a=function(e,t){return Boolean(e.find((e=>V(e.param,t))))}(e.widgets,r);return a?e:{...e,widgets:[...e.widgets,{...n,param:r}],isDirty:Date.now()}}case"unregister":{const{key:n}=t;return{...e,widgets:e.widgets.filter((e=>e.key!==n))}}case"request_success":return{...e,status:"idle",isDirty:null};default:return e}};function W({fallbackParameters:e,indexName:n,maxRecommendations:r,queryParameters:a,recommendClient:s,threshold:o,transformItems:i=(e=>e)}){const[m,l]=t.useState({recommendations:[]}),{status:u,setStatus:d}=C("loading"),f=R(a),g=R(e),{hasProvider:p,register:h}=I(),{client:y,isContextClient:k}=q(s);P({recommendClient:y});const x=t.useRef(i);return t.useEffect((()=>{x.current=i}),[i]),t.useEffect((()=>{const e={fallbackParameters:g,indexName:n,maxRecommendations:r,queryParameters:f,threshold:o,transformItems:x.current};if(p&&k){const t=JSON.stringify(e),a=[{indexName:n,threshold:o,maxRecommendations:r,queryParameters:f,fallbackParameters:g,model:"recommended-for-you"}];return h({key:t,getParameters:()=>({queries:a,keyPair:{key:t,value:1}}),onRequest(){d("loading")},onResult(e){l(e),d("idle")}})}return d("loading"),function({indexName:e,threshold:t,queryParameters:n,recommendClient:r,maxRecommendations:a,transformItems:s=(e=>e)}){r.addAlgoliaAgent("recommend-core",c);const o=[{indexName:e,threshold:t,queryParameters:n,maxRecommendations:a}];return r.getRecommendedForYou(o).then((e=>({recommendations:s(e.results.map((e=>e.hits)).flat())})))}({...e,recommendClient:y}).then((e=>{l(e),d("idle")})),()=>{}}),[y,g,p,n,k,r,f,h,d,o]),{...m,status:u}}const J=function({createElement:e,Fragment:t}){return function(r){const i={title:"Recommended For You",sliderLabel:"Recommended For You",...r.translations},m=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??o({createElement:e,Fragment:t});return c({classNames:m,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:i,View:a=>e(d,n({classNames:m,itemComponent:r.itemComponent,items:r.items,translations:i,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});function H({indexName:e,maxRecommendations:n,recommendClient:r,threshold:a,transformItems:s=(e=>e),facetName:o}){const[i,l]=t.useState({recommendations:[]}),{status:u,setStatus:d}=C("loading"),{hasProvider:f,register:g}=I(),{client:p,isContextClient:h}=q(r);P({recommendClient:p});const y=t.useRef(s);return t.useEffect((()=>{y.current=s}),[s]),t.useEffect((()=>{const t={model:"trending-facets",indexName:e,facetName:o,threshold:a,maxRecommendations:n,transformItems:y.current};if(f&&h){const e=JSON.stringify(t);return g({key:e,getParameters:()=>({queries:[t],keyPair:{key:e,value:1}}),onRequest(){d("loading")},onResult(e){l(e),d("idle")}})}return d("loading"),function({recommendClient:e,transformItems:t=(e=>e),indexName:n,maxRecommendations:r,threshold:a,facetName:s}){const o={indexName:n,maxRecommendations:r,threshold:a,facetName:s};return e.addAlgoliaAgent("recommend-core",c),e.getTrendingFacets([o]).then((e=>m({maxRecommendations:r,hits:e.results.map((e=>e.hits)).flat()}))).then((e=>({recommendations:t(e)})))}({...t,recommendClient:p,facetName:o,transformItems:y.current}).then((e=>{l(e),d("idle")})),()=>{}}),[e,n,p,d,a,o,f,h,g]),{...i,status:u}}const M=function({createElement:e,Fragment:t}){return function(o){const i={title:"Trending facets",sliderLabel:"Trending facets",...o.translations},m=o.classNames??{},c=o.children??a({createElement:e,Fragment:t}),l=o.fallbackComponent??function(){return null},u=o.headerComponent??s({createElement:e,Fragment:t}),d=o.view??function({createElement:e,Fragment:t}){return function(n){return e("div",{className:r("auc-Recommend-container",n.classNames.container)},e("ol",{className:r("auc-Recommend-list",n.classNames.list)},n.items.map((a=>e("li",{key:a.facetValue,className:r("auc-Recommend-item",n.classNames.item)},e(n.itemComponent,{createElement:e,Fragment:t,item:a}))))))}}({createElement:e,Fragment:t});return c({classNames:m,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:o.items,status:o.status,translations:i,View:r=>e(d,n({classNames:m,itemComponent:o.itemComponent,items:o.items,translations:i,Fragment:t,createElement:e},r))})}}({createElement:t.createElement,Fragment:t.Fragment});e.FrequentlyBoughtTogether=function(e){const{recommendations:r,status:a}=T(e);return t.createElement(j,n({},e,{items:r,status:a}))},e.LookingSimilar=function(e){const{recommendations:r,status:a}=A(e);return t.createElement(S,n({},e,{items:r,status:a}))},e.Recommend=function({recommendClient:e,children:n,...r}){const{userToken:a,region:s,suppressExperimentalWarning:o}=x(r),[l,u]=t.useReducer(B,{isDirty:null,cache:{},widgets:[],recommendClient:e});F(o,"<Recommend>"),t.useEffect((()=>{(async()=>{if(!l.isDirty)return;const t=_(l.widgets,l.cache);if(0===t.length)return;const{queries:n,keys:r}=function(e){return{queries:e.map((e=>e.queries)).reduce(((e,t)=>e.concat(t)),[]),keys:e.map((e=>e.keyPair))}}(t);r.forEach((e=>{l.widgets.forEach((t=>{t.key===e.key&&t.onRequest()}))}));const o=n.map((e=>k(e)?{userToken:e.userToken,region:e.region}:null)).filter(K);a&&s&&o.push({userToken:a,region:s});const d=Array.from(new Set(o)),f=await(async(e,t,n)=>(await Promise.all(n.map((({userToken:n,region:r})=>y({appId:e,apiKey:t,userToken:n,region:r}))))).reduce(((e,t,r)=>{const{userToken:a,region:s}=n[r];return e[`${s}_${a}`]=t,e}),{}))(e.appId,e.transporter.queryParameters["x-algolia-api-key"],d),g=n.map((e=>{if((e=>"trending-facets"===e.model)(e)||(e=>"recommended-for-you"===e.model)(e))return e;const{_userToken:t,_region:n}=k(e)?{_userToken:e.userToken,_region:e.region}:{_userToken:a,_region:s};if(!t||!n)return{...e,userToken:void 0,region:void 0};const r=f[`${n}_${t}`];return r?{...e,userToken:void 0,region:void 0,queryParameters:{...e.queryParameters,optionalFilters:[...r,...e.queryParameters?.optionalFilters??[]]}}:{...e,userToken:void 0,region:void 0}}));Object.keys(f).length>0&&e.addAlgoliaAgent("experimental-personalization");const p=await async function({keys:e,queries:t,recommendClient:n}){n.addAlgoliaAgent("recommend-core",c);const r=await n.getRecommendations(t);let a=0,s=0;const o={};return e.forEach((e=>{const{model:n}=JSON.parse(e.key);s+=e.value;const{maxRecommendations:c,transformItems:l=(e=>e)}=t[a],u=r?.results?.slice(a,s);a+=e.value;let d=[],f=[];"trending-facets"===n?f=u.map((e=>e.hits)).flat():d="trending-items"===n?m({maxRecommendations:c,hits:u.map((e=>e.hits)).flat()}):i({maxRecommendations:c,hits:u.map((e=>e.hits)),nrOfObjs:e.value}),d=l(d),o[e.key]={recommendations:d,trendingFacets:f}})),o}({recommendClient:e,queries:g,keys:r});Object.entries(p).forEach((([e,t])=>{l.widgets.forEach((n=>{n.key===e&&(n.onResult(t),l.cache[$(n.param.queries)]=t)}))})),u({type:"request_success"})})()}),[l.isDirty,u,l.widgets,l.cache,e,a,s]);const d=t.useCallback((e=>(u({type:"register",widget:e}),()=>{u({type:"unregister",key:e.key})})),[]),f=t.useMemo((()=>({register:d,recommendClient:e,hasProvider:!0})),[e,d]);return t.createElement(N.Provider,{value:f},n)},e.RecommendedForYou=function(e){const{recommendations:r,status:a}=W(e);return t.createElement(J,n({},e,{items:r,status:a}))},e.RelatedProducts=function(e){const{recommendations:r,status:a}=v(e);return t.createElement(w,n({},e,{items:r,status:a}))},e.TrendingFacets=function(e){const{recommendations:r,status:a}=H(e);return t.createElement(M,n({},e,{items:r,status:a}))},e.TrendingItems=function(e){const{recommendations:r,status:a}=D(e);return t.createElement(O,n({},e,{items:r,status:a}))},e.useFrequentlyBoughtTogether=T,e.useLookingSimilar=A,e.useRecommendations=function({fallbackParameters:e,indexName:n,maxRecommendations:r,model:a,objectIDs:s,queryParameters:o,recommendClient:i,threshold:m,transformItems:c=(e=>e),...l}){const{userToken:d,region:f,suppressExperimentalWarning:g}=x(l),[p,h]=t.useState({recommendations:[]}),{status:k,setStatus:b}=C("loading"),E=R(s),N=R(o),I=R(e);P({recommendClient:i});const q=t.useRef(c);return t.useEffect((()=>{q.current=c}),[c]),F(g,"useRecommendations"),t.useEffect((()=>{b("loading"),d&&f?(i.addAlgoliaAgent("experimental-personalization"),y({apiKey:i.transporter.queryParameters["x-algolia-api-key"],appId:i.appId,region:f,userToken:d}).then((e=>{u({fallbackParameters:I,indexName:n,maxRecommendations:r,model:a,objectIDs:E,queryParameters:{...N,optionalFilters:[...e,...N?.optionalFilters??[]]},recommendClient:i,threshold:m,transformItems:q.current}).then((e=>{h(e),b("idle")}))}))):u({fallbackParameters:I,indexName:n,maxRecommendations:r,model:a,objectIDs:E,queryParameters:N,recommendClient:i,threshold:m,transformItems:q.current}).then((e=>{h(e),b("idle")}))}),[I,n,r,a,E,N,i,b,m,d,f]),{...p,status:k}},e.useRecommendedForYou=W,e.useRelatedProducts=v,e.useTrendingFacets=H,e.useTrendingItems=D})); | ||
/*! @algolia/recommend-react 1.15.0 | MIT License | © Algolia, Inc. and contributors | https://github.com/algolia/recommend */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@algolia/recommend-react"]={},e.React)}(this,(function(e,t){"use strict";function n(){return n=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},n.apply(this,arguments)}function r(...e){return e.filter(Boolean).join(" ")}function a({createElement:e,Fragment:t}){return function(n){return 0===n.recommendations.length&&"idle"===n.status?e(n.Fallback,null):e("section",{className:r("auc-Recommend",n.classNames.root)},e(n.Header,{classNames:n.classNames,recommendations:n.recommendations,translations:n.translations,createElement:e,Fragment:t}),e(n.View,null))}}function s({createElement:e}){return function(t){return!t.recommendations||t.recommendations.length<1?null:t.translations.title?e("h3",{className:r("auc-Recommend-title",t.classNames.title)},t.translations.title):null}}function o({createElement:e,Fragment:t}){return function(n){return e("div",{className:r("auc-Recommend-container",n.classNames.container)},e("ol",{className:r("auc-Recommend-list",n.classNames.list)},n.items.map((a=>e("li",{key:a.objectID,className:r("auc-Recommend-item",n.classNames.item)},e(n.itemComponent,{createElement:e,Fragment:t,item:a}))))))}}function i({hits:e,maxRecommendations:t,nrOfObjs:n}){const r={};e.forEach((e=>{e.forEach(((e,t)=>{r[e.objectID]?r[e.objectID]={indexSum:r[e.objectID].indexSum+t,nr:r[e.objectID].nr+1}:r[e.objectID]={indexSum:t,nr:1}}))}));const a=((e,t)=>{const n=[];for(const r of Object.keys(e))e[r].nr<2&&(e[r].indexSum+=100),n.push({objectID:r,avgOfIndices:e[r].indexSum/t});return n.sort(((e,t)=>e.avgOfIndices>t.avgOfIndices?1:-1))})(r,n);return a.reduce(((t,n)=>{const r=e.flat().find((e=>e.objectID===n.objectID));return r?t.concat(r):t}),[]).slice(0,t&&t>0?t:void 0)}function m({hits:e,maxRecommendations:t}){return function(e,t){const n=[...t];return n.sort(e),n}(((e,t)=>(e._score||0)>(t._score||0)?-1:1),e).slice(0,t&&t>0?t:void 0)}const c="1.15.0";function l({objectIDs:e,recommendClient:t,transformItems:n=(e=>e),indexName:r,maxRecommendations:a,queryParameters:s,threshold:o}){const m=e.map((e=>({indexName:r,maxRecommendations:a,objectID:e,queryParameters:s,threshold:o})));return t.addAlgoliaAgent("recommend-core",c),0===m.length?Promise.resolve({recommendations:[]}):t.getFrequentlyBoughtTogether(m).then((t=>i({maxRecommendations:a,hits:t.results.map((e=>e.hits)),nrOfObjs:e.length}))).then((e=>({recommendations:n(e)})))}function u({objectIDs:e,recommendClient:t,transformItems:n=(e=>e),fallbackParameters:r,indexName:a,maxRecommendations:s,model:o,queryParameters:m,threshold:l}){const u=e.map((e=>({fallbackParameters:r,indexName:a,maxRecommendations:s,model:o,objectID:e,queryParameters:m,threshold:l})));return t.addAlgoliaAgent("recommend-core",c),0===u.length?Promise.resolve({recommendations:[]}):t.getRecommendations(u).then((t=>i({maxRecommendations:s,hits:t.results.map((e=>e.hits)),nrOfObjs:e.length}))).then((e=>({recommendations:n(e)})))}function d({objectIDs:e,recommendClient:t,transformItems:n=(e=>e),fallbackParameters:r,indexName:a,maxRecommendations:s,queryParameters:o,threshold:m}){const l=e.map((e=>({fallbackParameters:r,indexName:a,maxRecommendations:s,objectID:e,queryParameters:o,threshold:m})));return t.addAlgoliaAgent("recommend-core",c),0===l.length?Promise.resolve({recommendations:[]}):t.getRelatedProducts(l).then((t=>i({maxRecommendations:s,hits:t.results.map((e=>e.hits)),nrOfObjs:e.length}))).then((e=>({recommendations:n(e)})))}function f({objectIDs:e,recommendClient:t,transformItems:n=(e=>e),fallbackParameters:r,indexName:a,maxRecommendations:s,queryParameters:o,threshold:m}){const l=e.map((e=>({fallbackParameters:r,indexName:a,maxRecommendations:s,objectID:e,queryParameters:o,threshold:m})));return t.addAlgoliaAgent("recommend-core",c),0===l.length?Promise.resolve({recommendations:[]}):t.getLookingSimilar(l).then((t=>i({maxRecommendations:s,hits:t.results.map((e=>e.hits)),nrOfObjs:e.length}))).then((e=>({recommendations:n(e)})))}function g({recommendClient:e,transformItems:t=(e=>e),fallbackParameters:n,indexName:r,maxRecommendations:a,queryParameters:s,threshold:o,facetName:i,facetValue:l}){const u={fallbackParameters:n,indexName:r,maxRecommendations:a,queryParameters:s,threshold:o,facetName:i,facetValue:l};return e.addAlgoliaAgent("recommend-core",c),r?e.getTrendingItems([u]).then((e=>{return m({maxRecommendations:a,hits:(t="objectID",n=e.results.map((e=>e.hits)).flat(),[...new Map(n.map((e=>[e[t],e]))).values()])});var t,n})).then((e=>({recommendations:t(e)}))):Promise.resolve({recommendations:[]})}const p=async({userToken:e,region:t,apiKey:n,appId:r})=>{const a=await fetch(`https://personalization.${t}.algolia.com/1/profiles/personalization/${encodeURIComponent(e)}`,{headers:{"Content-Type":"application/json","X-Algolia-Application-Id":r,"X-Algolia-API-Key":n}});if(!a.ok)throw new Error(`Failed to fetch personalization affinities. Status: ${a.status}`);return await a.json()},h=async({region:e,apiKey:t,appId:n})=>{const r=await fetch(`https://personalization.${e}.algolia.com/1/strategies/personalization`,{headers:{"Content-Type":"application/json","X-Algolia-Application-Id":n,"X-Algolia-API-Key":t}});if(!r.ok)throw new Error(`Failed to fetch personalization strategy. Status: ${r.status}`);return await r.json()},y=async({userToken:e,region:t,apiKey:n,appId:r})=>{if(!t)throw new Error("[Algolia Recommend] parameter `region` is required to enable personalization.");if(!e)return console.warn("[Algolia Recommend] Personalization couldn't be enabled because `userToken` is missing. Falling back to non-personalized recommendations."),[];try{const[a,s]=await Promise.all([p({userToken:e,apiKey:n,appId:r,region:t}),h({apiKey:n,appId:r,region:t})]),o=100,i=new Map(s.facetsScoring.map((e=>[e.facetName,e.score])));return Object.entries(a.scores).flatMap((([e,t])=>Object.entries(t).map((([t,n])=>{const r=i.get(e)??o,a=Math.floor(n*(r/100));return`${e}:${t}<score=${a}>`}))))}catch(e){const t=e instanceof Error?e.message:String(e);return console.error(`[Algolia Recommend] Personalization couldn't be enabled. Falling back to non-personalized recommendations. Error: ${t}`),[]}},k=e=>void 0!==e.region&&void 0!==e.userToken,x=e=>k(e)?{region:e.region,userToken:e.userToken,suppressExperimentalWarning:Boolean(e.suppressExperimentalWarning)}:{region:void 0,userToken:void 0,suppressExperimentalWarning:Boolean(e.suppressExperimentalWarning)};function P(e){t.useEffect((()=>{e.recommendClient.addAlgoliaAgent("recommend-react","1.15.0")}),[e.recommendClient])}var b=Object.prototype.hasOwnProperty;function E(e,t){var n,r;if(e===t)return!0;if(e&&t&&(n=e.constructor)===t.constructor){if(n===Date)return e.getTime()===t.getTime();if(n===RegExp)return e.toString()===t.toString();if(n===Array){if((r=e.length)===t.length)for(;r--&&E(e[r],t[r]););return-1===r}if(!n||"object"==typeof e){for(n in r=0,e){if(b.call(e,n)&&++r&&!b.call(t,n))return!1;if(!(n in t)||!E(e[n],t[n]))return!1}return Object.keys(t).length===r}}return e!=e&&t!=t}function R(e){const[n,r]=t.useState((()=>e));return t.useEffect((()=>{!1===E(n,e)&&r(e)}),[e]),n}function C(e){const n=t.useRef(void 0),[r,a]=t.useState(e);return t.useEffect((()=>{"stalled"!==r&&n.current&&clearTimeout(n.current),"loading"===r&&(n.current=setTimeout((()=>{a("stalled")}),300))}),[r]),{status:r,setStatus:a}}const F=(e,n)=>{t.useEffect((()=>{}),[e,n])},N=t.createContext({hasProvider:!1}),I=()=>t.useContext(N),q=e=>{const n=t.useContext(N);if(e)return{client:e,isContextClient:!1};if(n.recommendClient)return{client:n.recommendClient,isContextClient:!0};throw new Error("Pass an Algolia `recommendClient` instance either to the Recommend React context, a component or hook.")};function T({indexName:e,maxRecommendations:n,objectIDs:r,queryParameters:a,recommendClient:s,threshold:o,transformItems:i=(e=>e),...m}){const{userToken:c,region:u,suppressExperimentalWarning:d}=x(m),[f,g]=t.useState({recommendations:[]}),{status:p,setStatus:h}=C("loading"),k=R(r),b=R(a),{hasProvider:E,register:N}=I(),{client:T,isContextClient:j}=q(s);P({recommendClient:T});const v=t.useRef(i);return t.useEffect((()=>{v.current=i}),[i]),F(d,"FrequentlyBoughtTogether"),t.useEffect((()=>{const t={indexName:e,maxRecommendations:n,objectIDs:k,queryParameters:b,threshold:o,transformItems:v.current};if(E&&j){const r=JSON.stringify(t),a=k.map((t=>({indexName:e,model:"bought-together",threshold:o,maxRecommendations:n,objectID:t,queryParameters:b,userToken:c,region:u})));return N({key:r,getParameters:()=>({queries:a,keyPair:{key:r,value:k.length}}),onRequest(){h("loading")},onResult(e){g(e),h("idle")}})}return h("loading"),c&&u?(T.addAlgoliaAgent("experimental-personalization"),y({apiKey:T.transporter.queryParameters["x-algolia-api-key"],appId:T.appId,region:u,userToken:c}).then((e=>{l({...t,queryParameters:{...t.queryParameters,optionalFilters:[...e,...t.queryParameters?.optionalFilters??[]]},recommendClient:T}).then((e=>{g(e),h("idle")}))}))):l({...t,recommendClient:T}).then((e=>{g(e),h("idle")})),()=>{}}),[e,n,k,b,T,h,o,E,j,N,c,u]),{...f,status:p}}const j=function({createElement:e,Fragment:t}){return function(r){const i={title:"Frequently bought together",sliderLabel:"Frequently bought together products",...r.translations},m=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??o({createElement:e,Fragment:t});return c({classNames:m,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:i,View:a=>e(d,n({classNames:m,itemComponent:r.itemComponent,items:r.items,translations:i,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});function v({fallbackParameters:e,indexName:n,maxRecommendations:r,objectIDs:a,queryParameters:s,recommendClient:o,threshold:i,transformItems:m=(e=>e),...c}){const{userToken:l,region:u,suppressExperimentalWarning:f}=x(c),[g,p]=t.useState({recommendations:[]}),{status:h,setStatus:k}=C("loading"),b=R(a),E=R(s),N=R(e),{hasProvider:T,register:j}=I(),{client:v,isContextClient:w}=q(o);P({recommendClient:v});const A=t.useRef(m);return t.useEffect((()=>{A.current=m}),[m]),F(f,"RelatedProducts"),t.useEffect((()=>{const e={fallbackParameters:N,indexName:n,maxRecommendations:r,objectIDs:b,queryParameters:E,threshold:i,transformItems:A.current};if(T&&w){const t=JSON.stringify(e),a=b.map((e=>({indexName:n,model:"related-products",threshold:i,maxRecommendations:r,objectID:e,queryParameters:E,fallbackParameters:N,userToken:l,region:u})));return j({key:t,getParameters:()=>({queries:a,keyPair:{key:t,value:b.length}}),onRequest(){k("loading")},onResult(e){p(e),k("idle")}})}return k("loading"),l&&u?(v.addAlgoliaAgent("experimental-personalization"),y({apiKey:v.transporter.queryParameters["x-algolia-api-key"],appId:v.appId,region:u,userToken:l}).then((t=>{d({...e,queryParameters:{...e.queryParameters,optionalFilters:[...t,...e.queryParameters?.optionalFilters??[]]},recommendClient:v}).then((e=>{p(e),k("idle")}))}))):d({...e,recommendClient:v}).then((e=>{p(e),k("idle")})),()=>{}}),[v,N,T,n,w,r,b,E,j,k,i,l,u]),{...g,status:h}}const w=function({createElement:e,Fragment:t}){return function(r){const i={title:"Related products",sliderLabel:"Related products",...r.translations},m=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??o({createElement:e,Fragment:t});return c({classNames:m,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:i,View:a=>e(d,n({classNames:m,itemComponent:r.itemComponent,items:r.items,translations:i,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});function A({fallbackParameters:e,indexName:n,maxRecommendations:r,objectIDs:a,queryParameters:s,recommendClient:o,threshold:i,transformItems:m=(e=>e),...c}){const{userToken:l,region:u,suppressExperimentalWarning:d}=x(c),[g,p]=t.useState({recommendations:[]}),{status:h,setStatus:k}=C("loading"),b=R(a),E=R(s),N=R(e),{hasProvider:T,register:j}=I(),{client:v,isContextClient:w}=q(o);P({recommendClient:v});const A=t.useRef(m);return t.useEffect((()=>{A.current=m}),[m]),F(d,"LookingSimilar"),t.useEffect((()=>{const e={fallbackParameters:N,indexName:n,maxRecommendations:r,objectIDs:b,queryParameters:E,threshold:i,transformItems:A.current};if(T&&w){const t=JSON.stringify(e),a=b.map((e=>({indexName:n,model:"looking-similar",threshold:i,maxRecommendations:r,objectID:e,queryParameters:E,fallbackParameters:N,userToken:l,region:u})));return j({key:t,getParameters:()=>({queries:a,keyPair:{key:t,value:b.length}}),onRequest(){k("loading")},onResult(e){p(e),k("idle")}})}return k("loading"),l&&u?(v.addAlgoliaAgent("experimental-personalization"),y({apiKey:v.transporter.queryParameters["x-algolia-api-key"],appId:v.appId,region:u,userToken:l}).then((t=>{f({...e,queryParameters:{...e.queryParameters,optionalFilters:[...t,...e.queryParameters?.optionalFilters??[]]},recommendClient:v}).then((e=>{p(e),k("idle")}))}))):f({...e,recommendClient:v}).then((e=>{p(e),k("idle")})),()=>{}}),[v,N,T,n,w,r,b,E,j,k,i,l,u]),{...g,status:h}}const S=function({createElement:e,Fragment:t}){return function(r){const i={title:"Looking Similar",sliderLabel:"Looking Similar",...r.translations},m=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??o({createElement:e,Fragment:t});return c({classNames:m,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:i,View:a=>e(d,n({classNames:m,itemComponent:r.itemComponent,items:r.items,translations:i,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});function D({fallbackParameters:e,indexName:n,maxRecommendations:r,queryParameters:a,recommendClient:s,threshold:o,facetName:i,facetValue:m,transformItems:c=(e=>e),...l}){const{userToken:u,region:d,suppressExperimentalWarning:f}=x(l),[p,h]=t.useState({recommendations:[]}),{status:k,setStatus:b}=C("loading"),E=R(a),N=R(e),{hasProvider:T,register:j}=I(),{client:v,isContextClient:w}=q(s);P({recommendClient:v});const A=t.useRef(c);return t.useEffect((()=>{A.current=c}),[c]),F(f,"TrendingItems"),t.useEffect((()=>{const e={model:"trending-items",fallbackParameters:N,indexName:n,maxRecommendations:r,queryParameters:E,threshold:o,facetName:i,facetValue:m,transformItems:A.current};if(T&&w){const t=[{...e,userToken:u,region:d}],n=JSON.stringify(e);return j({key:n,getParameters:()=>({queries:t,keyPair:{key:n,value:1}}),onRequest(){b("loading")},onResult(e){h(e),b("idle")}})}return b("loading"),u&&d?(v.addAlgoliaAgent("experimental-personalization"),y({apiKey:v.transporter.queryParameters["x-algolia-api-key"],appId:v.appId,region:d,userToken:u}).then((t=>{g({...e,queryParameters:{...e.queryParameters,optionalFilters:[...t,...e.queryParameters?.optionalFilters??[]]},recommendClient:v}).then((e=>{h(e),b("idle")}))}))):g({...e,recommendClient:v,transformItems:A.current}).then((e=>{h(e),b("idle")})),()=>{}}),[N,n,r,E,v,b,o,i,m,T,w,j,u,d]),{...p,status:k}}const O=function({createElement:e,Fragment:t}){return function(r){const i={title:"Trending items",sliderLabel:"Trending items",...r.translations},m=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??o({createElement:e,Fragment:t});return c({classNames:m,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:i,View:a=>e(d,n({classNames:m,itemComponent:r.itemComponent,items:r.items,translations:i,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});var z=Object.prototype.hasOwnProperty;function L(e,t,n){for(n of e.keys())if(V(n,t))return n}function V(e,t){var n,r,a;if(e===t)return!0;if(e&&t&&(n=e.constructor)===t.constructor){if(n===Date)return e.getTime()===t.getTime();if(n===RegExp)return e.toString()===t.toString();if(n===Array){if((r=e.length)===t.length)for(;r--&&V(e[r],t[r]););return-1===r}if(n===Set){if(e.size!==t.size)return!1;for(r of e){if((a=r)&&"object"==typeof a&&!(a=L(t,a)))return!1;if(!t.has(a))return!1}return!0}if(n===Map){if(e.size!==t.size)return!1;for(r of e){if((a=r[0])&&"object"==typeof a&&!(a=L(t,a)))return!1;if(!V(r[1],t.get(a)))return!1}return!0}if(n===ArrayBuffer)e=new Uint8Array(e),t=new Uint8Array(t);else if(n===DataView){if((r=e.byteLength)===t.byteLength)for(;r--&&e.getInt8(r)===t.getInt8(r););return-1===r}if(ArrayBuffer.isView(e)){if((r=e.byteLength)===t.byteLength)for(;r--&&e[r]===t[r];);return-1===r}if(!n||"object"==typeof e){for(n in r=0,e){if(z.call(e,n)&&++r&&!z.call(t,n))return!1;if(!(n in t)||!V(e[n],t[n]))return!1}return Object.keys(t).length===r}}return e!=e&&t!=t}function K(e){return null!=e}function $(e){return JSON.stringify(e)}function _(e,t){return e.map((e=>{if(function(e,t){return Boolean(e[$(t)])}(t,e.param.queries)){const n=t[$(e.param.queries)];return e.onResult(n),null}return e.param})).filter(K)}const B=(e,t)=>{const{type:n}=t;switch(n){case"register":{const{widget:n}=t,r=n.getParameters(),a=function(e,t){return Boolean(e.find((e=>V(e.param,t))))}(e.widgets,r);return a?e:{...e,widgets:[...e.widgets,{...n,param:r}],isDirty:Date.now()}}case"unregister":{const{key:n}=t;return{...e,widgets:e.widgets.filter((e=>e.key!==n))}}case"request_success":return{...e,status:"idle",isDirty:null};default:return e}};function W({fallbackParameters:e,indexName:n,maxRecommendations:r,queryParameters:a,recommendClient:s,threshold:o,transformItems:i=(e=>e)}){const[m,l]=t.useState({recommendations:[]}),{status:u,setStatus:d}=C("loading"),f=R(a),g=R(e),{hasProvider:p,register:h}=I(),{client:y,isContextClient:k}=q(s);P({recommendClient:y});const x=t.useRef(i);return t.useEffect((()=>{x.current=i}),[i]),t.useEffect((()=>{const e={fallbackParameters:g,indexName:n,maxRecommendations:r,queryParameters:f,threshold:o,transformItems:x.current};if(p&&k){const t=JSON.stringify(e),a=[{indexName:n,threshold:o,maxRecommendations:r,queryParameters:f,fallbackParameters:g,model:"recommended-for-you"}];return h({key:t,getParameters:()=>({queries:a,keyPair:{key:t,value:1}}),onRequest(){d("loading")},onResult(e){l(e),d("idle")}})}return d("loading"),function({indexName:e,threshold:t,queryParameters:n,recommendClient:r,maxRecommendations:a,transformItems:s=(e=>e)}){r.addAlgoliaAgent("recommend-core",c);const o=[{indexName:e,threshold:t,queryParameters:n,maxRecommendations:a}];return r.getRecommendedForYou(o).then((e=>({recommendations:s(e.results.map((e=>e.hits)).flat())})))}({...e,recommendClient:y}).then((e=>{l(e),d("idle")})),()=>{}}),[y,g,p,n,k,r,f,h,d,o]),{...m,status:u}}const J=function({createElement:e,Fragment:t}){return function(r){const i={title:"Recommended For You",sliderLabel:"Recommended For You",...r.translations},m=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??o({createElement:e,Fragment:t});return c({classNames:m,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:i,View:a=>e(d,n({classNames:m,itemComponent:r.itemComponent,items:r.items,translations:i,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});function H({indexName:e,maxRecommendations:n,recommendClient:r,threshold:a,transformItems:s=(e=>e),facetName:o}){const[i,l]=t.useState({recommendations:[]}),{status:u,setStatus:d}=C("loading"),{hasProvider:f,register:g}=I(),{client:p,isContextClient:h}=q(r);P({recommendClient:p});const y=t.useRef(s);return t.useEffect((()=>{y.current=s}),[s]),t.useEffect((()=>{const t={model:"trending-facets",indexName:e,facetName:o,threshold:a,maxRecommendations:n,transformItems:y.current};if(f&&h){const e=JSON.stringify(t);return g({key:e,getParameters:()=>({queries:[t],keyPair:{key:e,value:1}}),onRequest(){d("loading")},onResult(e){l(e),d("idle")}})}return d("loading"),function({recommendClient:e,transformItems:t=(e=>e),indexName:n,maxRecommendations:r,threshold:a,facetName:s}){const o={indexName:n,maxRecommendations:r,threshold:a,facetName:s};return e.addAlgoliaAgent("recommend-core",c),e.getTrendingFacets([o]).then((e=>m({maxRecommendations:r,hits:e.results.map((e=>e.hits)).flat()}))).then((e=>({recommendations:t(e)})))}({...t,recommendClient:p,facetName:o,transformItems:y.current}).then((e=>{l(e),d("idle")})),()=>{}}),[e,n,p,d,a,o,f,h,g]),{...i,status:u}}const M=function({createElement:e,Fragment:t}){return function(o){const i={title:"Trending facets",sliderLabel:"Trending facets",...o.translations},m=o.classNames??{},c=o.children??a({createElement:e,Fragment:t}),l=o.fallbackComponent??function(){return null},u=o.headerComponent??s({createElement:e,Fragment:t}),d=o.view??function({createElement:e,Fragment:t}){return function(n){return e("div",{className:r("auc-Recommend-container",n.classNames.container)},e("ol",{className:r("auc-Recommend-list",n.classNames.list)},n.items.map((a=>e("li",{key:a.facetValue,className:r("auc-Recommend-item",n.classNames.item)},e(n.itemComponent,{createElement:e,Fragment:t,item:a}))))))}}({createElement:e,Fragment:t});return c({classNames:m,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:o.items,status:o.status,translations:i,View:r=>e(d,n({classNames:m,itemComponent:o.itemComponent,items:o.items,translations:i,Fragment:t,createElement:e},r))})}}({createElement:t.createElement,Fragment:t.Fragment});e.FrequentlyBoughtTogether=function(e){const{recommendations:r,status:a}=T(e);return t.createElement(j,n({},e,{items:r,status:a}))},e.LookingSimilar=function(e){const{recommendations:r,status:a}=A(e);return t.createElement(S,n({},e,{items:r,status:a}))},e.Recommend=function({recommendClient:e,children:n,...r}){const{userToken:a,region:s,suppressExperimentalWarning:o}=x(r),[l,u]=t.useReducer(B,{isDirty:null,cache:{},widgets:[],recommendClient:e});F(o,"<Recommend>"),t.useEffect((()=>{(async()=>{if(!l.isDirty)return;const t=_(l.widgets,l.cache);if(0===t.length)return;const{queries:n,keys:r}=function(e){return{queries:e.map((e=>e.queries)).reduce(((e,t)=>e.concat(t)),[]),keys:e.map((e=>e.keyPair))}}(t);r.forEach((e=>{l.widgets.forEach((t=>{t.key===e.key&&t.onRequest()}))}));const o=n.map((e=>k(e)?{userToken:e.userToken,region:e.region}:null)).filter(K);a&&s&&o.push({userToken:a,region:s});const d=Array.from(new Set(o)),f=await(async(e,t,n)=>(await Promise.all(n.map((({userToken:n,region:r})=>y({appId:e,apiKey:t,userToken:n,region:r}))))).reduce(((e,t,r)=>{const{userToken:a,region:s}=n[r];return e[`${s}_${a}`]=t,e}),{}))(e.appId,e.transporter.queryParameters["x-algolia-api-key"],d),g=n.map((e=>{if((e=>"trending-facets"===e.model)(e)||(e=>"recommended-for-you"===e.model)(e))return e;const{_userToken:t,_region:n}=k(e)?{_userToken:e.userToken,_region:e.region}:{_userToken:a,_region:s};if(!t||!n)return{...e,userToken:void 0,region:void 0};const r=f[`${n}_${t}`];return r?{...e,userToken:void 0,region:void 0,queryParameters:{...e.queryParameters,optionalFilters:[...r,...e.queryParameters?.optionalFilters??[]]}}:{...e,userToken:void 0,region:void 0}}));Object.keys(f).length>0&&e.addAlgoliaAgent("experimental-personalization");const p=await async function({keys:e,queries:t,recommendClient:n}){n.addAlgoliaAgent("recommend-core",c);const r=await n.getRecommendations(t);let a=0,s=0;const o={};return e.forEach((e=>{const{model:n}=JSON.parse(e.key);s+=e.value;const{maxRecommendations:c,transformItems:l=(e=>e)}=t[a],u=r?.results?.slice(a,s);a+=e.value;let d=[],f=[];"trending-facets"===n?f=u.map((e=>e.hits)).flat():d="trending-items"===n?m({maxRecommendations:c,hits:u.map((e=>e.hits)).flat()}):i({maxRecommendations:c,hits:u.map((e=>e.hits)),nrOfObjs:e.value}),d=l(d),o[e.key]={recommendations:d,trendingFacets:f}})),o}({recommendClient:e,queries:g,keys:r});Object.entries(p).forEach((([e,t])=>{l.widgets.forEach((n=>{n.key===e&&(n.onResult(t),l.cache[$(n.param.queries)]=t)}))})),u({type:"request_success"})})()}),[l.isDirty,u,l.widgets,l.cache,e,a,s]);const d=t.useCallback((e=>(u({type:"register",widget:e}),()=>{u({type:"unregister",key:e.key})})),[]),f=t.useMemo((()=>({register:d,recommendClient:e,hasProvider:!0})),[e,d]);return t.createElement(N.Provider,{value:f},n)},e.RecommendedForYou=function(e){const{recommendations:r,status:a}=W(e);return t.createElement(J,n({},e,{items:r,status:a}))},e.RelatedProducts=function(e){const{recommendations:r,status:a}=v(e);return t.createElement(w,n({},e,{items:r,status:a}))},e.TrendingFacets=function(e){const{recommendations:r,status:a}=H(e);return t.createElement(M,n({},e,{items:r,status:a}))},e.TrendingItems=function(e){const{recommendations:r,status:a}=D(e);return t.createElement(O,n({},e,{items:r,status:a}))},e.useFrequentlyBoughtTogether=T,e.useLookingSimilar=A,e.useRecommendations=function({fallbackParameters:e,indexName:n,maxRecommendations:r,model:a,objectIDs:s,queryParameters:o,recommendClient:i,threshold:m,transformItems:c=(e=>e),...l}){const{userToken:d,region:f,suppressExperimentalWarning:g}=x(l),[p,h]=t.useState({recommendations:[]}),{status:k,setStatus:b}=C("loading"),E=R(s),N=R(o),I=R(e);P({recommendClient:i});const q=t.useRef(c);return t.useEffect((()=>{q.current=c}),[c]),F(g,"useRecommendations"),t.useEffect((()=>{b("loading"),d&&f?(i.addAlgoliaAgent("experimental-personalization"),y({apiKey:i.transporter.queryParameters["x-algolia-api-key"],appId:i.appId,region:f,userToken:d}).then((e=>{u({fallbackParameters:I,indexName:n,maxRecommendations:r,model:a,objectIDs:E,queryParameters:{...N,optionalFilters:[...e,...N?.optionalFilters??[]]},recommendClient:i,threshold:m,transformItems:q.current}).then((e=>{h(e),b("idle")}))}))):u({fallbackParameters:I,indexName:n,maxRecommendations:r,model:a,objectIDs:E,queryParameters:N,recommendClient:i,threshold:m,transformItems:q.current}).then((e=>{h(e),b("idle")}))}),[I,n,r,a,E,N,i,b,m,d,f]),{...p,status:k}},e.useRecommendedForYou=W,e.useRelatedProducts=v,e.useTrendingFacets=H,e.useTrendingItems=D})); | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,3 @@ | ||
/*! @algolia/recommend-react 1.14.0 | MIT License | © Algolia, Inc. and contributors | https://github.com/algolia/recommend */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@algolia/recommend-react"]={},e.React)}(this,(function(e,t){"use strict";function n(){return n=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},n.apply(this,arguments)}function r(...e){return e.filter(Boolean).join(" ")}function a({createElement:e,Fragment:t}){return function(n){return 0===n.recommendations.length&&"idle"===n.status?e(n.Fallback,null):e("section",{className:r("auc-Recommend",n.classNames.root)},e(n.Header,{classNames:n.classNames,recommendations:n.recommendations,translations:n.translations,createElement:e,Fragment:t}),e(n.View,null))}}function s({createElement:e}){return function(t){return!t.recommendations||t.recommendations.length<1?null:t.translations.title?e("h3",{className:r("auc-Recommend-title",t.classNames.title)},t.translations.title):null}}function m({createElement:e,Fragment:t}){return function(n){return e("div",{className:r("auc-Recommend-container",n.classNames.container)},e("ol",{className:r("auc-Recommend-list",n.classNames.list)},n.items.map((a=>e("li",{key:a.objectID,className:r("auc-Recommend-item",n.classNames.item)},e(n.itemComponent,{createElement:e,Fragment:t,item:a}))))))}}function o({hits:e,maxRecommendations:t,nrOfObjs:n}){const r={};e.forEach((e=>{e.forEach(((e,t)=>{r[e.objectID]?r[e.objectID]={indexSum:r[e.objectID].indexSum+t,nr:r[e.objectID].nr+1}:r[e.objectID]={indexSum:t,nr:1}}))}));const a=((e,t)=>{const n=[];for(const r of Object.keys(e))e[r].nr<2&&(e[r].indexSum+=100),n.push({objectID:r,avgOfIndices:e[r].indexSum/t});return n.sort(((e,t)=>e.avgOfIndices>t.avgOfIndices?1:-1))})(r,n);return a.reduce(((t,n)=>{const r=e.flat().find((e=>e.objectID===n.objectID));return r?t.concat(r):t}),[]).slice(0,t&&t>0?t:void 0)}function i({hits:e,maxRecommendations:t}){return function(e,t){const n=[...t];return n.sort(e),n}(((e,t)=>(e._score||0)>(t._score||0)?-1:1),e).slice(0,t&&t>0?t:void 0)}const c="1.14.0";const l=t.createContext({hasProvider:!1}),u=()=>t.useContext(l),d=e=>{const n=t.useContext(l);if(e)return{client:e,isContextClient:!1};if(n.recommendClient)return{client:n.recommendClient,isContextClient:!0};throw new Error("Pass an Algolia `recommendClient` instance either to the Recommend React context, a component or hook.")};function f(e){t.useEffect((()=>{e.recommendClient.addAlgoliaAgent("recommend-react","1.14.0")}),[e.recommendClient])}var g=Object.prototype.hasOwnProperty;function h(e,t){var n,r;if(e===t)return!0;if(e&&t&&(n=e.constructor)===t.constructor){if(n===Date)return e.getTime()===t.getTime();if(n===RegExp)return e.toString()===t.toString();if(n===Array){if((r=e.length)===t.length)for(;r--&&h(e[r],t[r]););return-1===r}if(!n||"object"==typeof e){for(n in r=0,e){if(g.call(e,n)&&++r&&!g.call(t,n))return!1;if(!(n in t)||!h(e[n],t[n]))return!1}return Object.keys(t).length===r}}return e!=e&&t!=t}function y(e){const[n,r]=t.useState((()=>e));return t.useEffect((()=>{!1===h(n,e)&&r(e)}),[e]),n}function b(e){const n=t.useRef(void 0),[r,a]=t.useState(e);return t.useEffect((()=>{"stalled"!==r&&n.current&&clearTimeout(n.current),"loading"===r&&(n.current=setTimeout((()=>{a("stalled")}),300))}),[r]),{status:r,setStatus:a}}function x({indexName:e,maxRecommendations:n,objectIDs:r,queryParameters:a,recommendClient:s,threshold:m,transformItems:i=(e=>e)}){const[l,g]=t.useState({recommendations:[]}),{status:h,setStatus:x}=b("loading"),R=y(r),p=y(a),{hasProvider:E,register:N}=u(),{client:C,isContextClient:P}=d(s);f({recommendClient:C});const k=t.useRef(i);return t.useEffect((()=>{k.current=i}),[i]),t.useEffect((()=>{const t={indexName:e,maxRecommendations:n,objectIDs:R,queryParameters:p,threshold:m,transformItems:k.current};if(E&&P){const r=JSON.stringify(t),a=R.map((t=>({indexName:e,model:"bought-together",threshold:m,maxRecommendations:n,objectID:t,queryParameters:p})));return N({key:r,getParameters:()=>({queries:a,keyPair:{key:r,value:R.length}}),onRequest(){x("loading")},onResult(e){g(e),x("idle")}})}return x("loading"),function({objectIDs:e,recommendClient:t,transformItems:n=(e=>e),indexName:r,maxRecommendations:a,queryParameters:s,threshold:m}){const i=e.map((e=>({indexName:r,maxRecommendations:a,objectID:e,queryParameters:s,threshold:m})));return t.addAlgoliaAgent("recommend-core",c),0===i.length?Promise.resolve({recommendations:[]}):t.getFrequentlyBoughtTogether(i).then((t=>o({maxRecommendations:a,hits:t.results.map((e=>e.hits)),nrOfObjs:e.length}))).then((e=>({recommendations:n(e)})))}({...t,recommendClient:C}).then((e=>{g(e),x("idle")})),()=>{}}),[e,n,R,p,C,x,m,E,P,N]),{...l,status:h}}const R=function({createElement:e,Fragment:t}){return function(r){const o={title:"Frequently bought together",sliderLabel:"Frequently bought together products",...r.translations},i=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??m({createElement:e,Fragment:t});return c({classNames:i,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:o,View:a=>e(d,n({classNames:i,itemComponent:r.itemComponent,items:r.items,translations:o,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});function p({fallbackParameters:e,indexName:n,maxRecommendations:r,objectIDs:a,queryParameters:s,recommendClient:m,threshold:i,transformItems:l=(e=>e)}){const[g,h]=t.useState({recommendations:[]}),{status:x,setStatus:R}=b("loading"),p=y(a),E=y(s),N=y(e),{hasProvider:C,register:P}=u(),{client:k,isContextClient:F}=d(m);f({recommendClient:k});const I=t.useRef(l);return t.useEffect((()=>{I.current=l}),[l]),t.useEffect((()=>{const e={fallbackParameters:N,indexName:n,maxRecommendations:r,objectIDs:p,queryParameters:E,threshold:i,transformItems:I.current};if(C&&F){const t=JSON.stringify(e),a=p.map((e=>({indexName:n,model:"related-products",threshold:i,maxRecommendations:r,objectID:e,queryParameters:E,fallbackParameters:N})));return P({key:t,getParameters:()=>({queries:a,keyPair:{key:t,value:p.length}}),onRequest(){R("loading")},onResult(e){h(e),R("idle")}})}return R("loading"),function({objectIDs:e,recommendClient:t,transformItems:n=(e=>e),fallbackParameters:r,indexName:a,maxRecommendations:s,queryParameters:m,threshold:i}){const l=e.map((e=>({fallbackParameters:r,indexName:a,maxRecommendations:s,objectID:e,queryParameters:m,threshold:i})));return t.addAlgoliaAgent("recommend-core",c),0===l.length?Promise.resolve({recommendations:[]}):t.getRelatedProducts(l).then((t=>o({maxRecommendations:s,hits:t.results.map((e=>e.hits)),nrOfObjs:e.length}))).then((e=>({recommendations:n(e)})))}({...e,recommendClient:k}).then((e=>{h(e),R("idle")})),()=>{}}),[k,N,C,n,F,r,p,E,P,R,i]),{...g,status:x}}const E=function({createElement:e,Fragment:t}){return function(r){const o={title:"Related products",sliderLabel:"Related products",...r.translations},i=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??m({createElement:e,Fragment:t});return c({classNames:i,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:o,View:a=>e(d,n({classNames:i,itemComponent:r.itemComponent,items:r.items,translations:o,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});function N({fallbackParameters:e,indexName:n,maxRecommendations:r,queryParameters:a,recommendClient:s,threshold:m,transformItems:o=(e=>e)}){const[i,l]=t.useState({recommendations:[]}),{status:g,setStatus:h}=b("loading"),x=y(a),R=y(e),{hasProvider:p,register:E}=u(),{client:N,isContextClient:C}=d(s);f({recommendClient:N});const P=t.useRef(o);return t.useEffect((()=>{P.current=o}),[o]),t.useEffect((()=>{const e={fallbackParameters:R,indexName:n,maxRecommendations:r,queryParameters:x,threshold:m,transformItems:P.current};if(p&&C){const t=JSON.stringify(e),a=[{indexName:n,threshold:m,maxRecommendations:r,queryParameters:x,fallbackParameters:R,model:"recommended-for-you"}];return E({key:t,getParameters:()=>({queries:a,keyPair:{key:t,value:1}}),onRequest(){h("loading")},onResult(e){l(e),h("idle")}})}return h("loading"),function({indexName:e,threshold:t,queryParameters:n,recommendClient:r,maxRecommendations:a,transformItems:s=(e=>e)}){r.addAlgoliaAgent("recommend-core",c);const m=[{indexName:e,threshold:t,queryParameters:n,maxRecommendations:a}];return r.getRecommendedForYou(m).then((e=>({recommendations:s(e.results.map((e=>e.hits)).flat())})))}({...e,recommendClient:N}).then((e=>{l(e),h("idle")})),()=>{}}),[N,R,p,n,C,r,x,E,h,m]),{...i,status:g}}const C=function({createElement:e,Fragment:t}){return function(r){const o={title:"Recommended For You",sliderLabel:"Recommended For You",...r.translations},i=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??m({createElement:e,Fragment:t});return c({classNames:i,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:o,View:a=>e(d,n({classNames:i,itemComponent:r.itemComponent,items:r.items,translations:o,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});function P({fallbackParameters:e,indexName:n,maxRecommendations:r,objectIDs:a,queryParameters:s,recommendClient:m,threshold:i,transformItems:l=(e=>e)}){const[g,h]=t.useState({recommendations:[]}),{status:x,setStatus:R}=b("loading"),p=y(a),E=y(s),N=y(e),{hasProvider:C,register:P}=u(),{client:k,isContextClient:F}=d(m);f({recommendClient:k});const I=t.useRef(l);return t.useEffect((()=>{I.current=l}),[l]),t.useEffect((()=>{const e={fallbackParameters:N,indexName:n,maxRecommendations:r,objectIDs:p,queryParameters:E,threshold:i,transformItems:I.current};if(C&&F){const t=JSON.stringify(e),a=p.map((e=>({indexName:n,model:"looking-similar",threshold:i,maxRecommendations:r,objectID:e,queryParameters:E,fallbackParameters:N})));return P({key:t,getParameters:()=>({queries:a,keyPair:{key:t,value:p.length}}),onRequest(){R("loading")},onResult(e){h(e),R("idle")}})}return R("loading"),function({objectIDs:e,recommendClient:t,transformItems:n=(e=>e),fallbackParameters:r,indexName:a,maxRecommendations:s,queryParameters:m,threshold:i}){const l=e.map((e=>({fallbackParameters:r,indexName:a,maxRecommendations:s,objectID:e,queryParameters:m,threshold:i})));return t.addAlgoliaAgent("recommend-core",c),0===l.length?Promise.resolve({recommendations:[]}):t.getLookingSimilar(l).then((t=>o({maxRecommendations:s,hits:t.results.map((e=>e.hits)),nrOfObjs:e.length}))).then((e=>({recommendations:n(e)})))}({...e,recommendClient:k}).then((e=>{h(e),R("idle")})),()=>{}}),[k,N,C,n,F,r,p,E,P,R,i]),{...g,status:x}}const k=function({createElement:e,Fragment:t}){return function(r){const o={title:"Looking Similar",sliderLabel:"Looking Similar",...r.translations},i=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??m({createElement:e,Fragment:t});return c({classNames:i,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:o,View:a=>e(d,n({classNames:i,itemComponent:r.itemComponent,items:r.items,translations:o,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});function F({indexName:e,maxRecommendations:n,recommendClient:r,threshold:a,transformItems:s=(e=>e),facetName:m}){const[o,l]=t.useState({recommendations:[]}),{status:g,setStatus:h}=b("loading"),{hasProvider:y,register:x}=u(),{client:R,isContextClient:p}=d(r);f({recommendClient:R});const E=t.useRef(s);return t.useEffect((()=>{E.current=s}),[s]),t.useEffect((()=>{const t={model:"trending-facets",indexName:e,facetName:m,threshold:a,maxRecommendations:n,transformItems:E.current};if(y&&p){const e=JSON.stringify(t);return x({key:e,getParameters:()=>({queries:[t],keyPair:{key:e,value:1}}),onRequest(){h("loading")},onResult(e){l({...e,recommendations:e.trendingFacets}),h("idle")}})}return h("loading"),function({recommendClient:e,transformItems:t=(e=>e),indexName:n,maxRecommendations:r,threshold:a,facetName:s}){const m={indexName:n,maxRecommendations:r,threshold:a,facetName:s};return e.addAlgoliaAgent("recommend-core",c),e.getTrendingFacets([m]).then((e=>i({maxRecommendations:r,hits:e.results.map((e=>e.hits)).flat()}))).then((e=>({recommendations:t(e)})))}({...t,recommendClient:R,facetName:m,transformItems:E.current}).then((e=>{l(e),h("idle")})),()=>{}}),[e,n,R,h,a,m,y,p,x]),{...o,status:g}}const I=function({createElement:e,Fragment:t}){return function(m){const o={title:"Trending facets",sliderLabel:"Trending facets",...m.translations},i=m.classNames??{},c=m.children??a({createElement:e,Fragment:t}),l=m.fallbackComponent??function(){return null},u=m.headerComponent??s({createElement:e,Fragment:t}),d=m.view??function({createElement:e,Fragment:t}){return function(n){return e("div",{className:r("auc-Recommend-container",n.classNames.container)},e("ol",{className:r("auc-Recommend-list",n.classNames.list)},n.items.map((a=>e("li",{key:a.facetValue,className:r("auc-Recommend-item",n.classNames.item)},e(n.itemComponent,{createElement:e,Fragment:t,item:a}))))))}}({createElement:e,Fragment:t});return c({classNames:i,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:m.items,status:m.status,translations:o,View:r=>e(d,n({classNames:i,itemComponent:m.itemComponent,items:m.items,translations:o,Fragment:t,createElement:e},r))})}}({createElement:t.createElement,Fragment:t.Fragment});function q({fallbackParameters:e,indexName:n,maxRecommendations:r,queryParameters:a,recommendClient:s,threshold:m,transformItems:o=(e=>e),facetName:l,facetValue:g}){const[h,x]=t.useState({recommendations:[]}),{status:R,setStatus:p}=b("loading"),E=y(a),N=y(e),{hasProvider:C,register:P}=u(),{client:k,isContextClient:F}=d(s);f({recommendClient:k});const I=t.useRef(o);return t.useEffect((()=>{I.current=o}),[o]),t.useEffect((()=>{const e={model:"trending-items",fallbackParameters:N,indexName:n,maxRecommendations:r,queryParameters:E,threshold:m,facetName:l,facetValue:g,transformItems:I.current};if(C&&F){const t=JSON.stringify(e);return P({key:t,getParameters:()=>({queries:[e],keyPair:{key:t,value:1}}),onRequest(){p("loading")},onResult(e){x(e),p("idle")}})}return p("loading"),function({recommendClient:e,transformItems:t=(e=>e),fallbackParameters:n,indexName:r,maxRecommendations:a,queryParameters:s,threshold:m,facetName:o,facetValue:l}){const u={fallbackParameters:n,indexName:r,maxRecommendations:a,queryParameters:s,threshold:m,facetName:o,facetValue:l};return e.addAlgoliaAgent("recommend-core",c),r?e.getTrendingItems([u]).then((e=>{return i({maxRecommendations:a,hits:(t="objectID",n=e.results.map((e=>e.hits)).flat(),[...new Map(n.map((e=>[e[t],e]))).values()])});var t,n})).then((e=>({recommendations:t(e)}))):Promise.resolve({recommendations:[]})}({...e,recommendClient:k,transformItems:I.current}).then((e=>{x(e),p("idle")})),()=>{}}),[N,n,r,E,k,p,m,l,g,C,F,P]),{...h,status:R}}const j=function({createElement:e,Fragment:t}){return function(r){const o={title:"Trending items",sliderLabel:"Trending items",...r.translations},i=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??m({createElement:e,Fragment:t});return c({classNames:i,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:o,View:a=>e(d,n({classNames:i,itemComponent:r.itemComponent,items:r.items,translations:o,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});var v=Object.prototype.hasOwnProperty;function S(e,t,n){for(n of e.keys())if(w(n,t))return n}function w(e,t){var n,r,a;if(e===t)return!0;if(e&&t&&(n=e.constructor)===t.constructor){if(n===Date)return e.getTime()===t.getTime();if(n===RegExp)return e.toString()===t.toString();if(n===Array){if((r=e.length)===t.length)for(;r--&&w(e[r],t[r]););return-1===r}if(n===Set){if(e.size!==t.size)return!1;for(r of e){if((a=r)&&"object"==typeof a&&!(a=S(t,a)))return!1;if(!t.has(a))return!1}return!0}if(n===Map){if(e.size!==t.size)return!1;for(r of e){if((a=r[0])&&"object"==typeof a&&!(a=S(t,a)))return!1;if(!w(r[1],t.get(a)))return!1}return!0}if(n===ArrayBuffer)e=new Uint8Array(e),t=new Uint8Array(t);else if(n===DataView){if((r=e.byteLength)===t.byteLength)for(;r--&&e.getInt8(r)===t.getInt8(r););return-1===r}if(ArrayBuffer.isView(e)){if((r=e.byteLength)===t.byteLength)for(;r--&&e[r]===t[r];);return-1===r}if(!n||"object"==typeof e){for(n in r=0,e){if(v.call(e,n)&&++r&&!v.call(t,n))return!1;if(!(n in t)||!w(e[n],t[n]))return!1}return Object.keys(t).length===r}}return e!=e&&t!=t}function D(e){return null!=e}function O(e){return JSON.stringify(e)}function A(e,t){return e.map((e=>{if(function(e,t){return Boolean(e[O(t)])}(t,e.param.queries)){const n=t[O(e.param.queries)];return e.onResult(n),null}return e.param})).filter(D)}const T=(e,t)=>{const{type:n}=t;switch(n){case"register":{const{widget:n}=t,r=n.getParameters(),a=function(e,t){return Boolean(e.find((e=>w(e.param,t))))}(e.widgets,r);return a?e:{...e,widgets:[...e.widgets,{...n,param:r}],isDirty:Date.now()}}case"unregister":{const{key:n}=t;return{...e,widgets:e.widgets.filter((e=>e.key!==n))}}case"request_success":return{...e,status:"idle",isDirty:null};default:return e}};e.FrequentlyBoughtTogether=function(e){const{recommendations:r,status:a}=x(e);return t.createElement(R,n({},e,{items:r,status:a}))},e.LookingSimilar=function(e){const{recommendations:r,status:a}=P(e);return t.createElement(k,n({},e,{items:r,status:a}))},e.Recommend=function({recommendClient:e,children:n}){const[r,a]=t.useReducer(T,{isDirty:null,cache:{},widgets:[],recommendClient:e});t.useEffect((()=>{if(!r.isDirty)return;const t=A(r.widgets,r.cache);if(0===t.length)return;const{queries:n,keys:s}=function(e){return{queries:e.map((e=>e.queries)).reduce(((e,t)=>e.concat(t)),[]),keys:e.map((e=>e.keyPair))}}(t);s.forEach((e=>{r.widgets.forEach((t=>{t.key===e.key&&t.onRequest()}))})),async function({keys:e,queries:t,recommendClient:n}){n.addAlgoliaAgent("recommend-core",c);const r=await n.getRecommendations(t);let a=0,s=0;const m={};return e.forEach((e=>{const{model:n}=JSON.parse(e.key);s+=e.value;const{maxRecommendations:c,transformItems:l=(e=>e)}=t[a],u=r?.results?.slice(a,s);a+=e.value;let d=[],f=[];"trending-facets"===n?f=u.map((e=>e.hits)).flat():d="trending-items"===n?i({maxRecommendations:c,hits:u.map((e=>e.hits)).flat()}):o({maxRecommendations:c,hits:u.map((e=>e.hits)),nrOfObjs:e.value}),d=l(d),m[e.key]={recommendations:d,trendingFacets:f}})),m}({recommendClient:e,queries:n,keys:s}).then((e=>{Object.entries(e).forEach((([e,t])=>{r.widgets.forEach((n=>{n.key===e&&(n.onResult(t),r.cache[O(n.param.queries)]=t)}))})),a({type:"request_success"})}))}),[r.isDirty,a,r.widgets,r.cache,e]);const s=t.useCallback((e=>(a({type:"register",widget:e}),()=>{a({type:"unregister",key:e.key})})),[]),m=t.useMemo((()=>({register:s,recommendClient:e,hasProvider:!0})),[e,s]);return t.createElement(l.Provider,{value:m},n)},e.RecommendedForYou=function(e){const{recommendations:r,status:a}=N(e);return t.createElement(C,n({},e,{items:r,status:a}))},e.RelatedProducts=function(e){const{recommendations:r,status:a}=p(e);return t.createElement(E,n({},e,{items:r,status:a}))},e.TrendingFacets=function(e){const{recommendations:r,status:a}=F(e);return t.createElement(I,n({},e,{items:r,status:a}))},e.TrendingItems=function(e){const{recommendations:r,status:a}=q(e);return t.createElement(j,n({},e,{items:r,status:a}))},e.useFrequentlyBoughtTogether=x,e.useLookingSimilar=P,e.useRecommendations=function({fallbackParameters:e,indexName:n,maxRecommendations:r,model:a,objectIDs:s,queryParameters:m,recommendClient:i,threshold:l,transformItems:u=(e=>e)}){const[d,g]=t.useState({recommendations:[]}),{status:h,setStatus:x}=b("loading"),R=y(s),p=y(m),E=y(e);f({recommendClient:i});const N=t.useRef(u);return t.useEffect((()=>{N.current=u}),[u]),t.useEffect((()=>{x("loading"),function({objectIDs:e,recommendClient:t,transformItems:n=(e=>e),fallbackParameters:r,indexName:a,maxRecommendations:s,model:m,queryParameters:i,threshold:l}){const u=e.map((e=>({fallbackParameters:r,indexName:a,maxRecommendations:s,model:m,objectID:e,queryParameters:i,threshold:l})));return t.addAlgoliaAgent("recommend-core",c),0===u.length?Promise.resolve({recommendations:[]}):t.getRecommendations(u).then((t=>o({maxRecommendations:s,hits:t.results.map((e=>e.hits)),nrOfObjs:e.length}))).then((e=>({recommendations:n(e)})))}({fallbackParameters:E,indexName:n,maxRecommendations:r,model:a,objectIDs:R,queryParameters:p,recommendClient:i,threshold:l,transformItems:N.current}).then((e=>{g(e),x("idle")}))}),[E,n,r,a,R,p,i,x,l]),{...d,status:h}},e.useRecommendedForYou=N,e.useRelatedProducts=p,e.useTrendingFacets=F,e.useTrendingItems=q})); | ||
/*! @algolia/recommend-react 1.15.0 | MIT License | © Algolia, Inc. and contributors | https://github.com/algolia/recommend */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@algolia/recommend-react"]={},e.React)}(this,(function(e,t){"use strict";function n(){return n=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},n.apply(this,arguments)}function r(...e){return e.filter(Boolean).join(" ")}function a({createElement:e,Fragment:t}){return function(n){return 0===n.recommendations.length&&"idle"===n.status?e(n.Fallback,null):e("section",{className:r("auc-Recommend",n.classNames.root)},e(n.Header,{classNames:n.classNames,recommendations:n.recommendations,translations:n.translations,createElement:e,Fragment:t}),e(n.View,null))}}function s({createElement:e}){return function(t){return!t.recommendations||t.recommendations.length<1?null:t.translations.title?e("h3",{className:r("auc-Recommend-title",t.classNames.title)},t.translations.title):null}}function m({createElement:e,Fragment:t}){return function(n){return e("div",{className:r("auc-Recommend-container",n.classNames.container)},e("ol",{className:r("auc-Recommend-list",n.classNames.list)},n.items.map((a=>e("li",{key:a.objectID,className:r("auc-Recommend-item",n.classNames.item)},e(n.itemComponent,{createElement:e,Fragment:t,item:a}))))))}}function o({hits:e,maxRecommendations:t,nrOfObjs:n}){const r={};e.forEach((e=>{e.forEach(((e,t)=>{r[e.objectID]?r[e.objectID]={indexSum:r[e.objectID].indexSum+t,nr:r[e.objectID].nr+1}:r[e.objectID]={indexSum:t,nr:1}}))}));const a=((e,t)=>{const n=[];for(const r of Object.keys(e))e[r].nr<2&&(e[r].indexSum+=100),n.push({objectID:r,avgOfIndices:e[r].indexSum/t});return n.sort(((e,t)=>e.avgOfIndices>t.avgOfIndices?1:-1))})(r,n);return a.reduce(((t,n)=>{const r=e.flat().find((e=>e.objectID===n.objectID));return r?t.concat(r):t}),[]).slice(0,t&&t>0?t:void 0)}function i({hits:e,maxRecommendations:t}){return function(e,t){const n=[...t];return n.sort(e),n}(((e,t)=>(e._score||0)>(t._score||0)?-1:1),e).slice(0,t&&t>0?t:void 0)}const c="1.15.0";const l=t.createContext({hasProvider:!1}),u=()=>t.useContext(l),d=e=>{const n=t.useContext(l);if(e)return{client:e,isContextClient:!1};if(n.recommendClient)return{client:n.recommendClient,isContextClient:!0};throw new Error("Pass an Algolia `recommendClient` instance either to the Recommend React context, a component or hook.")};function f(e){t.useEffect((()=>{e.recommendClient.addAlgoliaAgent("recommend-react","1.15.0")}),[e.recommendClient])}var g=Object.prototype.hasOwnProperty;function h(e,t){var n,r;if(e===t)return!0;if(e&&t&&(n=e.constructor)===t.constructor){if(n===Date)return e.getTime()===t.getTime();if(n===RegExp)return e.toString()===t.toString();if(n===Array){if((r=e.length)===t.length)for(;r--&&h(e[r],t[r]););return-1===r}if(!n||"object"==typeof e){for(n in r=0,e){if(g.call(e,n)&&++r&&!g.call(t,n))return!1;if(!(n in t)||!h(e[n],t[n]))return!1}return Object.keys(t).length===r}}return e!=e&&t!=t}function y(e){const[n,r]=t.useState((()=>e));return t.useEffect((()=>{!1===h(n,e)&&r(e)}),[e]),n}function b(e){const n=t.useRef(void 0),[r,a]=t.useState(e);return t.useEffect((()=>{"stalled"!==r&&n.current&&clearTimeout(n.current),"loading"===r&&(n.current=setTimeout((()=>{a("stalled")}),300))}),[r]),{status:r,setStatus:a}}function x({indexName:e,maxRecommendations:n,objectIDs:r,queryParameters:a,recommendClient:s,threshold:m,transformItems:i=(e=>e)}){const[l,g]=t.useState({recommendations:[]}),{status:h,setStatus:x}=b("loading"),R=y(r),p=y(a),{hasProvider:E,register:N}=u(),{client:C,isContextClient:P}=d(s);f({recommendClient:C});const k=t.useRef(i);return t.useEffect((()=>{k.current=i}),[i]),t.useEffect((()=>{const t={indexName:e,maxRecommendations:n,objectIDs:R,queryParameters:p,threshold:m,transformItems:k.current};if(E&&P){const r=JSON.stringify(t),a=R.map((t=>({indexName:e,model:"bought-together",threshold:m,maxRecommendations:n,objectID:t,queryParameters:p})));return N({key:r,getParameters:()=>({queries:a,keyPair:{key:r,value:R.length}}),onRequest(){x("loading")},onResult(e){g(e),x("idle")}})}return x("loading"),function({objectIDs:e,recommendClient:t,transformItems:n=(e=>e),indexName:r,maxRecommendations:a,queryParameters:s,threshold:m}){const i=e.map((e=>({indexName:r,maxRecommendations:a,objectID:e,queryParameters:s,threshold:m})));return t.addAlgoliaAgent("recommend-core",c),0===i.length?Promise.resolve({recommendations:[]}):t.getFrequentlyBoughtTogether(i).then((t=>o({maxRecommendations:a,hits:t.results.map((e=>e.hits)),nrOfObjs:e.length}))).then((e=>({recommendations:n(e)})))}({...t,recommendClient:C}).then((e=>{g(e),x("idle")})),()=>{}}),[e,n,R,p,C,x,m,E,P,N]),{...l,status:h}}const R=function({createElement:e,Fragment:t}){return function(r){const o={title:"Frequently bought together",sliderLabel:"Frequently bought together products",...r.translations},i=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??m({createElement:e,Fragment:t});return c({classNames:i,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:o,View:a=>e(d,n({classNames:i,itemComponent:r.itemComponent,items:r.items,translations:o,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});function p({fallbackParameters:e,indexName:n,maxRecommendations:r,objectIDs:a,queryParameters:s,recommendClient:m,threshold:i,transformItems:l=(e=>e)}){const[g,h]=t.useState({recommendations:[]}),{status:x,setStatus:R}=b("loading"),p=y(a),E=y(s),N=y(e),{hasProvider:C,register:P}=u(),{client:k,isContextClient:F}=d(m);f({recommendClient:k});const I=t.useRef(l);return t.useEffect((()=>{I.current=l}),[l]),t.useEffect((()=>{const e={fallbackParameters:N,indexName:n,maxRecommendations:r,objectIDs:p,queryParameters:E,threshold:i,transformItems:I.current};if(C&&F){const t=JSON.stringify(e),a=p.map((e=>({indexName:n,model:"related-products",threshold:i,maxRecommendations:r,objectID:e,queryParameters:E,fallbackParameters:N})));return P({key:t,getParameters:()=>({queries:a,keyPair:{key:t,value:p.length}}),onRequest(){R("loading")},onResult(e){h(e),R("idle")}})}return R("loading"),function({objectIDs:e,recommendClient:t,transformItems:n=(e=>e),fallbackParameters:r,indexName:a,maxRecommendations:s,queryParameters:m,threshold:i}){const l=e.map((e=>({fallbackParameters:r,indexName:a,maxRecommendations:s,objectID:e,queryParameters:m,threshold:i})));return t.addAlgoliaAgent("recommend-core",c),0===l.length?Promise.resolve({recommendations:[]}):t.getRelatedProducts(l).then((t=>o({maxRecommendations:s,hits:t.results.map((e=>e.hits)),nrOfObjs:e.length}))).then((e=>({recommendations:n(e)})))}({...e,recommendClient:k}).then((e=>{h(e),R("idle")})),()=>{}}),[k,N,C,n,F,r,p,E,P,R,i]),{...g,status:x}}const E=function({createElement:e,Fragment:t}){return function(r){const o={title:"Related products",sliderLabel:"Related products",...r.translations},i=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??m({createElement:e,Fragment:t});return c({classNames:i,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:o,View:a=>e(d,n({classNames:i,itemComponent:r.itemComponent,items:r.items,translations:o,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});function N({fallbackParameters:e,indexName:n,maxRecommendations:r,queryParameters:a,recommendClient:s,threshold:m,transformItems:o=(e=>e)}){const[i,l]=t.useState({recommendations:[]}),{status:g,setStatus:h}=b("loading"),x=y(a),R=y(e),{hasProvider:p,register:E}=u(),{client:N,isContextClient:C}=d(s);f({recommendClient:N});const P=t.useRef(o);return t.useEffect((()=>{P.current=o}),[o]),t.useEffect((()=>{const e={fallbackParameters:R,indexName:n,maxRecommendations:r,queryParameters:x,threshold:m,transformItems:P.current};if(p&&C){const t=JSON.stringify(e),a=[{indexName:n,threshold:m,maxRecommendations:r,queryParameters:x,fallbackParameters:R,model:"recommended-for-you"}];return E({key:t,getParameters:()=>({queries:a,keyPair:{key:t,value:1}}),onRequest(){h("loading")},onResult(e){l(e),h("idle")}})}return h("loading"),function({indexName:e,threshold:t,queryParameters:n,recommendClient:r,maxRecommendations:a,transformItems:s=(e=>e)}){r.addAlgoliaAgent("recommend-core",c);const m=[{indexName:e,threshold:t,queryParameters:n,maxRecommendations:a}];return r.getRecommendedForYou(m).then((e=>({recommendations:s(e.results.map((e=>e.hits)).flat())})))}({...e,recommendClient:N}).then((e=>{l(e),h("idle")})),()=>{}}),[N,R,p,n,C,r,x,E,h,m]),{...i,status:g}}const C=function({createElement:e,Fragment:t}){return function(r){const o={title:"Recommended For You",sliderLabel:"Recommended For You",...r.translations},i=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??m({createElement:e,Fragment:t});return c({classNames:i,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:o,View:a=>e(d,n({classNames:i,itemComponent:r.itemComponent,items:r.items,translations:o,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});function P({fallbackParameters:e,indexName:n,maxRecommendations:r,objectIDs:a,queryParameters:s,recommendClient:m,threshold:i,transformItems:l=(e=>e)}){const[g,h]=t.useState({recommendations:[]}),{status:x,setStatus:R}=b("loading"),p=y(a),E=y(s),N=y(e),{hasProvider:C,register:P}=u(),{client:k,isContextClient:F}=d(m);f({recommendClient:k});const I=t.useRef(l);return t.useEffect((()=>{I.current=l}),[l]),t.useEffect((()=>{const e={fallbackParameters:N,indexName:n,maxRecommendations:r,objectIDs:p,queryParameters:E,threshold:i,transformItems:I.current};if(C&&F){const t=JSON.stringify(e),a=p.map((e=>({indexName:n,model:"looking-similar",threshold:i,maxRecommendations:r,objectID:e,queryParameters:E,fallbackParameters:N})));return P({key:t,getParameters:()=>({queries:a,keyPair:{key:t,value:p.length}}),onRequest(){R("loading")},onResult(e){h(e),R("idle")}})}return R("loading"),function({objectIDs:e,recommendClient:t,transformItems:n=(e=>e),fallbackParameters:r,indexName:a,maxRecommendations:s,queryParameters:m,threshold:i}){const l=e.map((e=>({fallbackParameters:r,indexName:a,maxRecommendations:s,objectID:e,queryParameters:m,threshold:i})));return t.addAlgoliaAgent("recommend-core",c),0===l.length?Promise.resolve({recommendations:[]}):t.getLookingSimilar(l).then((t=>o({maxRecommendations:s,hits:t.results.map((e=>e.hits)),nrOfObjs:e.length}))).then((e=>({recommendations:n(e)})))}({...e,recommendClient:k}).then((e=>{h(e),R("idle")})),()=>{}}),[k,N,C,n,F,r,p,E,P,R,i]),{...g,status:x}}const k=function({createElement:e,Fragment:t}){return function(r){const o={title:"Looking Similar",sliderLabel:"Looking Similar",...r.translations},i=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??m({createElement:e,Fragment:t});return c({classNames:i,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:o,View:a=>e(d,n({classNames:i,itemComponent:r.itemComponent,items:r.items,translations:o,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});function F({indexName:e,maxRecommendations:n,recommendClient:r,threshold:a,transformItems:s=(e=>e),facetName:m}){const[o,l]=t.useState({recommendations:[]}),{status:g,setStatus:h}=b("loading"),{hasProvider:y,register:x}=u(),{client:R,isContextClient:p}=d(r);f({recommendClient:R});const E=t.useRef(s);return t.useEffect((()=>{E.current=s}),[s]),t.useEffect((()=>{const t={model:"trending-facets",indexName:e,facetName:m,threshold:a,maxRecommendations:n,transformItems:E.current};if(y&&p){const e=JSON.stringify(t);return x({key:e,getParameters:()=>({queries:[t],keyPair:{key:e,value:1}}),onRequest(){h("loading")},onResult(e){l({...e,recommendations:e.trendingFacets}),h("idle")}})}return h("loading"),function({recommendClient:e,transformItems:t=(e=>e),indexName:n,maxRecommendations:r,threshold:a,facetName:s}){const m={indexName:n,maxRecommendations:r,threshold:a,facetName:s};return e.addAlgoliaAgent("recommend-core",c),e.getTrendingFacets([m]).then((e=>i({maxRecommendations:r,hits:e.results.map((e=>e.hits)).flat()}))).then((e=>({recommendations:t(e)})))}({...t,recommendClient:R,facetName:m,transformItems:E.current}).then((e=>{l(e),h("idle")})),()=>{}}),[e,n,R,h,a,m,y,p,x]),{...o,status:g}}const I=function({createElement:e,Fragment:t}){return function(m){const o={title:"Trending facets",sliderLabel:"Trending facets",...m.translations},i=m.classNames??{},c=m.children??a({createElement:e,Fragment:t}),l=m.fallbackComponent??function(){return null},u=m.headerComponent??s({createElement:e,Fragment:t}),d=m.view??function({createElement:e,Fragment:t}){return function(n){return e("div",{className:r("auc-Recommend-container",n.classNames.container)},e("ol",{className:r("auc-Recommend-list",n.classNames.list)},n.items.map((a=>e("li",{key:a.facetValue,className:r("auc-Recommend-item",n.classNames.item)},e(n.itemComponent,{createElement:e,Fragment:t,item:a}))))))}}({createElement:e,Fragment:t});return c({classNames:i,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:m.items,status:m.status,translations:o,View:r=>e(d,n({classNames:i,itemComponent:m.itemComponent,items:m.items,translations:o,Fragment:t,createElement:e},r))})}}({createElement:t.createElement,Fragment:t.Fragment});function q({fallbackParameters:e,indexName:n,maxRecommendations:r,queryParameters:a,recommendClient:s,threshold:m,transformItems:o=(e=>e),facetName:l,facetValue:g}){const[h,x]=t.useState({recommendations:[]}),{status:R,setStatus:p}=b("loading"),E=y(a),N=y(e),{hasProvider:C,register:P}=u(),{client:k,isContextClient:F}=d(s);f({recommendClient:k});const I=t.useRef(o);return t.useEffect((()=>{I.current=o}),[o]),t.useEffect((()=>{const e={model:"trending-items",fallbackParameters:N,indexName:n,maxRecommendations:r,queryParameters:E,threshold:m,facetName:l,facetValue:g,transformItems:I.current};if(C&&F){const t=JSON.stringify(e);return P({key:t,getParameters:()=>({queries:[e],keyPair:{key:t,value:1}}),onRequest(){p("loading")},onResult(e){x(e),p("idle")}})}return p("loading"),function({recommendClient:e,transformItems:t=(e=>e),fallbackParameters:n,indexName:r,maxRecommendations:a,queryParameters:s,threshold:m,facetName:o,facetValue:l}){const u={fallbackParameters:n,indexName:r,maxRecommendations:a,queryParameters:s,threshold:m,facetName:o,facetValue:l};return e.addAlgoliaAgent("recommend-core",c),r?e.getTrendingItems([u]).then((e=>{return i({maxRecommendations:a,hits:(t="objectID",n=e.results.map((e=>e.hits)).flat(),[...new Map(n.map((e=>[e[t],e]))).values()])});var t,n})).then((e=>({recommendations:t(e)}))):Promise.resolve({recommendations:[]})}({...e,recommendClient:k,transformItems:I.current}).then((e=>{x(e),p("idle")})),()=>{}}),[N,n,r,E,k,p,m,l,g,C,F,P]),{...h,status:R}}const j=function({createElement:e,Fragment:t}){return function(r){const o={title:"Trending items",sliderLabel:"Trending items",...r.translations},i=r.classNames??{},c=r.children??a({createElement:e,Fragment:t}),l=r.fallbackComponent??function(){return null},u=r.headerComponent??s({createElement:e,Fragment:t}),d=r.view??m({createElement:e,Fragment:t});return c({classNames:i,Fallback:()=>e(l,{Fragment:t,createElement:e}),Header:u,recommendations:r.items,status:r.status,translations:o,View:a=>e(d,n({classNames:i,itemComponent:r.itemComponent,items:r.items,translations:o,Fragment:t,createElement:e},a))})}}({createElement:t.createElement,Fragment:t.Fragment});var v=Object.prototype.hasOwnProperty;function S(e,t,n){for(n of e.keys())if(w(n,t))return n}function w(e,t){var n,r,a;if(e===t)return!0;if(e&&t&&(n=e.constructor)===t.constructor){if(n===Date)return e.getTime()===t.getTime();if(n===RegExp)return e.toString()===t.toString();if(n===Array){if((r=e.length)===t.length)for(;r--&&w(e[r],t[r]););return-1===r}if(n===Set){if(e.size!==t.size)return!1;for(r of e){if((a=r)&&"object"==typeof a&&!(a=S(t,a)))return!1;if(!t.has(a))return!1}return!0}if(n===Map){if(e.size!==t.size)return!1;for(r of e){if((a=r[0])&&"object"==typeof a&&!(a=S(t,a)))return!1;if(!w(r[1],t.get(a)))return!1}return!0}if(n===ArrayBuffer)e=new Uint8Array(e),t=new Uint8Array(t);else if(n===DataView){if((r=e.byteLength)===t.byteLength)for(;r--&&e.getInt8(r)===t.getInt8(r););return-1===r}if(ArrayBuffer.isView(e)){if((r=e.byteLength)===t.byteLength)for(;r--&&e[r]===t[r];);return-1===r}if(!n||"object"==typeof e){for(n in r=0,e){if(v.call(e,n)&&++r&&!v.call(t,n))return!1;if(!(n in t)||!w(e[n],t[n]))return!1}return Object.keys(t).length===r}}return e!=e&&t!=t}function D(e){return null!=e}function O(e){return JSON.stringify(e)}function A(e,t){return e.map((e=>{if(function(e,t){return Boolean(e[O(t)])}(t,e.param.queries)){const n=t[O(e.param.queries)];return e.onResult(n),null}return e.param})).filter(D)}const T=(e,t)=>{const{type:n}=t;switch(n){case"register":{const{widget:n}=t,r=n.getParameters(),a=function(e,t){return Boolean(e.find((e=>w(e.param,t))))}(e.widgets,r);return a?e:{...e,widgets:[...e.widgets,{...n,param:r}],isDirty:Date.now()}}case"unregister":{const{key:n}=t;return{...e,widgets:e.widgets.filter((e=>e.key!==n))}}case"request_success":return{...e,status:"idle",isDirty:null};default:return e}};e.FrequentlyBoughtTogether=function(e){const{recommendations:r,status:a}=x(e);return t.createElement(R,n({},e,{items:r,status:a}))},e.LookingSimilar=function(e){const{recommendations:r,status:a}=P(e);return t.createElement(k,n({},e,{items:r,status:a}))},e.Recommend=function({recommendClient:e,children:n}){const[r,a]=t.useReducer(T,{isDirty:null,cache:{},widgets:[],recommendClient:e});t.useEffect((()=>{if(!r.isDirty)return;const t=A(r.widgets,r.cache);if(0===t.length)return;const{queries:n,keys:s}=function(e){return{queries:e.map((e=>e.queries)).reduce(((e,t)=>e.concat(t)),[]),keys:e.map((e=>e.keyPair))}}(t);s.forEach((e=>{r.widgets.forEach((t=>{t.key===e.key&&t.onRequest()}))})),async function({keys:e,queries:t,recommendClient:n}){n.addAlgoliaAgent("recommend-core",c);const r=await n.getRecommendations(t);let a=0,s=0;const m={};return e.forEach((e=>{const{model:n}=JSON.parse(e.key);s+=e.value;const{maxRecommendations:c,transformItems:l=(e=>e)}=t[a],u=r?.results?.slice(a,s);a+=e.value;let d=[],f=[];"trending-facets"===n?f=u.map((e=>e.hits)).flat():d="trending-items"===n?i({maxRecommendations:c,hits:u.map((e=>e.hits)).flat()}):o({maxRecommendations:c,hits:u.map((e=>e.hits)),nrOfObjs:e.value}),d=l(d),m[e.key]={recommendations:d,trendingFacets:f}})),m}({recommendClient:e,queries:n,keys:s}).then((e=>{Object.entries(e).forEach((([e,t])=>{r.widgets.forEach((n=>{n.key===e&&(n.onResult(t),r.cache[O(n.param.queries)]=t)}))})),a({type:"request_success"})}))}),[r.isDirty,a,r.widgets,r.cache,e]);const s=t.useCallback((e=>(a({type:"register",widget:e}),()=>{a({type:"unregister",key:e.key})})),[]),m=t.useMemo((()=>({register:s,recommendClient:e,hasProvider:!0})),[e,s]);return t.createElement(l.Provider,{value:m},n)},e.RecommendedForYou=function(e){const{recommendations:r,status:a}=N(e);return t.createElement(C,n({},e,{items:r,status:a}))},e.RelatedProducts=function(e){const{recommendations:r,status:a}=p(e);return t.createElement(E,n({},e,{items:r,status:a}))},e.TrendingFacets=function(e){const{recommendations:r,status:a}=F(e);return t.createElement(I,n({},e,{items:r,status:a}))},e.TrendingItems=function(e){const{recommendations:r,status:a}=q(e);return t.createElement(j,n({},e,{items:r,status:a}))},e.useFrequentlyBoughtTogether=x,e.useLookingSimilar=P,e.useRecommendations=function({fallbackParameters:e,indexName:n,maxRecommendations:r,model:a,objectIDs:s,queryParameters:m,recommendClient:i,threshold:l,transformItems:u=(e=>e)}){const[d,g]=t.useState({recommendations:[]}),{status:h,setStatus:x}=b("loading"),R=y(s),p=y(m),E=y(e);f({recommendClient:i});const N=t.useRef(u);return t.useEffect((()=>{N.current=u}),[u]),t.useEffect((()=>{x("loading"),function({objectIDs:e,recommendClient:t,transformItems:n=(e=>e),fallbackParameters:r,indexName:a,maxRecommendations:s,model:m,queryParameters:i,threshold:l}){const u=e.map((e=>({fallbackParameters:r,indexName:a,maxRecommendations:s,model:m,objectID:e,queryParameters:i,threshold:l})));return t.addAlgoliaAgent("recommend-core",c),0===u.length?Promise.resolve({recommendations:[]}):t.getRecommendations(u).then((t=>o({maxRecommendations:s,hits:t.results.map((e=>e.hits)),nrOfObjs:e.length}))).then((e=>({recommendations:n(e)})))}({fallbackParameters:E,indexName:n,maxRecommendations:r,model:a,objectIDs:R,queryParameters:p,recommendClient:i,threshold:l,transformItems:N.current}).then((e=>{g(e),x("idle")}))}),[E,n,r,a,R,p,i,x,l]),{...d,status:h}},e.useRecommendedForYou=N,e.useRelatedProducts=p,e.useTrendingFacets=F,e.useTrendingItems=q})); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@algolia/recommend-react", | ||
"description": "React package for Algolia Recommend.", | ||
"version": "1.14.0", | ||
"version": "1.15.0", | ||
"license": "MIT", | ||
@@ -32,4 +32,4 @@ "homepage": "https://github.com/algolia/recommend", | ||
"dependencies": { | ||
"@algolia/recommend-core": "1.14.0", | ||
"@algolia/recommend-vdom": "1.14.0", | ||
"@algolia/recommend-core": "1.15.0", | ||
"@algolia/recommend-vdom": "1.15.0", | ||
"dequal": "2.0.3" | ||
@@ -36,0 +36,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
+ Added@algolia/recommend-core@1.15.0(transitive)
+ Added@algolia/recommend-vdom@1.15.0(transitive)
- Removed@algolia/recommend-core@1.14.0(transitive)
- Removed@algolia/recommend-vdom@1.14.0(transitive)