@adyen/adyen-platform-experience-web
Version:

46 lines (45 loc) • 1.59 kB
JavaScript
import { useState as h, useRef as B, useCallback as w, useMemo as R, useEffect as l } from "../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { useFetch as j } from "./useFetch.js";
import { createAbortable as k } from "../primitives/async/abortable/main.js";
import { useConfigContext as v } from "../core/ConfigContext/preact/context.js";
import { isFunction as G } from "../utils/value/is.js";
const W = ({ balanceAccount: T }) => {
const [m, u] = h(!1), [o, p] = h(() => performance.now()), [C, q] = h(), d = B(k()).current, t = T?.id, x = !!t, { getBalances: r } = v().endpoints, a = G(r), n = a && x, i = n && C !== o, E = w(async () => {
if (i) {
const { signal: f } = d.refresh(!0);
try {
const I = await r({ signal: f }, { path: { balanceAccountId: t } });
if (!f.aborted)
return I?.data;
} catch (y) {
if (!f.aborted)
throw y;
}
}
}, [d, t, r, i]), { data: s, error: b, isFetching: e } = j({
fetchOptions: { enabled: i },
queryFn: E
}), F = B(e), c = !e && n, g = R(() => Array.isArray(s) ? s : [], [s]), A = w(() => {
c && u(!0);
}, [c]);
return l(() => {
t && p(performance.now());
}, [t]), l(() => {
m && (u(!1), p(performance.now()));
}, [m]), l(() => {
F.current && !e && q(o), F.current = e;
}, [e, o]), R(
() => ({
balances: g,
error: b,
canRefresh: c,
refresh: A,
isAvailable: a,
isWaiting: e || a && !n && !s
}),
[g, n, a, c, s, b, e, A]
);
};
export {
W as default
};