@nosto/nosto-react 2.1.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/dist/index.d.ts +148 -24
- package/dist/index.es.js +255 -824
- package/dist/index.umd.js +1 -25
- package/package.json +3 -3
- package/src/components/Nosto404.tsx +17 -3
- package/src/components/NostoCategory.tsx +19 -3
- package/src/components/NostoCheckout.tsx +17 -3
- package/src/components/NostoHome.tsx +17 -3
- package/src/components/NostoOrder.tsx +19 -6
- package/src/components/NostoOther.tsx +17 -3
- package/src/components/NostoPlacement.tsx +6 -2
- package/src/components/NostoProduct.tsx +21 -8
- package/src/components/NostoProvider.tsx +8 -2
- package/src/components/NostoSearch.tsx +19 -3
- package/src/components/NostoSession.tsx +26 -7
- package/src/components/index.ts +10 -10
- package/src/hooks/useDeepCompareEffect.ts +6 -6
- package/src/hooks/useNostoApi.ts +3 -6
- package/src/hooks/useRenderCampaigns.tsx +4 -2
- package/src/utils/snakeize.ts +5 -1
- package/src/utils/types.ts +29 -0
package/dist/index.umd.js
CHANGED
|
@@ -1,25 +1 @@
|
|
|
1
|
-
(function(
|
|
2
|
-
* @license React
|
|
3
|
-
* react-jsx-runtime.production.min.js
|
|
4
|
-
*
|
|
5
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
6
|
-
*
|
|
7
|
-
* This source code is licensed under the MIT license found in the
|
|
8
|
-
* LICENSE file in the root directory of this source tree.
|
|
9
|
-
*/var de;function Ve(){if(de)return M;de=1;var r=w,n=Symbol.for("react.element"),a=Symbol.for("react.fragment"),c=Object.prototype.hasOwnProperty,i=r.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,m={key:!0,ref:!0,__self:!0,__source:!0};function R(p,g,O){var b,f={},P=null,j=null;O!==void 0&&(P=""+O),g.key!==void 0&&(P=""+g.key),g.ref!==void 0&&(j=g.ref);for(b in g)c.call(g,b)&&!m.hasOwnProperty(b)&&(f[b]=g[b]);if(p&&p.defaultProps)for(b in g=p.defaultProps,g)f[b]===void 0&&(f[b]=g[b]);return{$$typeof:n,type:p,key:P,ref:j,props:f,_owner:i.current}}return M.Fragment=a,M.jsx=R,M.jsxs=R,M}var W={};/**
|
|
10
|
-
* @license React
|
|
11
|
-
* react-jsx-runtime.development.js
|
|
12
|
-
*
|
|
13
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
14
|
-
*
|
|
15
|
-
* This source code is licensed under the MIT license found in the
|
|
16
|
-
* LICENSE file in the root directory of this source tree.
|
|
17
|
-
*/var me;function Ye(){return me||(me=1,process.env.NODE_ENV!=="production"&&function(){var r=w,n=Symbol.for("react.element"),a=Symbol.for("react.portal"),c=Symbol.for("react.fragment"),i=Symbol.for("react.strict_mode"),m=Symbol.for("react.profiler"),R=Symbol.for("react.provider"),p=Symbol.for("react.context"),g=Symbol.for("react.forward_ref"),O=Symbol.for("react.suspense"),b=Symbol.for("react.suspense_list"),f=Symbol.for("react.memo"),P=Symbol.for("react.lazy"),j=Symbol.for("react.offscreen"),F=Symbol.iterator,z="@@iterator";function J(e){if(e===null||typeof e!="object")return null;var t=F&&e[F]||e[z];return typeof t=="function"?t:null}var T=r.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;function _(e){{for(var t=arguments.length,o=new Array(t>1?t-1:0),s=1;s<t;s++)o[s-1]=arguments[s];it("error",e,o)}}function it(e,t,o){{var s=T.ReactDebugCurrentFrame,d=s.getStackAddendum();d!==""&&(t+="%s",o=o.concat([d]));var v=o.map(function(l){return String(l)});v.unshift("Warning: "+t),Function.prototype.apply.call(console[e],console,v)}}var st=!1,ct=!1,ut=!1,lt=!1,ft=!1,ve;ve=Symbol.for("react.module.reference");function dt(e){return!!(typeof e=="string"||typeof e=="function"||e===c||e===m||ft||e===i||e===O||e===b||lt||e===j||st||ct||ut||typeof e=="object"&&e!==null&&(e.$$typeof===P||e.$$typeof===f||e.$$typeof===R||e.$$typeof===p||e.$$typeof===g||e.$$typeof===ve||e.getModuleId!==void 0))}function mt(e,t,o){var s=e.displayName;if(s)return s;var d=t.displayName||t.name||"";return d!==""?o+"("+d+")":o}function pe(e){return e.displayName||"Context"}function N(e){if(e==null)return null;if(typeof e.tag=="number"&&_("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),typeof e=="function")return e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case c:return"Fragment";case a:return"Portal";case m:return"Profiler";case i:return"StrictMode";case O:return"Suspense";case b:return"SuspenseList"}if(typeof e=="object")switch(e.$$typeof){case p:var t=e;return pe(t)+".Consumer";case R:var o=e;return pe(o._context)+".Provider";case g:return mt(e,e.render,"ForwardRef");case f:var s=e.displayName||null;return s!==null?s:N(e.type)||"Memo";case P:{var d=e,v=d._payload,l=d._init;try{return N(l(v))}catch{return null}}}return null}var D=Object.assign,U=0,ge,ye,he,Re,Ee,we,be;function _e(){}_e.__reactDisabledLog=!0;function vt(){{if(U===0){ge=console.log,ye=console.info,he=console.warn,Re=console.error,Ee=console.group,we=console.groupCollapsed,be=console.groupEnd;var e={configurable:!0,enumerable:!0,value:_e,writable:!0};Object.defineProperties(console,{info:e,log:e,warn:e,error:e,group:e,groupCollapsed:e,groupEnd:e})}U++}}function pt(){{if(U--,U===0){var e={configurable:!0,enumerable:!0,writable:!0};Object.defineProperties(console,{log:D({},e,{value:ge}),info:D({},e,{value:ye}),warn:D({},e,{value:he}),error:D({},e,{value:Re}),group:D({},e,{value:Ee}),groupCollapsed:D({},e,{value:we}),groupEnd:D({},e,{value:be})})}U<0&&_("disabledDepth fell below zero. This is a bug in React. Please file an issue.")}}var te=T.ReactCurrentDispatcher,re;function G(e,t,o){{if(re===void 0)try{throw Error()}catch(d){var s=d.stack.trim().match(/\n( *(at )?)/);re=s&&s[1]||""}return`
|
|
18
|
-
`+re+e}}var ne=!1,K;{var gt=typeof WeakMap=="function"?WeakMap:Map;K=new gt}function Ce(e,t){if(!e||ne)return"";{var o=K.get(e);if(o!==void 0)return o}var s;ne=!0;var d=Error.prepareStackTrace;Error.prepareStackTrace=void 0;var v;v=te.current,te.current=null,vt();try{if(t){var l=function(){throw Error()};if(Object.defineProperty(l.prototype,"props",{set:function(){throw Error()}}),typeof Reflect=="object"&&Reflect.construct){try{Reflect.construct(l,[])}catch(x){s=x}Reflect.construct(e,[],l)}else{try{l.call()}catch(x){s=x}e.call(l.prototype)}}else{try{throw Error()}catch(x){s=x}e()}}catch(x){if(x&&s&&typeof x.stack=="string"){for(var u=x.stack.split(`
|
|
19
|
-
`),C=s.stack.split(`
|
|
20
|
-
`),y=u.length-1,E=C.length-1;y>=1&&E>=0&&u[y]!==C[E];)E--;for(;y>=1&&E>=0;y--,E--)if(u[y]!==C[E]){if(y!==1||E!==1)do if(y--,E--,E<0||u[y]!==C[E]){var S=`
|
|
21
|
-
`+u[y].replace(" at new "," at ");return e.displayName&&S.includes("<anonymous>")&&(S=S.replace("<anonymous>",e.displayName)),typeof e=="function"&&K.set(e,S),S}while(y>=1&&E>=0);break}}}finally{ne=!1,te.current=v,pt(),Error.prepareStackTrace=d}var I=e?e.displayName||e.name:"",Ie=I?G(I):"";return typeof e=="function"&&K.set(e,Ie),Ie}function yt(e,t,o){return Ce(e,!1)}function ht(e){var t=e.prototype;return!!(t&&t.isReactComponent)}function $(e,t,o){if(e==null)return"";if(typeof e=="function")return Ce(e,ht(e));if(typeof e=="string")return G(e);switch(e){case O:return G("Suspense");case b:return G("SuspenseList")}if(typeof e=="object")switch(e.$$typeof){case g:return yt(e.render);case f:return $(e.type,t,o);case P:{var s=e,d=s._payload,v=s._init;try{return $(v(d),t,o)}catch{}}}return""}var X=Object.prototype.hasOwnProperty,Pe={},Se=T.ReactDebugCurrentFrame;function q(e){if(e){var t=e._owner,o=$(e.type,e._source,t?t.type:null);Se.setExtraStackFrame(o)}else Se.setExtraStackFrame(null)}function Rt(e,t,o,s,d){{var v=Function.call.bind(X);for(var l in e)if(v(e,l)){var u=void 0;try{if(typeof e[l]!="function"){var C=Error((s||"React class")+": "+o+" type `"+l+"` is invalid; it must be a function, usually from the `prop-types` package, but received `"+typeof e[l]+"`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`.");throw C.name="Invariant Violation",C}u=e[l](t,l,s,o,null,"SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED")}catch(y){u=y}u&&!(u instanceof Error)&&(q(d),_("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).",s||"React class",o,l,typeof u),q(null)),u instanceof Error&&!(u.message in Pe)&&(Pe[u.message]=!0,q(d),_("Failed %s type: %s",o,u.message),q(null))}}}var Et=Array.isArray;function oe(e){return Et(e)}function wt(e){{var t=typeof Symbol=="function"&&Symbol.toStringTag,o=t&&e[Symbol.toStringTag]||e.constructor.name||"Object";return o}}function bt(e){try{return Oe(e),!1}catch{return!0}}function Oe(e){return""+e}function Te(e){if(bt(e))return _("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.",wt(e)),Oe(e)}var B=T.ReactCurrentOwner,_t={key:!0,ref:!0,__self:!0,__source:!0},je,Ne,ae;ae={};function Ct(e){if(X.call(e,"ref")){var t=Object.getOwnPropertyDescriptor(e,"ref").get;if(t&&t.isReactWarning)return!1}return e.ref!==void 0}function Pt(e){if(X.call(e,"key")){var t=Object.getOwnPropertyDescriptor(e,"key").get;if(t&&t.isReactWarning)return!1}return e.key!==void 0}function St(e,t){if(typeof e.ref=="string"&&B.current&&t&&B.current.stateNode!==t){var o=N(B.current.type);ae[o]||(_('Component "%s" contains the string ref "%s". Support for string refs will be removed in a future major release. This case cannot be automatically converted to an arrow function. We ask you to manually fix this case by using useRef() or createRef() instead. Learn more about using refs safely here: https://reactjs.org/link/strict-mode-string-ref',N(B.current.type),e.ref),ae[o]=!0)}}function Ot(e,t){{var o=function(){je||(je=!0,_("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",t))};o.isReactWarning=!0,Object.defineProperty(e,"key",{get:o,configurable:!0})}}function Tt(e,t){{var o=function(){Ne||(Ne=!0,_("%s: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",t))};o.isReactWarning=!0,Object.defineProperty(e,"ref",{get:o,configurable:!0})}}var jt=function(e,t,o,s,d,v,l){var u={$$typeof:n,type:e,key:t,ref:o,props:l,_owner:v};return u._store={},Object.defineProperty(u._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:!1}),Object.defineProperty(u,"_self",{configurable:!1,enumerable:!1,writable:!1,value:s}),Object.defineProperty(u,"_source",{configurable:!1,enumerable:!1,writable:!1,value:d}),Object.freeze&&(Object.freeze(u.props),Object.freeze(u)),u};function Nt(e,t,o,s,d){{var v,l={},u=null,C=null;o!==void 0&&(Te(o),u=""+o),Pt(t)&&(Te(t.key),u=""+t.key),Ct(t)&&(C=t.ref,St(t,d));for(v in t)X.call(t,v)&&!_t.hasOwnProperty(v)&&(l[v]=t[v]);if(e&&e.defaultProps){var y=e.defaultProps;for(v in y)l[v]===void 0&&(l[v]=y[v])}if(u||C){var E=typeof e=="function"?e.displayName||e.name||"Unknown":e;u&&Ot(l,E),C&&Tt(l,E)}return jt(e,u,C,d,s,B.current,l)}}var ie=T.ReactCurrentOwner,xe=T.ReactDebugCurrentFrame;function L(e){if(e){var t=e._owner,o=$(e.type,e._source,t?t.type:null);xe.setExtraStackFrame(o)}else xe.setExtraStackFrame(null)}var se;se=!1;function ce(e){return typeof e=="object"&&e!==null&&e.$$typeof===n}function ke(){{if(ie.current){var e=N(ie.current.type);if(e)return`
|
|
22
|
-
|
|
23
|
-
Check the render method of \``+e+"`."}return""}}function xt(e){return""}var Ae={};function kt(e){{var t=ke();if(!t){var o=typeof e=="string"?e:e.displayName||e.name;o&&(t=`
|
|
24
|
-
|
|
25
|
-
Check the top-level render call using <`+o+">.")}return t}}function De(e,t){{if(!e._store||e._store.validated||e.key!=null)return;e._store.validated=!0;var o=kt(t);if(Ae[o])return;Ae[o]=!0;var s="";e&&e._owner&&e._owner!==ie.current&&(s=" It was passed a child from "+N(e._owner.type)+"."),L(e),_('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.',o,s),L(null)}}function Fe(e,t){{if(typeof e!="object")return;if(oe(e))for(var o=0;o<e.length;o++){var s=e[o];ce(s)&&De(s,t)}else if(ce(e))e._store&&(e._store.validated=!0);else if(e){var d=J(e);if(typeof d=="function"&&d!==e.entries)for(var v=d.call(e),l;!(l=v.next()).done;)ce(l.value)&&De(l.value,t)}}}function At(e){{var t=e.type;if(t==null||typeof t=="string")return;var o;if(typeof t=="function")o=t.propTypes;else if(typeof t=="object"&&(t.$$typeof===g||t.$$typeof===f))o=t.propTypes;else return;if(o){var s=N(t);Rt(o,e.props,"prop",s,e)}else if(t.PropTypes!==void 0&&!se){se=!0;var d=N(t);_("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?",d||"Unknown")}typeof t.getDefaultProps=="function"&&!t.getDefaultProps.isReactClassApproved&&_("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead.")}}function Dt(e){{for(var t=Object.keys(e.props),o=0;o<t.length;o++){var s=t[o];if(s!=="children"&&s!=="key"){L(e),_("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.",s),L(null);break}}e.ref!==null&&(L(e),_("Invalid attribute `ref` supplied to `React.Fragment`."),L(null))}}function Le(e,t,o,s,d,v){{var l=dt(e);if(!l){var u="";(e===void 0||typeof e=="object"&&e!==null&&Object.keys(e).length===0)&&(u+=" You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.");var C=xt();C?u+=C:u+=ke();var y;e===null?y="null":oe(e)?y="array":e!==void 0&&e.$$typeof===n?(y="<"+(N(e.type)||"Unknown")+" />",u=" Did you accidentally export a JSX literal instead of a component?"):y=typeof e,_("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s",y,u)}var E=Nt(e,t,o,d,v);if(E==null)return E;if(l){var S=t.children;if(S!==void 0)if(s)if(oe(S)){for(var I=0;I<S.length;I++)Fe(S[I],e);Object.freeze&&Object.freeze(S)}else _("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else Fe(S,e)}return e===c?Dt(E):At(E),E}}function Ft(e,t,o){return Le(e,t,o,!0)}function Lt(e,t,o){return Le(e,t,o,!1)}var It=Lt,Mt=Ft;W.Fragment=c,W.jsx=It,W.jsxs=Mt}()),W}process.env.NODE_ENV==="production"?ee.exports=Ve():ee.exports=Ye();var V=ee.exports;function Ue(r){return w.cloneElement(r.recommendationComponent,{nostoRecommendation:r.nostoRecommendation})}function Be(r){if(!window.nostojs)throw new Error("Nosto has not yet been initialized");window.nostojs(n=>{n.placements.injectCampaigns(r.recommendations)})}function A(){const{responseMode:r,recommendationComponent:n}=H(),a=w.useRef({});if(r=="HTML")return{renderCampaigns:Be};function c(i){var R;const m=((R=i.campaigns)==null?void 0:R.recommendations)??{};for(const p in m){const g=m[p],O="#"+p,b=document.querySelector(O);b&&(a.current[p]||(a.current[p]=Me.createRoot(b)),a.current[p].render(V.jsx(Ue,{recommendationComponent:n,nostoRecommendation:g})))}}return{renderCampaigns:c}}function He(){return typeof window.nosto<"u"}function ze(r,n){return new Promise((a,c)=>{const i=document.createElement("script");i.type="text/javascript",i.src=r,i.async=!0,i.onload=()=>a(),i.onerror=()=>c(),Object.entries((n==null?void 0:n.attributes)??{}).forEach(([m,R])=>i.setAttribute(m,R)),(n==null?void 0:n.position)==="head"?document.head.appendChild(i):document.body.appendChild(i)})}function Je(r){const{host:n="connect.nosto.com",scriptLoader:a=ze,account:c,shopifyMarkets:i,loadScript:m=!0}=r,[R,p]=w.useState(!1);return w.useEffect(()=>{function g(){var f;"nostoReactTest"in window&&((f=window.nosto)==null||f.reload({site:"localhost"})),p(!0)}async function O(f,P={}){const j=`//${n}${f}`,F={"nosto-client-script":"",...P};await a(j,{attributes:F}),g()}function b(){var z,J;const f=document.querySelector("[nosto-client-script]"),P=String((i==null?void 0:i.marketId)||""),j=(i==null?void 0:i.language)||"",F=(f==null?void 0:f.getAttribute("nosto-language"))!==j||(f==null?void 0:f.getAttribute("nosto-market-id"))!==P;if(!f||F){R&&p(!1);const T=document.querySelector("#nosto-sandbox");(z=f==null?void 0:f.parentNode)==null||z.removeChild(f),(J=T==null?void 0:T.parentNode)==null||J.removeChild(T);const _=`/script/shopify/market/nosto.js?merchant=${c}&market=${P}&locale=${j.toLowerCase()}`;O(_,{"nosto-language":j,"nosto-market-id":P})}}if(window.nostojs||(window.nostojs=f=>{(window.nostojs.q=window.nostojs.q||[]).push(f)},window.nostojs(f=>f.setAutoLoad(!1))),!m){window.nosto?g():window.nostojs(g);return}if(!He()&&!i){const f=`/include/${c}`;O(f)}i&&b()},[i==null?void 0:i.marketId,i==null?void 0:i.language]),{clientScriptLoaded:R}}function Ge(r){const{renderCampaigns:n}=A();return k(async a=>{const c=await a.defaultSession().viewNotFound().setPlacements(r.placements||a.placements.getPlacements()).load();n(c)}),null}function Ke(r){const{renderCampaigns:n}=A();return k(async a=>{const c=await a.defaultSession().viewOther().setPlacements(r.placements||a.placements.getPlacements()).load();n(c)}),null}function $e(r){const{renderCampaigns:n}=A();return k(async a=>{const c=await a.defaultSession().viewCart().setPlacements(r.placements||a.placements.getPlacements()).load();n(c)}),null}function Xe(r){const{product:n,tagging:a,placements:c}=r,{renderCampaigns:i}=A();return k(async m=>{const R=await m.defaultSession().viewProduct(a??n).setPlacements(c||m.placements.getPlacements()).load();i(R)},[n,a==null?void 0:a.selected_sku_id]),null}function qe(r){const{category:n,placements:a}=r,{renderCampaigns:c}=A();return k(async i=>{const m=await i.defaultSession().viewCategory(n).setPlacements(a||i.placements.getPlacements()).load();c(m)},[n]),null}function Ze(r){const{query:n,placements:a}=r,{renderCampaigns:c}=A();return k(async i=>{const m=await i.defaultSession().viewSearch(n).setPlacements(a||i.placements.getPlacements()).load();c(m)},[n]),null}function Y(r){return!r||typeof r!="object"||Qe(r)||et(r)?r:Array.isArray(r)?r.map(Y):Object.keys(r).reduce((n,a)=>{const c=a[0].toLowerCase()+a.slice(1).replace(/([A-Z]+)/g,(i,m)=>"_"+m.toLowerCase());return n[c]=Y(r[a]),n},{})}function Qe(r){return Object.prototype.toString.call(r)==="[object Date]"}function et(r){return Object.prototype.toString.call(r)==="[object RegExp]"}function tt(r){const{order:n,placements:a}=r,{renderCampaigns:c}=A();return k(async i=>{const m=await i.defaultSession().addOrder(Y(n)).setPlacements(a||i.placements.getPlacements()).load();c(m)},[n],{deep:!0}),null}function rt(r){const{renderCampaigns:n}=A();return k(async a=>{const c=await a.defaultSession().viewFrontPage().setPlacements(r.placements||a.placements.getPlacements()).load();n(c)}),null}function nt(r){const{id:n,pageType:a}=r;return V.jsx("div",{className:"nosto_element",id:n},n+(a||""))}function ot(r){const{account:n,multiCurrency:a=!1,children:c,recommendationComponent:i}=r,m=a?r.currentVariation:"";if(i&&!w.isValidElement(i))throw new Error("The recommendationComponent prop must be a valid React element. Please provide a valid React element.");const R=i?"JSON_ORIGINAL":"HTML",{clientScriptLoaded:p}=Je(r);return V.jsx(Q.Provider,{value:{account:n,clientScriptLoaded:p,currentVariation:m,responseMode:R,recommendationComponent:i},children:c})}function at(r){const{cart:n,customer:a}=r??{},{clientScriptLoaded:c}=H();return fe(()=>{const i=n?Y(n):void 0,m=a?Y(a):void 0;c&&window.nostojs(R=>{R.defaultSession().setResponseMode("HTML").setCart(i).setCustomer(m).viewOther().load()})},[c,n,a]),V.jsx(V.Fragment,{})}h.Nosto404=Ge,h.NostoCategory=qe,h.NostoCheckout=$e,h.NostoContext=Q,h.NostoHome=rt,h.NostoOrder=tt,h.NostoOther=Ke,h.NostoPlacement=nt,h.NostoProduct=Xe,h.NostoProvider=ot,h.NostoSearch=Ze,h.NostoSession=at,h.useNostoContext=H,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(r,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("react"),require("react/jsx-runtime"),require("react-dom")):typeof define=="function"&&define.amd?define(["exports","react","react/jsx-runtime","react-dom"],a):(r=typeof globalThis<"u"?globalThis:r||self,a(r["@nosto/nosto-react"]={},r.React,r["react/jsx-runtime"],r.ReactDOM))})(this,function(r,a,g,z){"use strict";const p=e=>String(e)==="[object Object]";function L(e){if(!p(e))return!1;const t=e.constructor;if(t===void 0)return!0;const n=t.prototype;return!(!p(n)||!n.hasOwnProperty("isPrototypeOf"))}function j(e,t){if(e===t)return!0;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(e instanceof Array&&t instanceof Array)return e.length!==t.length?!1:e.every((n,s)=>j(n,t[s]));if(L(e)&&L(t)){const n=Object.entries(e);return n.length!==Object.keys(t).length?!1:n.every(([s,o])=>j(o,t[s]))}return!1}function _(e,t){return a.useEffect(e,U(t))}function U(e){const t=a.useRef(e),n=a.useRef(0);return j(e,t.current)||(t.current=e,n.current+=1),a.useMemo(()=>t.current,[n.current])}const v=a.createContext({account:"",currentVariation:"",responseMode:"HTML",clientScriptLoaded:!1});function S(){return a.useContext(v)}function l(e,t,n){const{clientScriptLoaded:s}=S();(n!=null&&n.deep?_:a.useEffect)(()=>{s&&window.nostojs(e)},[s,...t??[]])}var E,y=z;if(process.env.NODE_ENV==="production")E=y.createRoot,y.hydrateRoot;else{var A=y.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;E=function(e,t){A.usingClientEntryPoint=!0;try{return y.createRoot(e,t)}finally{A.usingClientEntryPoint=!1}}}function W(e){return a.cloneElement(e.recommendationComponent,{nostoRecommendation:e.nostoRecommendation})}function B(e){if(!window.nostojs)throw new Error("Nosto has not yet been initialized");window.nostojs(t=>{t.placements.injectCampaigns(e.recommendations)})}function m(){const{responseMode:e,recommendationComponent:t}=S(),n=a.useRef({});if(e=="HTML")return{renderCampaigns:B};function s(o){var d;const i=((d=o.campaigns)==null?void 0:d.recommendations)??{};for(const u in i){const f=i[u],P="#"+u,h=document.querySelector(P);h&&(n.current[u]||(n.current[u]=E(h)),n.current[u].render(g.jsx(W,{recommendationComponent:t,nostoRecommendation:f})))}}return{renderCampaigns:s}}function G(){return typeof window.nosto<"u"}function J(e,t){return new Promise((n,s)=>{const o=document.createElement("script");o.type="text/javascript",o.src=e,o.async=!0,o.onload=()=>n(),o.onerror=()=>s(),Object.entries((t==null?void 0:t.attributes)??{}).forEach(([i,d])=>o.setAttribute(i,d)),(t==null?void 0:t.position)==="head"?document.head.appendChild(o):document.body.appendChild(o)})}function Y(e){const{host:t="connect.nosto.com",scriptLoader:n=J,account:s,shopifyMarkets:o,loadScript:i=!0}=e,[d,u]=a.useState(!1);return a.useEffect(()=>{function f(){var c;"nostoReactTest"in window&&((c=window.nosto)==null||c.reload({site:"localhost"})),u(!0)}async function P(c,w={}){const C=`//${t}${c}`,R={"nosto-client-script":"",...w};await n(C,{attributes:R}),f()}function h(){var b,F;const c=document.querySelector("[nosto-client-script]"),w=String((o==null?void 0:o.marketId)||""),C=(o==null?void 0:o.language)||"",R=(c==null?void 0:c.getAttribute("nosto-language"))!==C||(c==null?void 0:c.getAttribute("nosto-market-id"))!==w;if(!c||R){d&&u(!1);const O=document.querySelector("#nosto-sandbox");(b=c==null?void 0:c.parentNode)==null||b.removeChild(c),(F=O==null?void 0:O.parentNode)==null||F.removeChild(O);const ie=`/script/shopify/market/nosto.js?merchant=${s}&market=${w}&locale=${C.toLowerCase()}`;P(ie,{"nosto-language":C,"nosto-market-id":w})}}if(window.nostojs||(window.nostojs=c=>{(window.nostojs.q=window.nostojs.q||[]).push(c)},window.nostojs(c=>c.setAutoLoad(!1))),!i){window.nosto?f():window.nostojs(f);return}if(!G()&&!o){const c=`/include/${s}`;P(c)}o&&h()},[o==null?void 0:o.marketId,o==null?void 0:o.language]),{clientScriptLoaded:d}}function Z(e){return T(e),null}function T(e){const{renderCampaigns:t}=m();l(async n=>{const s=await n.defaultSession().viewNotFound().setPlacements((e==null?void 0:e.placements)||n.placements.getPlacements()).load();t(s)})}function K(e){return D(e),null}function D(e){const{renderCampaigns:t}=m();l(async n=>{const s=await n.defaultSession().viewOther().setPlacements((e==null?void 0:e.placements)||n.placements.getPlacements()).load();t(s)})}function Q(e){return k(e),null}function k(e){const{renderCampaigns:t}=m();l(async n=>{const s=await n.defaultSession().viewCart().setPlacements((e==null?void 0:e.placements)||n.placements.getPlacements()).load();t(s)})}function X(e){return I(e),null}function I({product:e,tagging:t,placements:n}){const{renderCampaigns:s}=m();l(async o=>{const i=await o.defaultSession().viewProduct(t??e).setPlacements(n||o.placements.getPlacements()).load();s(i)},[e,t==null?void 0:t.selected_sku_id])}function x(e){return q(e),null}function q({category:e,placements:t}){const{renderCampaigns:n}=m();l(async s=>{const o=await s.defaultSession().viewCategory(e).setPlacements(t||s.placements.getPlacements()).load();n(o)},[e])}function ee(e){return H(e),null}function H({query:e,placements:t}){const{renderCampaigns:n}=m();l(async s=>{const o=await s.defaultSession().viewSearch(e).setPlacements(t||s.placements.getPlacements()).load();n(o)},[e])}function N(e){return!e||typeof e!="object"||te(e)||ne(e)?e:Array.isArray(e)?e.map(N):Object.keys(e).reduce((t,n)=>{const s=n[0].toLowerCase()+n.slice(1).replace(/([A-Z]+)/g,(o,i)=>"_"+i.toLowerCase());return t[s]=N(e[n]),t},{})}function te(e){return Object.prototype.toString.call(e)==="[object Date]"}function ne(e){return Object.prototype.toString.call(e)==="[object RegExp]"}function oe(e){return V(e),null}function V({order:e,placements:t}){const{renderCampaigns:n}=m();l(async s=>{const o=await s.defaultSession().addOrder(N(e)).setPlacements(t||s.placements.getPlacements()).load();n(o)},[e],{deep:!0})}function se(e){return $(e),null}function $(e){const{renderCampaigns:t}=m();l(async n=>{const s=await n.defaultSession().viewFrontPage().setPlacements((e==null?void 0:e.placements)||n.placements.getPlacements()).load();t(s)})}function re({id:e,pageType:t}){return g.jsx("div",{className:"nosto_element",id:e},e+(t||""))}function ce(e){const{account:t,multiCurrency:n=!1,children:s,recommendationComponent:o}=e,i=n?e.currentVariation:"";if(o&&!a.isValidElement(o))throw new Error("The recommendationComponent prop must be a valid React element. Please provide a valid React element.");const d=o?"JSON_ORIGINAL":"HTML",{clientScriptLoaded:u}=Y(e);return u&&window.nostojs(f=>{f.defaultSession().setVariation(i).setResponseMode(d)}),g.jsx(v.Provider,{value:{account:t,clientScriptLoaded:u,currentVariation:i,responseMode:d,recommendationComponent:o},children:s})}function ae(e){return M(e),null}function M({cart:e,customer:t}={}){const{clientScriptLoaded:n}=S();_(()=>{const s=e?N(e):void 0,o=t?N(t):void 0;n&&window.nostojs(i=>{i.defaultSession().setCart(s).setCustomer(o).viewOther().load({skipPageViews:!0})})},[n,e,t])}r.Nosto404=Z,r.NostoCategory=x,r.NostoCheckout=Q,r.NostoContext=v,r.NostoHome=se,r.NostoOrder=oe,r.NostoOther=K,r.NostoPlacement=re,r.NostoProduct=X,r.NostoProvider=ce,r.NostoSearch=ee,r.NostoSession=ae,r.useNosto404=T,r.useNostoCategory=q,r.useNostoCheckout=k,r.useNostoContext=S,r.useNostoHome=$,r.useNostoOrder=V,r.useNostoOther=D,r.useNostoProduct=I,r.useNostoSearch=H,r.useNostoSession=M,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nosto/nosto-react",
|
|
3
3
|
"description": "Component library to simply implementing Nosto on React.",
|
|
4
|
-
"version": "2.1
|
|
4
|
+
"version": "2.2.1",
|
|
5
5
|
"author": "Mridang Agarwalla, Dominik Gilg",
|
|
6
6
|
"license": "ISC",
|
|
7
7
|
"repository": {
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"eslint-plugin-promise": "^6.1.1",
|
|
44
44
|
"eslint-plugin-react": "^7.33.2",
|
|
45
45
|
"prettier": "^2.0.5",
|
|
46
|
-
"react": "
|
|
47
|
-
"react-dom": "
|
|
46
|
+
"react": "^18.3.1",
|
|
47
|
+
"react-dom": "^18.3.1",
|
|
48
48
|
"react-router": "^6.25.1",
|
|
49
49
|
"react-router-dom": "^6.25.1",
|
|
50
50
|
"rimraf": "^3.0.2",
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { useNostoApi, useRenderCampaigns } from "../hooks"
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @group Components
|
|
5
|
+
*/
|
|
6
|
+
export type Nosto404Props = { placements?: string[] }
|
|
7
|
+
|
|
3
8
|
/**
|
|
4
9
|
* You can personalise your cart and checkout pages by using the `Nosto404` component.
|
|
5
10
|
* The component does not require any props.
|
|
@@ -20,16 +25,25 @@ import { useNostoApi, useRenderCampaigns } from "../hooks"
|
|
|
20
25
|
*
|
|
21
26
|
* @group Components
|
|
22
27
|
*/
|
|
23
|
-
export default function Nosto404(props:
|
|
28
|
+
export default function Nosto404(props: Nosto404Props) {
|
|
29
|
+
useNosto404(props)
|
|
30
|
+
return null
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* You can personalise your cart and checkout pages by using the `useNosto404` hook.
|
|
35
|
+
*
|
|
36
|
+
* @group Hooks
|
|
37
|
+
*/
|
|
38
|
+
export function useNosto404(props?: Nosto404Props) {
|
|
24
39
|
const { renderCampaigns } = useRenderCampaigns()
|
|
25
40
|
|
|
26
41
|
useNostoApi(
|
|
27
42
|
async (api) => {
|
|
28
43
|
const data = await api.defaultSession()
|
|
29
44
|
.viewNotFound()
|
|
30
|
-
.setPlacements(props
|
|
45
|
+
.setPlacements(props?.placements || api.placements.getPlacements())
|
|
31
46
|
.load()
|
|
32
47
|
renderCampaigns(data)
|
|
33
48
|
})
|
|
34
|
-
return null
|
|
35
49
|
}
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { useNostoApi, useRenderCampaigns } from "../hooks"
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @group Components
|
|
5
|
+
*/
|
|
6
|
+
export type NostoCategoryProps = {
|
|
7
|
+
category: string
|
|
8
|
+
placements?: string[]
|
|
9
|
+
}
|
|
10
|
+
|
|
3
11
|
/**
|
|
4
12
|
* You can personalise your category and collection pages by using the NostoCategory component.
|
|
5
13
|
* The component requires that you provide it the the slash-delimited slug representation of the current category.
|
|
@@ -22,8 +30,17 @@ import { useNostoApi, useRenderCampaigns } from "../hooks"
|
|
|
22
30
|
*
|
|
23
31
|
* @group Components
|
|
24
32
|
*/
|
|
25
|
-
export default function NostoCategory(props:
|
|
26
|
-
|
|
33
|
+
export default function NostoCategory(props: NostoCategoryProps) {
|
|
34
|
+
useNostoCategory(props)
|
|
35
|
+
return null
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* You can personalise your category and collection pages by using the useNostoCategory hook.
|
|
40
|
+
*
|
|
41
|
+
* @group Hooks
|
|
42
|
+
*/
|
|
43
|
+
export function useNostoCategory({ category, placements }: NostoCategoryProps) {
|
|
27
44
|
const { renderCampaigns } = useRenderCampaigns()
|
|
28
45
|
|
|
29
46
|
useNostoApi(
|
|
@@ -36,5 +53,4 @@ export default function NostoCategory(props: { category: string; placements?: st
|
|
|
36
53
|
},
|
|
37
54
|
[category]
|
|
38
55
|
)
|
|
39
|
-
return null
|
|
40
56
|
}
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { useNostoApi, useRenderCampaigns } from "../hooks"
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @group Components
|
|
5
|
+
*/
|
|
6
|
+
export type NostoCheckoutProps = { placements?: string[] }
|
|
7
|
+
|
|
3
8
|
/**
|
|
4
9
|
* You can personalise your cart and checkout pages by using the NostoCheckout component.
|
|
5
10
|
* The component does not require any props.
|
|
@@ -19,16 +24,25 @@ import { useNostoApi, useRenderCampaigns } from "../hooks"
|
|
|
19
24
|
*
|
|
20
25
|
* @group Components
|
|
21
26
|
*/
|
|
22
|
-
export default function NostoCheckout(props:
|
|
27
|
+
export default function NostoCheckout(props: NostoCheckoutProps) {
|
|
28
|
+
useNostoCheckout(props)
|
|
29
|
+
return null
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* You can personalise your cart and checkout pages by using the useNostoCheckout hook.
|
|
34
|
+
*
|
|
35
|
+
* @group Hooks
|
|
36
|
+
*/
|
|
37
|
+
export function useNostoCheckout(props?: NostoCheckoutProps) {
|
|
23
38
|
const { renderCampaigns } = useRenderCampaigns()
|
|
24
39
|
|
|
25
40
|
useNostoApi(
|
|
26
41
|
async (api) => {
|
|
27
42
|
const data = await api.defaultSession()
|
|
28
43
|
.viewCart()
|
|
29
|
-
.setPlacements(props
|
|
44
|
+
.setPlacements(props?.placements || api.placements.getPlacements())
|
|
30
45
|
.load()
|
|
31
46
|
renderCampaigns(data)
|
|
32
47
|
})
|
|
33
|
-
return null
|
|
34
48
|
}
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { useRenderCampaigns, useNostoApi } from "../hooks"
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @group Components
|
|
5
|
+
*/
|
|
6
|
+
export type NostoHomeProps = { placements?: string[] }
|
|
7
|
+
|
|
3
8
|
/**
|
|
4
9
|
* The `NostoHome` component must be used to personalise the home page. The component does not require any props.
|
|
5
10
|
*
|
|
@@ -23,17 +28,26 @@ import { useRenderCampaigns, useNostoApi } from "../hooks"
|
|
|
23
28
|
*
|
|
24
29
|
* @group Components
|
|
25
30
|
*/
|
|
26
|
-
export default function NostoHome(props:
|
|
31
|
+
export default function NostoHome(props: NostoHomeProps) {
|
|
32
|
+
useNostoHome(props)
|
|
33
|
+
return null
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* You can personalise your home page by using the useNostoHome hook.
|
|
38
|
+
*
|
|
39
|
+
* @group Hooks
|
|
40
|
+
*/
|
|
41
|
+
export function useNostoHome(props?: NostoHomeProps) {
|
|
27
42
|
const { renderCampaigns } = useRenderCampaigns()
|
|
28
43
|
|
|
29
44
|
useNostoApi(
|
|
30
45
|
async (api) => {
|
|
31
46
|
const data = await api.defaultSession()
|
|
32
47
|
.viewFrontPage()
|
|
33
|
-
.setPlacements(props
|
|
48
|
+
.setPlacements(props?.placements || api.placements.getPlacements())
|
|
34
49
|
.load()
|
|
35
50
|
renderCampaigns(data)
|
|
36
51
|
}
|
|
37
52
|
)
|
|
38
|
-
return null
|
|
39
53
|
}
|
|
@@ -2,6 +2,14 @@ import { Order } from "../types"
|
|
|
2
2
|
import { useRenderCampaigns, useNostoApi } from "../hooks"
|
|
3
3
|
import { snakeize } from "../utils/snakeize"
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* @group Components
|
|
7
|
+
*/
|
|
8
|
+
export type NostoOrderProps = {
|
|
9
|
+
order: Order
|
|
10
|
+
placements?: string[]
|
|
11
|
+
}
|
|
12
|
+
|
|
5
13
|
/**
|
|
6
14
|
* You can personalise your order-confirmation/thank-you page by using the `NostoOrder` component.
|
|
7
15
|
* The component requires that you provide it with the details of the order.
|
|
@@ -21,11 +29,17 @@ import { snakeize } from "../utils/snakeize"
|
|
|
21
29
|
*
|
|
22
30
|
* @group Components
|
|
23
31
|
*/
|
|
24
|
-
export default function NostoOrder(props: {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
32
|
+
export default function NostoOrder(props: NostoOrderProps) {
|
|
33
|
+
useNostoOrder(props)
|
|
34
|
+
return null
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* You can personalise your order-confirmation/thank-you page by using the `useNostoOrder` hook.
|
|
39
|
+
*
|
|
40
|
+
* @group Hooks
|
|
41
|
+
*/
|
|
42
|
+
export function useNostoOrder({ order, placements }: NostoOrderProps) {
|
|
29
43
|
const { renderCampaigns } = useRenderCampaigns()
|
|
30
44
|
|
|
31
45
|
useNostoApi(
|
|
@@ -39,5 +53,4 @@ export default function NostoOrder(props: {
|
|
|
39
53
|
[order],
|
|
40
54
|
{ deep: true }
|
|
41
55
|
)
|
|
42
|
-
return null
|
|
43
56
|
}
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { useRenderCampaigns, useNostoApi } from "../hooks"
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @group Components
|
|
5
|
+
*/
|
|
6
|
+
export type NostoOtherProps = { placements?: string[] }
|
|
7
|
+
|
|
3
8
|
/**
|
|
4
9
|
* You can personalise your miscellaneous pages by using the NostoOther component.
|
|
5
10
|
* The component does not require any props.
|
|
@@ -19,16 +24,25 @@ import { useRenderCampaigns, useNostoApi } from "../hooks"
|
|
|
19
24
|
*
|
|
20
25
|
* @group Components
|
|
21
26
|
*/
|
|
22
|
-
export default function NostoOther(props:
|
|
27
|
+
export default function NostoOther(props: NostoOtherProps) {
|
|
28
|
+
useNostoOther(props)
|
|
29
|
+
return null
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* You can personalise your miscellaneous pages by using the useNostoOther hook.
|
|
34
|
+
*
|
|
35
|
+
* @group Hooks
|
|
36
|
+
*/
|
|
37
|
+
export function useNostoOther(props?: NostoOtherProps) {
|
|
23
38
|
const { renderCampaigns } = useRenderCampaigns()
|
|
24
39
|
|
|
25
40
|
useNostoApi(
|
|
26
41
|
async (api) => {
|
|
27
42
|
const data = await api.defaultSession()
|
|
28
43
|
.viewOther()
|
|
29
|
-
.setPlacements(props
|
|
44
|
+
.setPlacements(props?.placements || api.placements.getPlacements())
|
|
30
45
|
.load()
|
|
31
46
|
renderCampaigns(data)
|
|
32
47
|
})
|
|
33
|
-
return null
|
|
34
48
|
}
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @group Components
|
|
3
|
+
*/
|
|
4
|
+
export type NostoPlacementProps = { id: string; pageType?: string }
|
|
5
|
+
|
|
1
6
|
/**
|
|
2
7
|
* Nosto React has a special component called NostoPlacement.
|
|
3
8
|
* The component is a simply a hidden `<div>` placeholder into which Nosto injects recommendations or personalises the content between the tags.
|
|
@@ -11,7 +16,6 @@
|
|
|
11
16
|
*
|
|
12
17
|
* @group Components
|
|
13
18
|
*/
|
|
14
|
-
export default function NostoPlacement(
|
|
15
|
-
const { id, pageType } = props
|
|
19
|
+
export default function NostoPlacement({ id, pageType }: NostoPlacementProps) {
|
|
16
20
|
return <div className="nosto_element" id={id} key={id + (pageType || "")} />
|
|
17
21
|
}
|
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import { useRenderCampaigns, useNostoApi } from "../hooks"
|
|
2
2
|
import { Product } from "../types"
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @group Components
|
|
6
|
+
*/
|
|
7
|
+
export type NostoProductProps = {
|
|
8
|
+
product: string
|
|
9
|
+
tagging?: Product
|
|
10
|
+
placements?: string[]
|
|
11
|
+
}
|
|
12
|
+
|
|
4
13
|
/**
|
|
5
14
|
* The NostoProduct component must be used to personalise the product page.
|
|
6
15
|
* The component requires that you provide it the identifier of the current product being viewed.
|
|
@@ -25,12 +34,17 @@ import { Product } from "../types"
|
|
|
25
34
|
*
|
|
26
35
|
* @group Components
|
|
27
36
|
*/
|
|
28
|
-
export default function NostoProduct(props: {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
37
|
+
export default function NostoProduct(props: NostoProductProps) {
|
|
38
|
+
useNostoProduct(props)
|
|
39
|
+
return null
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* You can personalise your product pages by using the useNostoProduct hook.
|
|
44
|
+
*
|
|
45
|
+
* @group Hooks
|
|
46
|
+
*/
|
|
47
|
+
export function useNostoProduct({ product, tagging, placements }: NostoProductProps) {
|
|
34
48
|
const { renderCampaigns } = useRenderCampaigns()
|
|
35
49
|
|
|
36
50
|
useNostoApi(
|
|
@@ -43,5 +57,4 @@ export default function NostoProduct(props: {
|
|
|
43
57
|
},
|
|
44
58
|
[product, tagging?.selected_sku_id]
|
|
45
59
|
)
|
|
46
|
-
|
|
47
|
-
}
|
|
60
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isValidElement } from "react"
|
|
2
2
|
import { NostoContext, RecommendationComponent } from "../context"
|
|
3
3
|
import { useLoadClientScript } from "../hooks"
|
|
4
|
-
import type {
|
|
4
|
+
import type { ReactNode } from "react"
|
|
5
5
|
import { ScriptLoadOptions } from "../hooks/scriptLoader"
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -23,7 +23,7 @@ export interface NostoProviderProps {
|
|
|
23
23
|
/**
|
|
24
24
|
* children
|
|
25
25
|
*/
|
|
26
|
-
children:
|
|
26
|
+
children: ReactNode | ReactNode[]
|
|
27
27
|
/**
|
|
28
28
|
* Indicates if merchant uses multiple currencies
|
|
29
29
|
*/
|
|
@@ -92,6 +92,12 @@ export default function NostoProvider(props: NostoProviderProps) {
|
|
|
92
92
|
|
|
93
93
|
const { clientScriptLoaded } = useLoadClientScript(props)
|
|
94
94
|
|
|
95
|
+
if (clientScriptLoaded) {
|
|
96
|
+
window.nostojs(api => {
|
|
97
|
+
api.defaultSession().setVariation(currentVariation!).setResponseMode(responseMode)
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
|
|
95
101
|
return (
|
|
96
102
|
<NostoContext.Provider
|
|
97
103
|
value={{
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { useRenderCampaigns, useNostoApi } from "../hooks"
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @group Components
|
|
5
|
+
*/
|
|
6
|
+
export type NostoSearchProps = {
|
|
7
|
+
query: string
|
|
8
|
+
placements?: string[]
|
|
9
|
+
}
|
|
10
|
+
|
|
3
11
|
/**
|
|
4
12
|
* You can personalise your search pages by using the NostoSearch component.
|
|
5
13
|
* The component requires that you provide it the current search term.
|
|
@@ -23,8 +31,17 @@ import { useRenderCampaigns, useNostoApi } from "../hooks"
|
|
|
23
31
|
*
|
|
24
32
|
* @group Components
|
|
25
33
|
*/
|
|
26
|
-
export default function NostoSearch(props:
|
|
27
|
-
|
|
34
|
+
export default function NostoSearch(props: NostoSearchProps) {
|
|
35
|
+
useNostoSearch(props)
|
|
36
|
+
return null
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* You can personalise your search pages by using the useNostoSearch hook.
|
|
41
|
+
*
|
|
42
|
+
* @group Hooks
|
|
43
|
+
*/
|
|
44
|
+
export function useNostoSearch({ query, placements }: NostoSearchProps) {
|
|
28
45
|
const { renderCampaigns } = useRenderCampaigns()
|
|
29
46
|
|
|
30
47
|
useNostoApi(
|
|
@@ -37,5 +54,4 @@ export default function NostoSearch(props: { query: string; placements?: string[
|
|
|
37
54
|
},
|
|
38
55
|
[query]
|
|
39
56
|
)
|
|
40
|
-
return null
|
|
41
57
|
}
|
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import { useNostoContext, useDeepCompareEffect } from "../hooks"
|
|
2
|
-
import { Cart, Customer } from "../types"
|
|
2
|
+
import { Cart as CartSnakeCase, Customer as CustomerSnakeCase } from "../types"
|
|
3
3
|
import { snakeize } from "../utils/snakeize"
|
|
4
|
+
import { ToCamelCase } from "../utils/types"
|
|
5
|
+
|
|
6
|
+
type Cart = CartSnakeCase | ToCamelCase<CartSnakeCase>
|
|
7
|
+
type Customer = CustomerSnakeCase | ToCamelCase<CustomerSnakeCase>
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @group Components
|
|
11
|
+
*/
|
|
12
|
+
export type NostoSessionProps = {
|
|
13
|
+
cart?: Cart
|
|
14
|
+
customer?: Customer
|
|
15
|
+
}
|
|
4
16
|
|
|
5
17
|
/**
|
|
6
18
|
* Nosto React requires that you pass it the details of current cart contents and the details of the currently logged-in customer, if any, on every route change.
|
|
@@ -10,10 +22,19 @@ import { snakeize } from "../utils/snakeize"
|
|
|
10
22
|
*
|
|
11
23
|
* The cart prop requires a value that adheres to the type `Cart`, while the customer prop requires a value that adheres to the type `Customer`.
|
|
12
24
|
*
|
|
13
|
-
* @group
|
|
25
|
+
* @group Components
|
|
26
|
+
*/
|
|
27
|
+
export default function NostoSession(props?: NostoSessionProps) {
|
|
28
|
+
useNostoSession(props)
|
|
29
|
+
return null
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Nosto React requires that you pass it the details of current cart contents and the details of the currently logged-in customer, if any, on every route change.
|
|
34
|
+
*
|
|
35
|
+
* @group Hooks
|
|
14
36
|
*/
|
|
15
|
-
export
|
|
16
|
-
const { cart, customer } = props ?? {}
|
|
37
|
+
export function useNostoSession({ cart, customer }: NostoSessionProps = {}) {
|
|
17
38
|
const { clientScriptLoaded } = useNostoContext()
|
|
18
39
|
|
|
19
40
|
useDeepCompareEffect(() => {
|
|
@@ -24,14 +45,12 @@ export default function NostoSession(props?: { cart?: Cart; customer?: Customer
|
|
|
24
45
|
window.nostojs(api => {
|
|
25
46
|
api
|
|
26
47
|
.defaultSession()
|
|
27
|
-
.setResponseMode("HTML")
|
|
28
48
|
.setCart(currentCart)
|
|
29
49
|
.setCustomer(currentCustomer)
|
|
30
50
|
.viewOther()
|
|
31
|
-
.load()
|
|
51
|
+
.load({ skipPageViews: true })
|
|
32
52
|
})
|
|
33
53
|
}
|
|
34
54
|
}, [clientScriptLoaded, cart, customer])
|
|
35
55
|
|
|
36
|
-
return <></>
|
|
37
56
|
}
|
package/src/components/index.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export { default as Nosto404 } from "./Nosto404"
|
|
2
|
-
export { default as NostoOther } from "./NostoOther"
|
|
3
|
-
export { default as NostoCheckout } from "./NostoCheckout"
|
|
4
|
-
export { default as NostoProduct } from "./NostoProduct"
|
|
5
|
-
export { default as NostoCategory } from "./NostoCategory"
|
|
6
|
-
export { default as NostoSearch } from "./NostoSearch"
|
|
7
|
-
export { default as NostoOrder } from "./NostoOrder"
|
|
8
|
-
export { default as NostoHome } from "./NostoHome"
|
|
9
|
-
export { default as NostoPlacement } from "./NostoPlacement"
|
|
1
|
+
export { default as Nosto404, useNosto404, type Nosto404Props } from "./Nosto404"
|
|
2
|
+
export { default as NostoOther, useNostoOther, type NostoOtherProps } from "./NostoOther"
|
|
3
|
+
export { default as NostoCheckout, useNostoCheckout, type NostoCheckoutProps } from "./NostoCheckout"
|
|
4
|
+
export { default as NostoProduct, useNostoProduct, type NostoProductProps } from "./NostoProduct"
|
|
5
|
+
export { default as NostoCategory, useNostoCategory, type NostoCategoryProps } from "./NostoCategory"
|
|
6
|
+
export { default as NostoSearch, useNostoSearch, type NostoSearchProps } from "./NostoSearch"
|
|
7
|
+
export { default as NostoOrder, useNostoOrder, type NostoOrderProps } from "./NostoOrder"
|
|
8
|
+
export { default as NostoHome, useNostoHome, type NostoHomeProps } from "./NostoHome"
|
|
9
|
+
export { default as NostoPlacement, type NostoPlacementProps } from "./NostoPlacement"
|
|
10
10
|
export { default as NostoProvider, type NostoProviderProps } from "./NostoProvider"
|
|
11
|
-
export { default as NostoSession } from "./NostoSession"
|
|
11
|
+
export { default as NostoSession, useNostoSession, type NostoSessionProps } from "./NostoSession"
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { useEffect, useRef, useMemo } from "react"
|
|
1
|
+
import { useEffect, useRef, useMemo, type EffectCallback, type DependencyList } from "react"
|
|
2
2
|
import { deepCompare } from "../utils/compare"
|
|
3
3
|
|
|
4
4
|
export function useDeepCompareEffect(
|
|
5
|
-
callback:
|
|
6
|
-
dependencies:
|
|
7
|
-
)
|
|
5
|
+
callback: EffectCallback,
|
|
6
|
+
dependencies: DependencyList
|
|
7
|
+
) {
|
|
8
8
|
return useEffect(callback, useDeepCompareMemoize(dependencies))
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
function useDeepCompareMemoize<T>(value: T) {
|
|
12
|
-
const ref = useRef<T>(value)
|
|
13
|
-
const signalRef = useRef
|
|
12
|
+
const ref = useRef<T>(value)
|
|
13
|
+
const signalRef = useRef(0)
|
|
14
14
|
|
|
15
15
|
if (!deepCompare(value, ref.current)) {
|
|
16
16
|
ref.current = value
|
package/src/hooks/useNostoApi.ts
CHANGED
|
@@ -8,15 +8,12 @@ export function useNostoApi(
|
|
|
8
8
|
deps?: DependencyList,
|
|
9
9
|
flags?: { deep?: boolean }
|
|
10
10
|
): void {
|
|
11
|
-
const { clientScriptLoaded
|
|
11
|
+
const { clientScriptLoaded } = useNostoContext()
|
|
12
12
|
const useEffectFn = flags?.deep ? useDeepCompareEffect : useEffect
|
|
13
13
|
|
|
14
14
|
useEffectFn(() => {
|
|
15
15
|
if (clientScriptLoaded) {
|
|
16
|
-
window.nostojs(
|
|
17
|
-
api.defaultSession().setVariation(currentVariation!).setResponseMode(responseMode)
|
|
18
|
-
cb(api)
|
|
19
|
-
})
|
|
16
|
+
window.nostojs(cb)
|
|
20
17
|
}
|
|
21
|
-
}, [clientScriptLoaded,
|
|
18
|
+
}, [clientScriptLoaded, ...(deps ?? [])])
|
|
22
19
|
}
|
|
@@ -4,6 +4,8 @@ import { ActionResponse, Recommendation } from "../types"
|
|
|
4
4
|
import { useNostoContext } from "./useNostoContext"
|
|
5
5
|
import { RecommendationComponent } from "../context"
|
|
6
6
|
|
|
7
|
+
type CampaignData = Pick<ActionResponse, "campaigns" | "recommendations">
|
|
8
|
+
|
|
7
9
|
// RecommendationComponent for client-side rendering:
|
|
8
10
|
function RecommendationComponentWrapper(props: {
|
|
9
11
|
recommendationComponent: RecommendationComponent,
|
|
@@ -15,7 +17,7 @@ function RecommendationComponentWrapper(props: {
|
|
|
15
17
|
})
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
function injectCampaigns(data:
|
|
20
|
+
function injectCampaigns(data: CampaignData) {
|
|
19
21
|
if (!window.nostojs) {
|
|
20
22
|
throw new Error("Nosto has not yet been initialized")
|
|
21
23
|
}
|
|
@@ -32,7 +34,7 @@ export function useRenderCampaigns() {
|
|
|
32
34
|
return { renderCampaigns: injectCampaigns }
|
|
33
35
|
}
|
|
34
36
|
|
|
35
|
-
function renderCampaigns(data:
|
|
37
|
+
function renderCampaigns(data: CampaignData) {
|
|
36
38
|
// render recommendation component into placements:
|
|
37
39
|
const recommendations = data.campaigns?.recommendations ?? {}
|
|
38
40
|
for (const key in recommendations) {
|