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