@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
67 lines • 2.56 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useManifestWithSessionId = void 0;
const react_1 = __importDefault(require("react"));
const appendQueryParamsToManifestURL_1 = require("../wallet-api/utils/appendQueryParamsToManifestURL");
const live_env_1 = require("@ledgerhq/live-env");
function useManifestWithSessionId({ manifest, shareAnalytics }) {
const [id, setId] = react_1.default.useState(null);
const [loading, setLoading] = react_1.default.useState(false);
react_1.default.useEffect(() => {
let cancelled = false;
async function fetchId() {
if (!shareAnalytics || !manifest) {
setId(null);
setLoading(false);
return;
}
setLoading(true);
try {
const response = await fetch((0, live_env_1.getEnv)("PROVIDER_SESSION_ID_ENDPOINT"), {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
});
if (!response.ok)
throw new Error("Failed to fetch session ID");
const { providerSessionId } = await response.json();
if (!cancelled) {
setId(providerSessionId);
setLoading(false);
}
}
catch {
if (!cancelled) {
setId(null);
setLoading(false);
}
}
}
fetchId();
return () => {
cancelled = true; // avoid setting state after unmount
};
}, [shareAnalytics]);
const customizedManifest = react_1.default.useMemo(() => {
if (!manifest)
return null;
if (loading) {
return null;
}
if (id) {
const url = (0, appendQueryParamsToManifestURL_1.appendQueryParamsToManifestURL)(manifest, {
externalID: id,
});
return url ? { ...manifest, url: url.toString() } : manifest;
}
return manifest;
}, [manifest, id, loading]);
return { manifest: customizedManifest, loading };
}
exports.useManifestWithSessionId = useManifestWithSessionId;
//# sourceMappingURL=useManifestWithSessionId.js.map