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

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