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

49 lines (48 loc) • 2.44 kB
JavaScript
import { useRef as l, useCallback as h, useState as G, useMemo as a, useEffect as T } from "../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import j from "../../../../hooks/useBooleanState.js";
import F from "../../../../hooks/useMounted.js";
import { getClampedPageLimit as H } from "../utils.js";
import { noop as J } from "../../../../utils/common.js";
import { clamp as K } from "../../../../utils/value/number.js";
const Z = (V, x, z) => {
const u = l(), c = l(), R = l(), e = l(), n = F(
h(() => {
u.current?.abort(), u.current = void 0;
}, [])
), [t, v] = G(e.current), [f, m] = j(!1), r = a(() => H(z), [z]), { getPageCount: s, getPageParams: M, resetPageCount: S, updatePagination: $ } = V, i = a(() => x ? (d) => {
if (!(r && Number.isInteger(d))) return;
const p = s(), o = d < 0 ? d + p + 1 : d;
o > 0 && (p ? o <= p : o === 1) && (u.current?.abort(), u.current = new AbortController(), n.current && (((e.current = o) > 1 || p) && v(e.current), (async () => {
const { signal: b } = u.current, B = { ...M(o, r), limit: r, page: o };
try {
const P = await x(B, b);
if (!P || !n.current) return;
const { size: y, ...D } = P;
$(o, r, D), c.current = e.current && Math.max(e.current, c.current || -1 / 0), e.current && e.current === c.current && (R.current = y), e.current === 1 && y > 0 && v(e.current), e.current = void 0, m(!0);
} catch (P) {
if (b.aborted) return;
console.error(P);
}
})()));
} : J, [r, x, n, s, M, $, m]), A = h(() => {
t && i(Math.min(t + 1, s()));
}, [i, t, s]), q = h(() => {
t && i(Math.max(t - 1, 1));
}, [i, t]), g = a(
() => s() || t || void 0,
// eslint-disable-next-line react-hooks/exhaustive-deps
[s, t, f]
), w = a(() => !!(t && g) && t < g, [t, g]), E = a(() => !!t && t > 1, [t]), C = a(
() => c.current ? (c.current - 1) * r + (R.current || 0) : 0,
// eslint-disable-next-line react-hooks/exhaustive-deps
[r, f]
), I = a(() => t ? K(0, C - (t - 1) * r, r) : 0, [r, C, t]), N = h(() => {
S(), c.current = R.current = e.current = void 0, n.current && v(e.current);
}, [S, n]);
return T(() => {
n.current && f && m(!1);
}, [f, n, m]), { goto: i, hasNext: w, hasPrev: E, limit: r, next: A, page: t, pages: g, pageSize: I, prev: q, resetPagination: N, size: C };
};
export {
Z as default
};