UNPKG

@mitosite-ai/mitosite-react

Version:

Personalize your website for every user!

3 lines (2 loc) 4.12 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("@mitosite-ai/mitosite-core")):"function"==typeof define&&define.amd?define(["exports","react","@mitosite-ai/mitosite-core"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).MitositeReact={},e.React,e.MitositeCore)}(this,function(e,t,n){"use strict";const r=t.createContext(void 0),o="https://gateway-production-7f1e.up.railway.app";e.MitoProvider=function({providerId:e,gatewayUrl:s=o,headers:c,requestDebounceMs:i=30,onError:u,debug:a,children:l}){if(t.useContext(r))throw new n.ProviderNestingError;const[d,f]=t.useState(null),[p,w]=t.useState(!0),[g,E]=t.useState(null),h=t.useRef(new Map),m=t.useRef(new Map),S=t.useRef(new Set),v=t.useRef(null),b=t.useRef(null),y=t.useRef(null),C=t.useRef(null),k=t.useCallback(t=>`${e}:${t||"unknown"}`,[e]),M=t.useCallback((e,t)=>{const n=m.current.get(e);if(n&&0!==n.size)for(const r of n)r(t,e)},[]),x=t.useCallback(()=>{b.current&&(window.clearTimeout(b.current),b.current=null),b.current=window.setTimeout(()=>{T()},Math.max(0,i))},[i]),R=t.useCallback(e=>h.current.get(e),[]),z=t.useCallback((e,t)=>{let n=m.current.get(e);return n||(n=new Set,m.current.set(e,n)),n.add(t),()=>{const n=m.current.get(e);n&&(n.delete(t),0===n.size&&m.current.delete(e))}},[]),P=t.useCallback(e=>{S.current.add(e),x()},[x]),I=t.useCallback(()=>{try{v.current?.close()}catch{}v.current=null},[]),T=t.useCallback(async()=>{if(!d)return;const t=[...S.current];if(S.current.clear(),0===t.length)return;if(!n.supportsSSE())return void(a&&console.warn("[Mitosite] SSE not supported; skipping personalize"));const r=n.collectNavigationContext(),i=k(r.pageUrl),l=n.makeCacheKey(i,d,t),f=n.computeEnvironmentHash(d),p=n.getCachedPersonalization(l);if(p&&p.data)for(const[e,t]of Object.entries(p.data))h.current.set(e,t),M(e,t);if(n.shouldSkipPersonalize(p,{policy:"skipIfComplete",subsetOkForSkip:!0,requestedIds:t}))return void(a&&console.log("[Mitosite] Skipping SSE personalize due to cache policy"));y.current===i&&C.current===f||I(),y.current=i,C.current=f;const w=`${(s||(a?"http://localhost:8093":o)).replace(/\/$/,"")}/personalize`,g={providerId:e,environmentData:d,navigationContext:r,contentIds:t},m=n.openSSE(w,g,"POST",c);v.current=m,m.onOpen(()=>{a&&console.log("[Mitosite] SSE connection opened")}),m.onMessage(e=>{if("update"===e.type&&e.content){const t=e.content;for(const[e,n]of Object.entries(t))h.current.set(e,n),M(e,n)}else if("completed"===e.type){const e={};for(const n of t){const t=h.current.get(n);"string"==typeof t&&(e[n]=t)}n.setCachedPersonalization(l,e,f,{completed:!0,contentIds:t}),a&&console.log("[Mitosite] SSE completed, cache written")}else if("error"===e.type){const e=new Error("SSE error");E(e),u&&u(e)}}),m.onError(e=>{const t=e instanceof Error?e:new Error("SSE connection error");E(t),u&&u(t)})},[d,c,s,e,M,k,I,a]),O=t.useCallback(()=>{for(const e of m.current.keys())S.current.add(e)},[]);t.useEffect(()=>{let e=!1;return(async()=>{try{w(!0);const t=await n.collectEnvironmentData();if(e)return;f(t)}catch(e){const t=e instanceof Error?e:new Error("Environment collection failed");E(t),u&&u(t)}finally{w(!1)}})(),()=>{e=!0}},[u]),t.useEffect(()=>{d&&(O(),T())},[d,O,T]),t.useEffect(()=>{const e=()=>{y.current=null,O(),T()};return window.addEventListener("popstate",e),window.addEventListener("hashchange",e),()=>{window.removeEventListener("popstate",e),window.removeEventListener("hashchange",e)}},[O,T]),t.useEffect(()=>()=>{I(),b.current&&(window.clearTimeout(b.current),b.current=null)},[I]);const q=t.useMemo(()=>({getValue:R,subscribe:z,register:P,refresh:async()=>{O(),await T()},environment:d,loading:p,error:g}),[R,z,P,O,T,d,p,g]);return t.createElement(r.Provider,{value:q},l)},e.useMitoContent=function({contentId:e,type:n,defaultValue:o}){const s=t.useContext(r);if(!s)throw new Error("useMitoContent must be used within a MitoProvider");const[c,i]=t.useState(()=>s.getValue(e)??o);return t.useEffect(()=>{s.register(e);const t=s.subscribe(e,e=>i(e??o));return()=>{t&&t()}},[e,o,n,s]),c}}); //# sourceMappingURL=index.umd.min.js.map