UNPKG

urbi-exhibitions

Version:
2 lines (1 loc) 4.48 kB
"use strict";const r=require("react"),L=require("./index--ts_8_Sq.cjs"),$=6048e5,x=864e5,k=Symbol.for("constructDateFrom");function A(e,t){return typeof e=="function"?e(t):e&&typeof e=="object"&&k in e?e[k](t):e instanceof Date?new e.constructor(t):new Date(t)}function C(e,t){return A(t||e,e)}function F(e,t){return+C(e)-+C(t)}function h(e){return+C(e)}const J=({timeout:e,autoStart:t,onTimeout:c})=>{const[s,l]=r.useState(null),[o,u]=r.useState(null),[d,i]=r.useState(!1),m=r.useRef(void 0),f=r.useMemo(()=>o===null||s===null?0:e-F(o,s),[o,s,e]),E=r.useMemo(()=>f?Math.ceil(f/1e3):0,[f]),n=r.useMemo(()=>o===null||s===null?0:F(o,s),[o,s]),a=r.useMemo(()=>n?Math.ceil(n/1e3):0,[n]),w=r.useMemo(()=>s!==null&&o!==null,[s,o]),S=r.useCallback(()=>{clearInterval(m.current)},[]),g=r.useCallback(()=>{S();const T=()=>{u(h(new Date))};T(),m.current=setInterval(T,1e3)},[S]),v=r.useCallback(()=>{s===null&&(l(h(new Date)),g())},[s,g]),b=r.useCallback(()=>{S()},[S]),I=r.useCallback(()=>{l(h(new Date)),u(null),g()},[g]);return r.useEffect(()=>{w&&f<=0&&(c==null||c(),b())},[w,f,c,b]),r.useEffect(()=>{t&&!d&&(v(),i(!0))},[t,d,v]),r.useMemo(()=>({start:v,stop:b,restart:I,expiresMilliseconds:f,expiresSeconds:E,passedMilliseconds:n,passedSeconds:a,isActive:w}),[v,b,I,f,E,n,a,w])},z=!0,O=!1,M=z,U=({timeout:e,enabled:t,onActivity:c,onInactivity:s})=>{const[l,o]=r.useState(M),u=r.useRef(void 0),d=r.useCallback(()=>{o(O),s==null||s()},[s]),i=r.useCallback(()=>{o(M),clearTimeout(u.current)},[]),m=r.useCallback(()=>{u.current=setTimeout(d,e)},[d,e]),f=r.useCallback(()=>{i(),m()},[i,m]),E=r.useCallback(()=>{o(z),f(),c==null||c()},[f,c]);return r.useEffect(()=>{if(!t)return i();const n=()=>{E()};return document.addEventListener("mousedown",n),document.addEventListener("touchstart",n),document.addEventListener("click",n),m(),()=>{document.removeEventListener("mousedown",n),document.removeEventListener("touchstart",n),document.removeEventListener("click",n),i()}},[t,E,m,i]),r.useMemo(()=>({isActive:l}),[l])};var _=typeof window<"u"?r.useLayoutEffect:r.useEffect;function p(e,t,c,s){const l=r.useRef(t);_(()=>{l.current=t},[t]),r.useEffect(()=>{const o=window;if(!(o&&o.addEventListener))return;const u=d=>{l.current(d)};return o.addEventListener(e,u,s),()=>{o.removeEventListener(e,u,s)}},[e,c,s])}function R(e){const t=r.useRef(()=>{throw new Error("Cannot call an event handler while rendering.")});return _(()=>{t.current=e},[e]),r.useCallback((...c)=>{var s;return(s=t.current)==null?void 0:s.call(t,...c)},[t])}var D=typeof window>"u";function W(e,t,c={}){const{initializeWithValue:s=!0}=c,l=r.useCallback(n=>c.serializer?c.serializer(n):JSON.stringify(n),[c]),o=r.useCallback(n=>{if(c.deserializer)return c.deserializer(n);if(n==="undefined")return;const a=t instanceof Function?t():t;let w;try{w=JSON.parse(n)}catch(S){return console.error("Error parsing JSON:",S),a}return w},[c,t]),u=r.useCallback(()=>{const n=t instanceof Function?t():t;if(D)return n;try{const a=window.localStorage.getItem(e);return a?o(a):n}catch(a){return console.warn(`Error reading localStorage key “${e}”:`,a),n}},[t,e,o]),[d,i]=r.useState(()=>s?u():t instanceof Function?t():t),m=R(n=>{D&&console.warn(`Tried setting localStorage key “${e}” even though environment is not a client`);try{const a=n instanceof Function?n(u()):n;window.localStorage.setItem(e,l(a)),i(a),window.dispatchEvent(new StorageEvent("local-storage",{key:e}))}catch(a){console.warn(`Error setting localStorage key “${e}”:`,a)}}),f=R(()=>{D&&console.warn(`Tried removing localStorage key “${e}” even though environment is not a client`);const n=t instanceof Function?t():t;window.localStorage.removeItem(e),i(n),window.dispatchEvent(new StorageEvent("local-storage",{key:e}))});r.useEffect(()=>{i(u())},[e]);const E=r.useCallback(n=>{n.key&&n.key!==e||i(u())},[e,u]);return p("storage",E),p("local-storage",E),[d,m,f]}function q(e){const t=r.useRef(e);t.current=e,r.useEffect(()=>()=>{t.current()},[])}function N(e,t=500,c){const s=r.useRef();q(()=>{s.current&&s.current.cancel()});const l=r.useMemo(()=>{const o=L.debounce(e,t,c),u=(...d)=>o(...d);return u.cancel=()=>{o.cancel()},u.isPending=()=>!!s.current,u.flush=()=>o.flush(),u},[e,t,c]);return r.useEffect(()=>{s.current=L.debounce(e,t,c)},[e,t,c]),l}exports.constructFrom=A;exports.millisecondsInDay=x;exports.millisecondsInWeek=$;exports.toDate=C;exports.useDebounceCallback=N;exports.useInactivityDetector=U;exports.useLocalStorage=W;exports.useTimer=J;