UNPKG

@virtuoso.dev/message-list

Version:

Virtualized React component for rendering human/AI chatbot conversations.

1,832 lines 64.3 kB
import { jsx as T, jsxs as dn } from "react/jsx-runtime"; import { Cell as u, Signal as E, map as g, filter as I, withLatestFrom as S, scan as te, debounceTime as ve, mapTo as W, throttleTime as Zt, delayWithMicrotask as qe, onNext as mn, Action as Jt, useCellValue as V, useRealm as Te, Realm as Eo, RealmContext as yo, useCellValues as $o, usePublisher as wo } from "@virtuoso.dev/gurx"; import f, { useEffect as bn, useLayoutEffect as Lo } from "react"; function Ee(e, t) { const n = u(e, (o) => { o.link(t(o), n); }); return n; } const Le = { lvl: 0 }; function vn(e, t, n, o = Le, i = Le) { return { k: e, l: o, lvl: n, r: i, v: t }; } function x(e) { return e === Le; } function De() { return Le; } function Ft(e, t) { if (x(e)) return Le; const { k: n, l: o, r: i } = e; if (t === n) { if (x(o)) return i; if (x(i)) return o; const [s, l] = In(o); return at(C(e, { k: s, l: kn(o), v: l })); } return t < n ? at(C(e, { l: Ft(o, t) })) : at(C(e, { r: Ft(i, t) })); } function ce(e, t, n = "k") { if (x(e)) return [Number.NEGATIVE_INFINITY, void 0]; if (e[n] === t) return [e.k, e.v]; if (e[n] < t) { const o = ce(e.r, t, n); return o[0] === Number.NEGATIVE_INFINITY ? [e.k, e.v] : o; } return ce(e.l, t, n); } function P(e, t, n) { return x(e) ? vn(t, n, 1) : t === e.k ? C(e, { k: t, v: n }) : t < e.k ? an(C(e, { l: P(e.l, t, n) })) : an(C(e, { r: P(e.r, t, n) })); } function Wt(e, t, n) { if (x(e)) return []; const { k: o, v: i, l: s, r: l } = e; let r = []; return o > t && (r = r.concat(Wt(s, t, n))), o >= t && o <= n && r.push({ k: o, v: i }), o <= n && (r = r.concat(Wt(l, t, n))), r; } function Ro(e, t, n, o) { if (x(e)) return Le; let i = De(); for (const { k: s, v: l } of Ie(e)) s > t && s <= n ? i = P(i, ...o(s, l)) : i = P(i, s, l); return i; } function Co(e, t, n) { let o = De(), i = -1; for (const { start: s, end: l, value: r } of Mo(e)) s < t ? (o = P(o, s, r), i = r) : s > t + n ? o = P(o, s - n, r) : l >= t + n && i !== r && (o = P(o, t, r)); return o; } function Ie(e) { return x(e) ? [] : [...Ie(e.l), { k: e.k, v: e.v }, ...Ie(e.r)]; } function In(e) { return x(e.r) ? [e.k, e.v] : In(e.r); } function kn(e) { return x(e.r) ? e.l : at(C(e, { r: kn(e.r) })); } function C(e, t) { return vn(t.k ?? e.k, t.v ?? e.v, t.lvl ?? e.lvl, t.l ?? e.l, t.r ?? e.r); } function Bt(e) { return x(e) || e.lvl > e.r.lvl; } function an(e) { return Pt(En(e)); } function at(e) { const { l: t, r: n, lvl: o } = e; if (n.lvl >= o - 1 && t.lvl >= o - 1) return e; if (o > n.lvl + 1) { if (Bt(t)) return En(C(e, { lvl: o - 1 })); if (!x(t) && !x(t.r)) return C(t.r, { l: C(t, { r: t.r.l }), lvl: o, r: C(e, { l: t.r.r, lvl: o - 1 }) }); throw new Error("Unexpected empty nodes"); } if (Bt(e)) return Pt(C(e, { lvl: o - 1 })); if (!x(n) && !x(n.l)) { const i = n.l, s = Bt(i) ? n.lvl - 1 : n.lvl; return C(i, { l: C(e, { lvl: o - 1, r: i.l }), lvl: i.lvl + 1, r: Pt(C(n, { l: i.r, lvl: s })) }); } throw new Error("Unexpected empty nodes"); } function Mo(e) { return Tn(Ie(e)); } function Sn(e, t, n) { if (x(e)) return []; const o = ce(e, t)[0]; return Tn(Wt(e, o, n)); } function xn(e, t) { const n = e.length; if (n === 0) return []; let { index: o, value: i } = t(e[0]); const s = []; for (let l = 1; l < n; l++) { const { index: r, value: c } = t(e[l]); s.push({ end: r - 1, start: o, value: i }), o = r, i = c; } return s.push({ end: Number.POSITIVE_INFINITY, start: o, value: i }), s; } function Tn(e) { return xn(e, ({ k: t, v: n }) => ({ index: t, value: n })); } function Pt(e) { const { r: t, lvl: n } = e; return !x(t) && !x(t.r) && t.lvl === n && t.r.lvl === n ? C(t, { l: C(e, { r: t.l }), lvl: n + 1 }) : e; } function En(e) { const { l: t } = e; return !x(t) && t.lvl === e.lvl ? C(t, { r: C(e, { l: t.r }) }) : e; } function kt(e, t, n, o = 0) { let i = e.length - 1; for (; o <= i; ) { const s = Math.floor((o + i) / 2), l = e[s], r = n(l, t); if (r === 0) return s; if (r === -1) { if (i - o < 2) return s - 1; i = s - 1; } else { if (i === o) return s; o = s + 1; } } throw new Error(`Failed binary finding record in array - ${e.join(",")}, searched for ${t}`); } function yn(e, t, n) { return e[kt(e, t, n)]; } function Vo(e, t, n, o) { const i = kt(e, t, o), s = kt(e, n, o, i); return e.slice(i, s + 1); } function Xt({ index: e }, t) { return t === e ? 0 : t < e ? -1 : 1; } function Ao({ offset: e }, t) { return t === e ? 0 : t < e ? -1 : 1; } function Oo(e) { return { index: e.index, value: e }; } function Do(e, t, n, o = 0) { return o > 0 && (t = Math.max(t, yn(e, o, Xt).offset)), t = Math.max(0, t), xn(Vo(e, t, n, Ao), Oo); } const Re = [[], 0, 0, 0]; function No(e, [t, n]) { let o = 0, i = 0, s = 0, l = 0; if (n !== 0) { l = kt(e, n - 1, Xt), s = e[l].offset; const c = ce(t, n - 1); o = c[0], i = c[1], e.length && e[l].height === ce(t, n)[1] && (l -= 1), e = e.slice(0, l + 1); } else e = []; for (const { start: r, value: c } of Sn(t, n, Number.POSITIVE_INFINITY)) { const a = (r - o) * i + s; e.push({ height: c, index: r, offset: a }), o = r, s = a, i = c; } return [e, i, s, o]; } function Bo(e) { const { size: t, startIndex: n, endIndex: o } = e; return (i) => i.start === n && (i.end === o || i.end === Number.POSITIVE_INFINITY) && i.value === t; } function _o(e, t) { let n = x(e) ? 0 : Number.POSITIVE_INFINITY; for (const o of t) { const { size: i, startIndex: s, endIndex: l } = o; if (n = Math.min(n, s), x(e)) { e = P(e, 0, i); continue; } const r = Sn(e, s - 1, l + 1); if (r.some(Bo(o))) continue; let c = !1, a = !1; for (const { start: p, end: m, value: v } of r) c ? (l >= p || i === v) && (e = Ft(e, p)) : (a = v !== i, c = !0), m > l && l >= p && v !== i && (e = P(e, l + 1, v)); a && (e = P(e, s, i)); } return [e, n]; } const $t = [De(), 0]; function Ho(e, [t, n]) { if (n.length > 0 && x(e) && t.length === 2) { const o = t[0].size, i = t[1].size; return [ n.reduce((s, l) => P(P(s, l, o), l + 1, i), De()), 0 ]; } return _o(e, t); } const be = E(); u([]); u([]); u(0); u(null); u(Number.NaN); const Ce = u(!1), pe = u($t, (e) => { e.link( e.pipe( be, I((t) => t.length > 0), S(Z), g(([t, n]) => Ho(n, [t, []])) ), pe ); }), Z = u($t[0], (e) => { e.link( e.pipe( pe, g(([t]) => t) ), Z ); }), $n = u($t[1], (e) => { e.link( e.pipe( pe, g(([, t]) => t) ), $n ); }), Me = u(Re[1]), ke = u(Re[0]), ze = u(Re, (e) => { e.link( e.pipe( Z, S($n), te(([t], [n, o]) => No(t, [n, o]), Re) ), ze ), e.link( e.pipe( ze, g(([, t]) => t) ), Me ), e.link( e.pipe( ze, g(([t]) => t) ), ke ); }), wn = u(Re[2], (e) => { e.link( e.pipe( ze, g(([, , t]) => t) ), wn ); }), Ln = u(Re[3], (e) => { e.link( e.pipe( ze, g(([, , , t]) => t) ), Ln ); }), Ne = u(0, (e) => { e.link( e.pipe( e.combine(ye, Ln, wn, Me), g(([t, n, o, i]) => o + (t - n) * i) ), Ne ); }); function Rn(e, t) { if (t.length === 0) return [0, 0]; const { offset: n, index: o, height: i } = yn(t, e, Xt); return [i * (e - o) + n, i]; } function St(e, t) { return Rn(e, t)[0]; } function Cn(e, t) { return Math.abs(e - t) < 1.01; } function Mn() { return typeof navigator > "u" ? !1 : /Macintosh/i.test(navigator.userAgent) && navigator.maxTouchPoints && navigator.maxTouchPoints > 1 || /iP(ad|od|hone)/i.test(navigator.userAgent) && /WebKit/i.test(navigator.userAgent); } function Vn(e) { return !e; } function Fo(e) { return e === 1 ? 1 : 1 - 2 ** (-10 * e); } function An(e = 1) { return (t, n) => { const o = n.signalInstance(); return n.sub(t, (i) => { let s = e; function l() { s > 0 ? (s--, requestAnimationFrame(l)) : n.pub(o, i); } l(); }), o; }; } const On = "up", _t = "down", Wo = "none", Po = { atBottom: !1, notAtBottomBecause: "NOT_SHOWING_LAST_ITEM", state: { offsetBottom: 0, scrollTop: 0, viewportHeight: 0, viewportWidth: 0, scrollHeight: 0 } }, Yo = 0, zo = 4; function pn(e) { return (t, n) => { const o = n.signalInstance(); return n.sub(t, (i) => { e > 0 ? e-- : n.pub(o, i); }), o; }; } u(!1); const Dn = u(!0); E(); const me = u(!1), Ko = E((e) => { e.link(e.pipe(Dn, Zt(50)), Ko); }), Nn = u(zo), jo = u(Yo, (e) => { e.link( e.pipe( e.combine(w, jo), g(([t, n]) => t <= n) ), Dn ); }), Ge = u(!1, (e) => { e.link(e.pipe(w, pn(1), W(!0)), Ge), e.link(e.pipe(w, pn(1), W(!1), ve(100)), Ge); }), Yt = u(!1, (e) => { e.link(e.pipe(X, W(!0)), Yt), e.link(e.pipe(X, W(!1), ve(200)), Yt); }), Bn = u(!1), pt = u( null, (e) => { e.link( e.pipe( e.combine( _, w, j, dt, Nn, Et, wt, Z ), I(([, , , , , , , t]) => !x(t)), te((t, [n, o, i, s, l, r]) => { const a = o + i - n + r > -l, p = { viewportWidth: s, viewportHeight: i, scrollTop: o, scrollHeight: n, listMarginTop: r }; if (a) { let v, b; return o > t.state.scrollTop ? (v = "SCROLLED_DOWN", b = t.state.scrollTop - o) : (v = n === i ? "LIST_TOO_SHORT" : "SIZE_DECREASED", b = t.state.scrollTop - o || t.scrollTopDelta), { atBottom: !0, state: p, atBottomBecause: v, scrollTopDelta: b }; } let m; return i < t.state.viewportHeight ? m = "VIEWPORT_HEIGHT_DECREASING" : s < t.state.viewportWidth ? m = "VIEWPORT_WIDTH_DECREASING" : o < t.state.scrollTop ? m = "SCROLLING_UPWARDS" : p.scrollHeight > t.state.scrollHeight || p.listMarginTop < t.state.listMarginTop ? t.atBottom ? m = "SIZE_INCREASED" : m = t.notAtBottomBecause : t.atBottom ? m = "NOT_FULLY_SCROLLED_TO_LAST_ITEM_BOTTOM" : m = t.notAtBottomBecause, { atBottom: !1, notAtBottomBecause: m, state: p }; }, Po) ), pt ), e.link( e.pipe( pt, te( ({ prev: t }, n) => { const o = !!(t && n && t.atBottom && !n.atBottom && n.notAtBottomBecause === "SIZE_INCREASED"); return { prev: n, shouldScroll: o }; }, { prev: null, shouldScroll: !1 } ), g(({ shouldScroll: t }) => t) ), Bn ), e.sub( e.pipe( j, S(pt, sn, Ze), I(([, , t, n]) => !t && !n), te( (t, [n, o]) => { let i = 0; return t.viewportHeight > n && o && !o.atBottom && o.notAtBottomBecause === "VIEWPORT_HEIGHT_DECREASING" && (i = t.viewportHeight - n), { viewportHeight: n, delta: i }; }, { viewportHeight: 0, delta: 0 } ) ), (t) => { t.delta && e.pub(X, t.delta); } ); }, (e, t) => !e || e.atBottom !== (t == null ? void 0 : t.atBottom) ? !1 : !e.atBottom && !t.atBottom ? e.notAtBottomBecause === t.notAtBottomBecause : !0 ), _n = u(0, (e) => { e.link( e.pipe( e.combine(w, _, j), te( (t, [n, o, i]) => { if (!Cn(t.scrollHeight, o)) { const s = o - (n + i) < 1; return t.scrollTop !== n && s ? { scrollHeight: o, scrollTop: n, jump: t.scrollTop - n, changed: !0 } : { scrollHeight: o, scrollTop: n, jump: 0, changed: !0 }; } return { scrollTop: n, scrollHeight: o, jump: 0, changed: !1 }; }, { scrollHeight: 0, jump: 0, scrollTop: 0, changed: !1 } ), I((t) => t.changed), g((t) => t.jump) ), _n ); }), xt = u(_t, (e) => { e.link( e.pipe( w, te( (t, n) => e.getValue(Yt) ? { direction: t.direction, prevScrollTop: n } : { direction: n < t.prevScrollTop ? On : _t, prevScrollTop: n }, { direction: _t, prevScrollTop: 0 } ), g((t) => t.direction) ), xt ), e.link(e.pipe(w, ve(100), W(Wo)), xt); }), fn = u(0, (e) => { e.link(e.pipe(Ge, I(Vn), W(0)), fn), e.link( e.pipe( w, Zt(100), S(Ge), I(([, t]) => !!t), te(([, t], [n]) => [t, n], [0, 0]), g(([t, n]) => n - t) ), fn ); }); function Qt(e, t) { if (typeof e == "number") return { index: e, offset: 0, behavior: "auto", align: "start-no-overflow" }; const n = { index: Number.NaN, align: e.align ?? "start-no-overflow", behavior: e.behavior ?? "auto", offset: e.offset ?? 0 }; return e.index === "LAST" ? n.index = t : e.index < 0 ? n.index = t + e.index : n.index = e.index, n; } function Hn({ location: e, sizeTree: t, offsetTree: n, totalHeight: o, totalCount: i, viewportHeight: s, headerHeight: l, stickyHeaderHeight: r, stickyFooterHeight: c }) { const { align: a, behavior: p, offset: m, index: v } = Qt(e, i - 1); function b() { const L = ce(t, v)[1]; if (L === void 0) throw new Error(`Item at index ${v} not found`); return L; } s -= r + c; let h = St(v, n) + l - r; a === "end" ? h = h - s + b() : a === "center" && (h = h - s / 2 + b() / 2), m && (h += m); let d = 0; return a === "start" && (d = Math.max(0, Math.min(h - (o - s)))), h = Math.max(0, h), { top: h, behavior: p, align: a, forceBottomSpace: d }; } const Pe = u(null), Uo = u(!1), ft = u(!0), zt = E((e) => { e.link( e.pipe( zt, g(() => !0) ), ft ), e.link( e.pipe( zt, g(() => null) ), Pe ); }), Fn = E((e) => { e.link( e.pipe( Fn, S(ye, ke, ot), g(([t, n, o, i]) => { let { align: s, behavior: l, offset: r, index: c } = Qt(t, n - 1); const a = typeof t != "number" ? t.done : void 0, [p, m] = Rn(c, o); return p < -i.listOffset ? ((typeof t == "number" || t.align === void 0) && (s = "start-no-overflow"), { index: c, align: s, behavior: l, offset: r, done: a }) : p + m > -i.listOffset + i.visibleListHeight ? ((typeof t == "number" || t.align === void 0) && (s = "end"), { index: c, align: s, behavior: l, offset: r, done: a }) : null; }), I((t) => t !== null) ), // @ts-expect-error contra variance J ); }), J = E((e) => { const t = e.pipe( J, S( Z, ke, ye, j, nt, _e, tt, Ne ), g( ([n, o, i, s, l, r, c, a, p]) => { try { return Hn({ location: n, totalHeight: p, sizeTree: o, offsetTree: i, totalCount: s, viewportHeight: l, headerHeight: r, stickyHeaderHeight: c, stickyFooterHeight: a }); } catch { return null; } } ), I((n) => n !== null) ); e.link(J, Pe), e.link(t, xe), e.link( e.pipe( J, I((n) => typeof n != "number" && n.index === "LAST"), W(!0) ), me ), e.link(e.pipe(t, W(!1)), ft), e.link(e.pipe(t, W(!1)), Uo), e.link( e.pipe( Z, // wait for the list to render with the specified sizeTree, so that enough space is available to scroll by ve(0), S(ft, Pe), I(([, n, o]) => !n && o !== null), g(([, , n]) => n) ), J ), e.sub(e.pipe(Qe, ve(10)), () => { const n = e.getValue(Pe); n !== null && typeof n != "number" && n.done !== void 0 && n.done(), e.pubIn({ [Pe]: null, [ft]: !0 }); }), e.link( e.pipe( mt, // wait for the list to render with the specified scrollOffset, so that enough space is available to scroll by qe(), I((n) => n !== 0) ), X ), e.link( e.pipe( mt, mn(w), g(() => 0) ), mt ); }), Ke = u(null), Ve = u(null, (e) => { e.link( e.pipe( Ve, I((n) => n !== null) ), Ke ); const t = e.pipe( e.combine(Ve, Z), S(Ke), I(([[n, o], i]) => n !== null && !x(o) && i !== null), g(([[n]]) => n) ); e.link(e.pipe(t, qe()), J), e.link( e.pipe( t, mn(e.pipe(Xe, I(Vn))), W(null) // unset the location after the scroll completes ), Ke ); }); function qo(e, t) { return [ { data: t == null ? void 0 : t[e], prevData: (t == null ? void 0 : t[e - 1]) ?? null, nextData: (t == null ? void 0 : t[e + 1]) ?? null, height: 0, index: e, offset: 0, type: "flat" } ]; } const Go = [], Ye = { items: Go, listBottom: 0, listTop: 0, offsetTree: [], paddingBottom: 0, paddingTop: 0, totalCount: 0, totalHeight: 0, deviationDelta: 0, data: null }, ht = u(!1), Be = u(Ye, (e) => { e.link( e.pipe( e.combine( Zo, zn, Z, ke, ye, Ne, R, mt, Ve, Ke, et, _e, tt, z, ht, Ce, G ), I((t) => { const n = t.at(-1), o = t.at(-2), i = t.at(-3); return !o && !n && !i; }), te( (t, [ n, o, i, s, l, r, c, a, p, m, v, b, h, d ]) => { var st; if ((c == null ? void 0 : c.length) === 0) return Ye; if (x(i)) { let ee = 0; return p !== null && (ee = Qt(p, l - 1).index), { ...Ye, items: qo(ee, c), offsetTree: s, totalCount: l, data: c }; } let L = 0; m !== null && n === 0 && (L = Hn({ totalHeight: r, location: m, sizeTree: i, offsetTree: s, totalCount: l, viewportHeight: e.getValue(j), headerHeight: e.getValue(nt), stickyHeaderHeight: b, stickyFooterHeight: h }).top ?? 0); let U = 0; e.getValue(w) !== 0 && !e.getValue(Xe) && e.getValue(xt) === On && t.totalCount === l && t.items.length > 0 && (U = r - t.totalHeight, U !== 0 && (U += e.getValue(_n))); const q = e.getValue(Pn), K = Math.min( Math.max( n + L + a - d - v + U - q, 0 ), r - o ), fe = K + o + q * 2; if (t.offsetTree === s && t.totalCount === l && t.data === c && K >= t.listTop && fe <= t.listBottom) return t; const he = [], He = l - 1, F = 0, A = Do(s, K, fe, F); let k = 0, H = 0, Q = !1; for (const ee of A) { const { value: { offset: ge, height: ne } } = ee; let oe = ee.start; k = ge, ge < K && (oe += Math.floor((K - ge) / ne), k += (oe - ee.start) * ne), oe < F && (k += (F - oe) * ne, oe = F); const Vt = Math.min(ee.end, He); for (let ie = oe; ie <= Vt && !(k >= fe); ie++) { const At = { data: c == null ? void 0 : c[ie], prevData: (c == null ? void 0 : c[ie - 1]) ?? null, nextData: (c == null ? void 0 : c[ie + 1]) ?? null, height: ne, index: ie, offset: k, type: "flat" }; Q || (Q = !0, H = k), he.push(At), k += ne; } } const it = r - k, Mt = ((st = he[0]) == null ? void 0 : st.offset) || 0; return { items: he, listBottom: k, listTop: H, offsetTree: s, paddingBottom: it, paddingTop: Mt, totalCount: l, totalHeight: r, data: c, deviationDelta: U }; }, Ye ) ), Be ); }), Tt = Ee([], (e) => e.pipe( e.combine(Be, w), g(([t, n]) => { const o = t.items.slice(); for (; o.length > 0 && o[0].offset + o[0].height < n; ) o.shift(); return o.map((i) => i.data); }) )), G = u(!1), je = u(!1), gt = E((e) => { e.link( e.pipe( Be, g((t) => t.deviationDelta), I((t) => t !== 0) ), gt ), Mn() ? (e.sub(e.pipe(gt, S(z, w)), ([t, n]) => { e.pub(z, n - t); }), e.sub( e.pipe(e.combine(w, z, Ce, je)), ([t, n, o, i]) => { o || i || (n > 0 && t < n ? (e.pub(G, !0), e.pub(xe, { top: 0, behavior: "instant" }), setTimeout(() => { e.pubIn({ [G]: !1, [z]: 0 }); })) : n < 0 && t <= 0 && (e.pubIn({ [G]: !0, [z]: 0 }), setTimeout(() => { e.pub(xe, { top: 0, behavior: "instant" }), e.pub(G, !1); }))); } ), e.sub( e.pipe( e.combine(Ge, z, G, Ce, je), I( ([t, n, o, i, s]) => !t && n !== 0 && !o && !i && !s ), Zt(100) ), ([, t]) => { e.pub(G, !0), t < 0 ? requestAnimationFrame(() => { e.pub(X, -t), e.pub(z, 0), requestAnimationFrame(() => { e.pub(G, !1); }); }) : requestAnimationFrame(() => { e.pub(X, -t), e.pub(z, 0), requestAnimationFrame(() => { e.pub(G, !1); }); }); } )) : e.link(gt, X); }), Xe = E(), en = u(!1), Qe = E((e) => { e.link(e.pipe(Qe, W(!1)), Xe); }, !1), w = u(0), j = u(0), dt = u(0), _ = u(0), Zo = w, mt = u(0), _e = u(0), et = u(0), tt = u(0), tn = u(0), Se = u(null), Wn = Jt(), Pn = u(0), Jo = Fo, Xo = 50, nt = Ee(0, (e) => e.pipe( e.combine(_e, et), g(([t, n]) => t + n) )), Yn = Ee(0, (e) => e.pipe( e.combine(tt, tn), g(([t, n]) => t + n) )), Qo = Ee(0, (e) => e.pipe( e.combine(_e, et, w), g(([t, n, o]) => t + Math.max(n - o, 0)) )), ei = Ee(0, (e) => e.pipe( e.combine(tt, tn, w, j, _), g(([t, n, o, i, s]) => { o = Math.min(o, s - i); const l = Math.max(n - (s - (o + i)), 0); return t + l; }) )), zn = Ee(0, (e) => e.pipe( e.combine(j, Qo, ei), g(([t, n, o]) => t - n - o) )), wt = u(0), Kn = u(0, (e) => { e.link( e.pipe( e.combine(Kn, Ne, j, nt, _e), g(([t, n, o, i, s]) => t === 0 ? 0 : Math.max(0, Math.min(t - (n + i + s - o)))) ), wt ); }), xe = E((e) => { e.link( e.pipe( xe, g((t) => t.align === "start" ? t.top ?? 0 : 0) ), Kn ), e.link( e.pipe( xe, S(w), I(([t, n]) => t.top !== n), W(!0) ), Xe ); }), nn = E((e) => { e.link( e.pipe( xe, S(Je), g(([t, n]) => ("top" in t && typeof t.top < "u" && (t = { ...t, top: t.top + n }), t)) ), nn ); }), ot = Ee( { listOffset: 0, visibleListHeight: 0, scrollHeight: 0, bottomOffset: 0, isAtBottom: !1 }, (e) => e.pipe( e.combine( w, nt, Yn, et, zn, _, wt, Ce, Ke, G, me ), I(([, , , , , , , t, n, o]) => !t && n === null && !o), g( ([ t, n, o, i, s, l, r, c, a, p, m ]) => { const v = e.getValue(Nn), b = l - n - o, h = -t + i, d = b + Math.min(0, h) - s - r; return { scrollHeight: b, listOffset: h, visibleListHeight: s, bottomOffset: d, isAtBottom: m || d <= v }; } ) ) ), Kt = E((e) => { e.link( e.pipe( w, ve(0), S(ot), I(([, t]) => t.scrollHeight > 0), g(([, t]) => t) ), Kt ); }), X = E(), z = u(0), bt = u(0), Et = u(0), jn = u(""), on = E(), Un = Jt(), qn = Jt(), sn = u(!1), Ze = u(null); u(0); const Je = u(0, (e) => { e.link( e.pipe( e.combine(Ae, ae, Je), g(([t, n, o]) => t - Math.max(0, o - n)) ), j ); }), ae = u(0, (e) => { e.link( e.pipe( e.combine(ae, Je), g(([t, n]) => Math.max(0, t - n)) ), w ); }), Ae = u(0); function ti(e, t) { var l, r; const n = t.slice(); let o = 0; const i = []; for (const { k: c, v: a } of Ie(e)) { for (; n.length && n[0] < c; ) n.shift(), o++; const p = Math.max(0, c - o), m = ((l = i.at(-1)) == null ? void 0 : l.k) ?? -1; p === m ? (((r = i.at(-2)) == null ? void 0 : r.v) ?? -1) === a ? i.pop() : i[i.length - 1].v = a : i.push({ k: p, v: a }); } let s = De(); for (const { k: c, v: a } of i) s = P(s, c, a); return s; } const ye = u(0), Oe = u(null), R = u(null, (e) => { e.link( e.pipe( R, I((t) => t !== null), g((t) => t.length) ), ye ); }), We = u(null), re = E((e) => { e.link( e.pipe( re, S(Me), g(([n, o]) => -(o * n.length)) ), z ), e.link(e.pipe(re, W(!0)), je), e.link(e.pipe(re, qe()), We); function t(n, o) { e.pubIn({ [ht]: !0, [X]: n, [bt]: n }), o ? requestAnimationFrame(() => { e.pubIn({ [z]: 0, [bt]: 0, [We]: null, [je]: !1, [ht]: !1 }); }) : e.pubIn({ [z]: 0, [bt]: 0, [We]: null, [je]: !1, [ht]: !1 }); } e.sub( e.pipe( ke, S(We), I(([, n]) => n !== null), g(([n, o]) => { if (o === null) throw new Error("Unexpected null items"); return St(o.length, n); }) ), (n) => { t(n, !1); } ), e.sub( e.pipe( re, An(2), S(ke, We), I(([, , n]) => n !== null), g(([n, o]) => St(n.length, o)) ), (n) => { t(n, !0); } ), e.changeWith(R, re, (n, o) => n ? [...o, ...n] : o.slice()), e.link( e.pipe( re, S(Z, Me), g(([n, o, i]) => { const s = n.length, l = i; return Ie(o).reduce( (c, { k: a, v: p }) => ({ ranges: [...c.ranges, { startIndex: c.prevIndex, endIndex: a + s - 1, size: c.prevSize }], prevIndex: a + s, prevSize: p }), { ranges: [], prevIndex: 0, prevSize: l } ).ranges; }) ), be ); }), Lt = E((e) => { const t = e.pipe( Lt, S(ot, en, Oe, Z), I(([, , , , o]) => !x(o)), g(([{ data: o, scrollToBottom: i }, s, l, r]) => { if (i === !1 || i === void 0) return null; let c = "auto"; const a = s.isAtBottom; if (typeof i == "function") { const p = i({ data: o, scrollLocation: s, scrollInProgress: l, context: r, atBottom: a }); if (!p) return null; if (typeof p == "object") return p; if (typeof p == "number") return { index: p, align: "end", behavior: "auto" }; c = p; } else { if (!a) return null; c = i; } return c === !0 && (c = "auto"), { index: "LAST", align: "end", behavior: c }; }) ); e.link( e.pipe( t, I((o) => o !== null), g(() => !0) ), me ), e.link( e.pipe( Qe, S(me), I(([o, i]) => i), g(() => !1) ), me ); const n = e.pipe( on, S(me), I(([o, i]) => o === "up" && i) ); e.link( e.pipe( n, g(() => !1) ), me ), e.link(e.pipe(n, W(!0)), zt), e.link( e.pipe( t, I((o) => o !== null), ve(20) ), J ); }), yt = E((e) => { e.changeWith(R, yt, (t, n) => t ? [...t, ...n.data] : n.data.slice()), e.link(yt, Lt); }), vt = E((e) => { e.changeWith(R, vt, (t, n) => t ? [...t.slice(0, n.offset), ...n.data, ...t.slice(n.offset)] : n.data.slice()), e.changeWith(pe, vt, ([t], n) => { const i = ce(t, n.offset, "k")[0], s = n.data.length; return [Ro(t, i, Number.POSITIVE_INFINITY, (r, c) => [r + s, c]), i]; }), e.link(vt, Lt); }), jt = E((e) => { e.changeWith(R, jt, (t, { offset: n, count: o }) => t ? t.slice(0, n).concat(t.slice(n + o)) : []), e.changeWith(pe, jt, ([t], { offset: n, count: o }) => [Co(t, n, o), n]); }), Ht = u(null), Ue = E((e) => { e.sub( e.pipe( Ue, S(R), I(([{ purgeItemSizes: t }, n]) => !!t || n === null || n.length === 0) ), ([t, n]) => { n === null || n.length === 0 ? e.pubIn({ ...t.initialLocation ? { [Ve]: t.initialLocation } : {}, [R]: t.data.slice() }) : e.pubIn({ ...t.initialLocation ? { [Ve]: t.initialLocation } : {}, [pe]: $t, [Be]: Ye, [Ht]: t.data.slice() }); } ), e.sub( e.pipe( qn, S(Ht), I(([, t]) => t !== null) ), ([, t]) => { e.pubIn({ [R]: t, [Ht]: null }); } ), e.link( e.pipe( Ue, I(({ purgeItemSizes: t }) => !t), S(Me), I(([, t]) => t > 0), g(([{ data: t }, n]) => [ { size: n, startIndex: t.length, endIndex: Number.POSITIVE_INFINITY } ]) ), be ), e.sub( e.pipe( Ue, I(({ purgeItemSizes: t }) => !t) ), ({ data: t, initialLocation: n, suppressItemMeasure: o }) => { requestAnimationFrame(() => { o || e.pub(Un), requestAnimationFrame(() => { n && e.pubIn({ [J]: n }); }); }), e.pubIn({ [R]: t.slice() }); } ); }), It = E((e) => { e.link( e.pipe( It, S(ke), g(([t, n]) => -St(t, n)) ), X ), e.changeWith(R, e.pipe(It, qe()), (t, n) => t ? t.slice(n) : []), e.changeWith(pe, e.pipe(It, qe()), ([t], n) => [Ie(t).reduce((i, { k: s, v: l }) => P(i, Math.max(0, s - n), l), De()), 0]); }), hn = E((e) => { e.changeWith(R, hn, (t, n) => t ? t.slice(0, t.length - n) : []), e.link( e.pipe( hn, S(ye, Me), g(([, t, n]) => [ { size: n, startIndex: t, endIndex: Number.POSITIVE_INFINITY } ]) ), be ); }), Gn = E((e) => { const t = e.pipe( Gn, S(R), g(([n, o]) => { if (!o) return []; const i = []; return o.forEach((s, l) => { n(s, l) && i.push(l); }), i; }) ); e.changeWith(R, t, (n, o) => n ? n.filter((i, s) => !o.includes(s)) : []), e.changeWith(pe, t, ([n], o) => [ti(n, o), 0]); }), Ut = E((e) => { e.changeWith(R, Ut, (t, { mapper: n }) => t ? t.map(n) : []), e.link( e.pipe( Ut, An(3), S(Bn), I(([{ autoscrollToBottomBehavior: t }, n]) => n && !!t), g(([{ autoscrollToBottomBehavior: t }]) => typeof t == "object" ? t.location() : { index: "LAST", align: "end", behavior: t }), I((t) => !!t) ), J ); }), ni = ({ item: e, ItemContent: t, mount: n, unmount: o }) => { const i = V(Oe), s = f.useRef(null), l = f.useCallback( (r) => { r ? (s.current = r, n(r)) : s.current && (o(s.current), s.current = null); }, [n, o] ); return /* @__PURE__ */ T( "div", { ref: l, "data-index": e.index, "data-known-size": e.height, style: { overflowAnchor: "none", position: "absolute", width: "100%", top: e.offset }, children: /* @__PURE__ */ T(t, { index: e.index, prevData: e.prevData, nextData: e.nextData, data: e.data, context: i }) } ); }; function oi(e) { return ii(li(ri(si(e), 8 * e.length))).toLowerCase(); } function ii(e) { for (var t, n = "0123456789ABCDEF", o = "", i = 0; i < e.length; i++) t = e.charCodeAt(i), o += n.charAt(t >>> 4 & 15) + n.charAt(15 & t); return o; } function si(e) { for (var t = Array(e.length >> 2), n = 0; n < t.length; n++) t[n] = 0; for (n = 0; n < 8 * e.length; n += 8) t[n >> 5] |= (255 & e.charCodeAt(n / 8)) << n % 32; return t; } function li(e) { for (var t = "", n = 0; n < 32 * e.length; n += 8) t += String.fromCharCode(e[n >> 5] >>> n % 32 & 255); return t; } function ri(e, t) { e[t >> 5] |= 128 << t % 32, e[14 + (t + 64 >>> 9 << 4)] = t; for (var n = 1732584193, o = -271733879, i = -1732584194, s = 271733878, l = 0; l < e.length; l += 16) { const r = n, c = o, a = i, p = s; o = B( o = B( o = B( o = B( o = N( o = N( o = N( o = N( o = D( o = D( o = D( o = D( o = O( o = O( o = O( o = O( o, i = O( i, s = O(s, n = O(n, o, i, s, e[l + 0], 7, -680876936), o, i, e[l + 1], 12, -389564586), n, o, e[l + 2], 17, 606105819 ), s, n, e[l + 3], 22, -1044525330 ), i = O( i, s = O(s, n = O(n, o, i, s, e[l + 4], 7, -176418897), o, i, e[l + 5], 12, 1200080426), n, o, e[l + 6], 17, -1473231341 ), s, n, e[l + 7], 22, -45705983 ), i = O( i, s = O(s, n = O(n, o, i, s, e[l + 8], 7, 1770035416), o, i, e[l + 9], 12, -1958414417), n, o, e[l + 10], 17, -42063 ), s, n, e[l + 11], 22, -1990404162 ), i = O( i, s = O(s, n = O(n, o, i, s, e[l + 12], 7, 1804603682), o, i, e[l + 13], 12, -40341101), n, o, e[l + 14], 17, -1502002290 ), s, n, e[l + 15], 22, 1236535329 ), i = D( i, s = D(s, n = D(n, o, i, s, e[l + 1], 5, -165796510), o, i, e[l + 6], 9, -1069501632), n, o, e[l + 11], 14, 643717713 ), s, n, e[l + 0], 20, -373897302 ), i = D( i, s = D(s, n = D(n, o, i, s, e[l + 5], 5, -701558691), o, i, e[l + 10], 9, 38016083), n, o, e[l + 15], 14, -660478335 ), s, n, e[l + 4], 20, -405537848 ), i = D( i, s = D(s, n = D(n, o, i, s, e[l + 9], 5, 568446438), o, i, e[l + 14], 9, -1019803690), n, o, e[l + 3], 14, -187363961 ), s, n, e[l + 8], 20, 1163531501 ), i = D( i, s = D(s, n = D(n, o, i, s, e[l + 13], 5, -1444681467), o, i, e[l + 2], 9, -51403784), n, o, e[l + 7], 14, 1735328473 ), s, n, e[l + 12], 20, -1926607734 ), i = N( i, s = N(s, n = N(n, o, i, s, e[l + 5], 4, -378558), o, i, e[l + 8], 11, -2022574463), n, o, e[l + 11], 16, 1839030562 ), s, n, e[l + 14], 23, -35309556 ), i = N( i, s = N(s, n = N(n, o, i, s, e[l + 1], 4, -1530992060), o, i, e[l + 4], 11, 1272893353), n, o, e[l + 7], 16, -155497632 ), s, n, e[l + 10], 23, -1094730640 ), i = N( i, s = N(s, n = N(n, o, i, s, e[l + 13], 4, 681279174), o, i, e[l + 0], 11, -358537222), n, o, e[l + 3], 16, -722521979 ), s, n, e[l + 6], 23, 76029189 ), i = N( i, s = N(s, n = N(n, o, i, s, e[l + 9], 4, -640364487), o, i, e[l + 12], 11, -421815835), n, o, e[l + 15], 16, 530742520 ), s, n, e[l + 2], 23, -995338651 ), i = B( i, s = B(s, n = B(n, o, i, s, e[l + 0], 6, -198630844), o, i, e[l + 7], 10, 1126891415), n, o, e[l + 14], 15, -1416354905 ), s, n, e[l + 5], 21, -57434055 ), i = B( i, s = B(s, n = B(n, o, i, s, e[l + 12], 6, 1700485571), o, i, e[l + 3], 10, -1894986606), n, o, e[l + 10], 15, -1051523 ), s, n, e[l + 1], 21, -2054922799 ), i = B( i, s = B(s, n = B(n, o, i, s, e[l + 8], 6, 1873313359), o, i, e[l + 15], 10, -30611744), n, o, e[l + 6], 15, -1560198380 ), s, n, e[l + 13], 21, 1309151649 ), i = B( i, s = B(s, n = B(n, o, i, s, e[l + 4], 6, -145523070), o, i, e[l + 11], 10, -1120210379), n, o, e[l + 2], 15, 718787259 ), s, n, e[l + 9], 21, -343485551 ), n = ue(n, r), o = ue(o, c), i = ue(i, a), s = ue(s, p); } return [n, o, i, s]; } function Rt(e, t, n, o, i, s) { return ue(ui(ue(ue(t, e), ue(o, s)), i), n); } function O(e, t, n, o, i, s, l) { return Rt(t & n | ~t & o, e, t, i, s, l); } function D(e, t, n, o, i, s, l) { return Rt(t & o | n & ~o, e, t, i, s, l); } function N(e, t, n, o, i, s, l) { return Rt(t ^ n ^ o, e, t, i, s, l); } function B(e, t, n, o, i, s, l) { return Rt(n ^ (t | ~o), e, t, i, s, l); } function ue(e, t) { const n = (65535 & e) + (65535 & t); return (e >> 16) + (t >> 16) + (n >> 16) << 16 | 65535 & n; } function ui(e, t) { return e << t | e >>> 32 - t; } const Zn = Symbol("INVALID_KEY"); function ci(e) { const t = e.slice(0, 32), n = e.slice(32), o = atob(n); if (t !== oi(n)) return Zn; const [i, s] = o.split(";"), l = i.slice(2), r = new Date(Number(s.slice(2))); return { orderNumber: l, expiryDate: r }; } const ai = { valid: !1, consoleMessage: "The VirtuosoMessageList license wrapper component is missing. Enclose the VirtuosoMessageList with VirtuosoMessageListLicense and add your key at the lisenceKey property.", watermarkMessage: "The VirtuosoMessageList license wrapper component is missing. Enclose the VirtuosoMessageList with VirtuosoMessageListLicense and add your key at the lisenceKey property." }, pi = { valid: !1, consoleMessage: "Your VirtuosoMessageListLicense is missing a license key. Purchase one from https://virtuoso.dev/pricing/", watermarkMessage: "Your VirtuosoMessageListLicense is missing a license key. Purchase one from https://virtuoso.dev/pricing/" }, fi = { valid: !1, consoleMessage: "Your VirtuosoMessageListLicense component is missing a license key - this component will not work if deployed in production. Purchase a key from https://virtuoso.dev/pricing/ before you deploy to production." }, Jn = { valid: !0 }, hi = { valid: !1, consoleMessage: "Your Virtuoso Message List license key is invalid. Ensure that you have copy-pasted the key from the purchase email correctly.", watermarkMessage: "Your Virtuoso Message List license key is invalid" }, gi = { valid: !1, consoleMessage: "Your annual license key to use Virtuoso Message List in non-production environments has expired. You can still use it in production. To keep using it in development, purchase a new key from https://virtuoso.dev/pricing/", watermarkMessage: "Your annual license key to use Virtuoso Message List in non-production environments has expired. You can still use it in production. To keep using it in development, purchase a new key from https://virtuoso.dev/pricing/" }, di = { valid: !1, consoleMessage: "You have installed a version of `@virtuoso.dev/message-list` that is newer than the period of your license key. Either downgrade to a supported version, or purchase a new license from https://virtuoso.dev/pricing/", watermarkMessage: "You have installed a version of `@virtuoso.dev/message-list` that is newer than the period of your license key. Either downgrade to a supported version, or purchase a new license from https://virtuoso.dev/pricing/" }, mi = Jn, bi = /^(?:127\.0\.0\.1|localhost|0\.0\.0\.0|.+\.local)$/, vi = ["virtuoso.dev", "csb.app", "codesandbox.io"]; function Ii({ licenseKey: e, now: t, hostname: n, packageTimestamp: o }) { const i = n.match(bi), s = vi.some((r) => n.endsWith(r)); if (!e) return s ? mi : i ? fi : pi; const l = ci(e); if (l === Zn) return hi; if (l.expiryDate.getTime() < t.getTime()) { if (i) return gi; if (l.expiryDate.getTime() < o) return di; } return Jn; } const Xn = f.createContext(ai), ki = ({ licenseKey: e, children: t }) => { const n = Ii({ licenseKey: e, hostname: typeof window < "u" ? window.location.hostname : "localhost", now: /* @__PURE__ */ new Date(), packageTimestamp: 1750570168712 }); return /* @__PURE__ */ T(Xn.Provider, { value: n, children: t }); }; ki.displayName = Math.random().toString(36).slice(2, 8); const Ct = f.createContext(void 0), Qn = u(null), eo = u(null), to = u(null), no = u(null), oo = u(null), io = u("div"), Si = { position: "sticky", top: 0, zIndex: 1 }, ut = { overflowAnchor: "none" }, xi = { position: "sticky", bottom: 0 }, so = f.forwardRef((e, t) => /* @__PURE__ */ T("div", { style: { zIndex: 1 }, ...e, ref: t })), lo = f.forwardRef((e, t) => /* @__PURE__ */ T("div", { ...e, ref: t })), ro = f.forwardRef( ({ style: e, ...t }, n) => /* @__PURE__ */ T("div", { ...t, style: { ...Si, ...e }, ref: n }) ), uo = f.forwardRef( ({ style: e, ...t }, n) => /* @__PURE__ */ T("div", { ...t, style: { ...xi, ...e }, ref: n }) ), co = u(so), ao = u(ro), po = u(lo), fo = u(uo), ho = ({ index: e }) => /* @__PURE__ */ dn("div", { children: [ "Item ", e ] }), go = ({ index: e }) => e, qt = u(ho), mo = u(go), Gt = u("top", (e) => { e.link( e.pipe( e.combine(Gt, Ne, j, nt, Yn), I(([t]) => t === "bottom" || t === "bottom-smooth"), g(([, t, n, o, i]) => Math.max(0, n - t - o - i)) ), Et ), e.link( e.pipe( e.combine(Et, Gt), I(([, t]) => t === "bottom-smooth"), te( (t, [n]) => [t[1], n], [0, 0] ), g(([t, n]) => t > 0 && n > 0 ? "margin-top 0.2s ease-out" : "") ), jn ); }); function ct(e) { const t = f.useRef(null); return [f.useCallback( (o) => { o ? (t.current = o, e == null || e.observe(o, { box: "border-box" })) : t.current && (e == null || e.unobserve(t.current), t.current = null); }, [e] ), t]; } function Ti(e, t, n) { const o = Te(), i = f.useRef(null), s = f.useRef(null), l = f.useCallback(() => { i.current && (cancelAnimationFrame(i.current), i.current = null, s.current = null); }, []); f.useEffect(() => o.sub(on, (a) => { a !== s.current && l(); }), [o, l]), f.useEffect(() => o.sub(Wn, l), [o, l]); const r = f.useCallback( (a, p, m) => { var L; i.current && l(); const v = ((L = e.current) == null ? void 0 : L.scrollTop) ?? 0; s.current = v < a ? "down" : "up"; let b = 0, h = 0; function d() { var q, K; const U = v + (a - v) * p(b); (q = e.current) == null || q.scrollTo({ top: U, behavior: "instant" }), b += 1 / m, h += 1, h < m ? i.current = requestAnimationFrame(d) : ((K = e.current) == null || K.scrollTo({ top: a, behavior: "instant" }), i.current = null, s.current = null); } d(); }, [e, l] ); return f.useCallback( (a) => { var p, m, v, b; if (a.top === ((p = e.current) == null ? void 0 : p.scrollTop)) { o.pub(Qe, (m = e.current) == null ? void 0 : m.scrollTop); return; } if (a.top !== void 0 && (n.current = a.top, o.pub(en, !0)), a.forceBottomSpace !== void 0 && t.current && (t.current.style.paddingBottom = `${a.forceBottomSpace}px`), a.behavior === "smooth") r(a.top ?? 0, Jo, Xo); else if (a.behavior === "auto" || a.behavior === "instant" || a.behavior === void 0) l(), (v = e.current) == null || v.scrollTo(a); else { const { easing: h, animationFrameCount: d } = a.behavior(((b = e.current) == null ? void 0 : b.scrollTop) ?? 0, a.top ?? 0); r(a.top ?? 0, h, d); } }, [o, r, t, e, n, l] ); } function bo(e) { return { data: { prepend: (t) => { e.pub(re, t); }, append: (t, n) => { e.pub(yt, { data: t, scrollToBottom: n }); }, replace: (t, n) => { e.pub(Ue, { ...n, data: t }); }, map: (t, n) => { e.pub(Ut, { mapper: t, autoscrollToBottomBehavior: n }); }, findAndDelete: (t) => { e.pub(Gn, t); }, findIndex: (t) => e.getValue(R).findIndex(t), find: (t) => e.getValue(R).find(t), insert: (t, n, o) => { e.pub(vt, { data: t, offset: n, scrollToBottom: o }); }, deleteRange: (t, n) => { e.pub(jt, { offset: t, count: n }); }, batch: (t, n) => { e.pub(Ce, !0), t(), e.pub(Ce, !1), e.pub(Lt, { data: [], scrollToBottom: n }); }, get: () => e.getValue(R).slice(), getCurrentlyRendered: () => e.getValue(Tt), removeFromStart: (t) => { e.pub(It, t); } }, scrollToItem: (t) => { e.pub(J, t); }, scrollIntoView: (t) => { e.pub(Fn, t); }, scrollerElement: () => e.getValue(Se), getScrollLocation() { return e.getValue(ot); }, cancelSmoothScroll() { e.pub(Wn); }, height: (t) => { var i; const n = ((i = e.getValue(R)) == null ? void 0 : i.indexOf(t)) ?? -1; if (n === -1) return 0; const o = e.getValue(Z); return ce(o, n)[1] ?? 0; } }; } let gn = !1; const Ei = f.forwardRe