@shopify/shop-minis-react
Version:
React component library for Shopify Shop Minis with Tailwind CSS v4 support (source-only, requires TypeScript)
35 lines (34 loc) • 1.05 kB
JavaScript
import { useRef as a, useCallback as c } from "react";
import { useHandleAction as f } from "../../internal/useHandleAction.js";
import { useShopActions as p } from "../../internal/useShopActions.js";
function g() {
const { generateUserToken: s } = p(), n = f(s), r = a(null), t = a(null), o = c(
(e) => {
if (!e?.data?.token || !e.data.expiresAt)
return !1;
try {
const u = new Date(e.data.expiresAt).getTime(), i = Date.now(), l = 5 * 60 * 1e3;
return u - l > i;
} catch {
return !1;
}
},
[]
);
return {
generateUserToken: c(async () => r.current && o(r.current) ? r.current : (t.current || (t.current = (async () => {
try {
const e = await n();
return e.data?.token && e.data?.expiresAt && (r.current = e), e;
} catch (e) {
throw r.current = null, e;
} finally {
t.current = null;
}
})()), t.current), [n, o])
};
}
export {
g as useGenerateUserToken
};
//# sourceMappingURL=useGenerateUserToken.js.map