UNPKG

drizzle-cube

Version:

Drizzle ORM-first semantic layer with Cube.js compatibility. Type-safe analytics and dashboards with SQL injection protection.

174 lines (173 loc) 4.54 kB
import { useState as b, useRef as w, useEffect as y, useCallback as C, useMemo as m } from "react"; import { u as L } from "./chunks/providers-DONuYrGH.js"; import { a as F } from "./chunks/providers-DONuYrGH.js"; function W(e, r = {}) { const { cubeApi: s, batchCoordinator: u, enableBatching: a } = L(), [l, d] = b({ resultSet: null, isLoading: !1, error: null, queryId: null }), f = w(""); return y(() => { if (!e || r.skip) return; const n = JSON.stringify(e); if (n === f.current && !r.resetResultSetOnChange) return; f.current = n; const o = `${Date.now()}_${Math.random().toString(36).substring(7)}`; d((t) => ({ resultSet: r.resetResultSetOnChange ? null : t.resultSet, isLoading: !0, error: null, queryId: o })), (a && u ? u.register(e) : s.load(e)).then((t) => { d((h) => h.queryId === o ? { resultSet: t, isLoading: !1, error: null, queryId: o } : h); }).catch((t) => { d((h) => h.queryId === o ? { resultSet: null, isLoading: !1, error: t instanceof Error ? t : new Error(String(t)), queryId: o } : h); }); }, [e, s, u, a, r.skip, r.resetResultSetOnChange]), l; } function D(e, r = !0) { const [s, u] = b([]), [a, l] = b(null), d = w(null), f = w(""), { resultSet: n, isLoading: o, error: i, queryId: t } = W(a, { skip: !a || !r, resetResultSetOnChange: !0 // Clear old results when query changes }), h = C((c) => { if (!c || !e) return []; try { const S = c.tablePivot(), g = /* @__PURE__ */ new Set(); return S.forEach((v) => { const E = v[e]; E != null && E !== "" && g.add(E); }), Array.from(g); } catch (S) { return console.error("Error extracting values from result set:", S), []; } }, [e]); y(() => { if (t && t !== d.current && !o) if (d.current = t, i) u([]); else if (n) { const c = h(n); u(c); } else u([]); }, [n, o, i, t, h]), y(() => { (!e || !r) && (u([]), l(null), d.current = null, f.current = ""); }, [e, r]); const I = C(() => { if (e) { f.current = ""; try { const c = { dimensions: [e], limit: 25, order: { [e]: "asc" } }; l(c); } catch (c) { console.error("Error creating query:", c); } } }, [e]), V = C((c, S = !1) => { if (e && !(!S && c === f.current)) { f.current = c; try { const g = { dimensions: [e], limit: 25, order: { [e]: "asc" } }; c && c.trim() && (g.filters = [{ member: e, operator: "contains", values: [c.trim()] }]), l(g); } catch (g) { console.error("Error creating search query:", g); } } }, [e]); return { values: s, loading: o, error: i ? i instanceof Error ? i.message : String(i) : null, refetch: I, searchValues: V }; } function Q(e, r) { const [s, u] = b(e); return y(() => { const a = setTimeout(() => { u(e); }, r); return () => { clearTimeout(a); }; }, [e, r]), s; } const R = 1200, p = 768; function k() { const [e, r] = b( () => typeof window < "u" ? window.innerWidth : R ), s = w(null), u = w(null), a = C((n) => { if (s.current && (s.current.disconnect(), s.current = null), u.current = n, n) { const o = n.offsetWidth; o > 0 && r(o), s.current = new ResizeObserver((i) => { const t = i[0]?.contentRect.width; t && t > 0 && r(t); }), s.current.observe(n); } }, []); y(() => () => { s.current && s.current.disconnect(); }, []), y(() => { const n = () => { if (u.current) { const i = u.current.offsetWidth; i > 0 && r(i); } }; window.addEventListener("resize", n); const o = setTimeout(n, 100); return () => { window.removeEventListener("resize", n), clearTimeout(o); }; }, []); const l = m(() => e >= R ? "desktop" : e >= p ? "scaled" : "mobile", [e]), d = m(() => l !== "scaled" ? 1 : e / R, [e, l]); return { containerRef: a, containerWidth: e, displayMode: l, scaleFactor: d, isEditable: l === "desktop", designWidth: R }; } export { F as useCubeMeta, W as useCubeQuery, Q as useDebounce, D as useFilterValues, k as useResponsiveDashboard }; //# sourceMappingURL=hooks.js.map