drizzle-cube
Version:
Drizzle ORM-first semantic layer with Cube.js compatibility. Type-safe analytics and dashboards with SQL injection protection.
135 lines (134 loc) • 3.31 kB
JavaScript
import { useState as h, useRef as b, useEffect as C, useCallback as V } from "react";
import { u as I } from "./chunks/providers-DwDirAYo.js";
import { a as v } from "./chunks/providers-DwDirAYo.js";
function L(r, t = {}) {
const { cubeApi: i } = I(), [o, c] = h({
resultSet: null,
isLoading: !1,
error: null,
queryId: null
}), a = b("");
return C(() => {
if (!r || t.skip)
return;
const f = JSON.stringify(r);
if (f === a.current && !t.resetResultSetOnChange)
return;
a.current = f;
const u = `${Date.now()}_${Math.random().toString(36).substring(7)}`;
c((n) => ({
resultSet: t.resetResultSetOnChange ? null : n.resultSet,
isLoading: !0,
error: null,
queryId: u
})), i.load(r).then((n) => {
c((s) => s.queryId === u ? {
resultSet: n,
isLoading: !1,
error: null,
queryId: u
} : s);
}).catch((n) => {
c((s) => s.queryId === u ? {
resultSet: null,
isLoading: !1,
error: n instanceof Error ? n : new Error(String(n)),
queryId: u
} : s);
});
}, [r, i, t.skip, t.resetResultSetOnChange]), o;
}
function m(r, t = !0) {
const [i, o] = h([]), [c, a] = h(null), f = b(null), u = b(""), {
resultSet: n,
isLoading: s,
error: g,
queryId: y
} = L(c, {
skip: !c || !t,
resetResultSetOnChange: !0
// Clear old results when query changes
}), q = V((e) => {
if (!e || !r)
return [];
try {
const S = e.tablePivot(), l = /* @__PURE__ */ new Set();
return S.forEach((x) => {
const d = x[r];
d != null && d !== "" && l.add(d);
}), Array.from(l);
} catch (S) {
return console.error("Error extracting values from result set:", S), [];
}
}, [r]);
C(() => {
if (y && y !== f.current && !s)
if (f.current = y, g)
o([]);
else if (n) {
const e = q(n);
o(e);
} else
o([]);
}, [n, s, g, y, q]), C(() => {
(!r || !t) && (o([]), a(null), f.current = null, u.current = "");
}, [r, t]);
const E = V(() => {
if (r) {
u.current = "";
try {
const e = {
dimensions: [r],
limit: 25,
order: { [r]: "asc" }
};
a(e);
} catch (e) {
console.error("Error creating query:", e);
}
}
}, [r]), R = V((e, S = !1) => {
if (r && !(!S && e === u.current)) {
u.current = e;
try {
const l = {
dimensions: [r],
limit: 25,
order: { [r]: "asc" }
};
e && e.trim() && (l.filters = [{
member: r,
operator: "contains",
values: [e.trim()]
}]), a(l);
} catch (l) {
console.error("Error creating search query:", l);
}
}
}, [r]);
return {
values: i,
loading: s,
error: g ? g instanceof Error ? g.message : String(g) : null,
refetch: E,
searchValues: R
};
}
function p(r, t) {
const [i, o] = h(r);
return C(() => {
const c = setTimeout(() => {
o(r);
}, t);
return () => {
clearTimeout(c);
};
}, [r, t]), i;
}
export {
v as useCubeMeta,
L as useCubeQuery,
p as useDebounce,
m as useFilterValues
};
//# sourceMappingURL=hooks.js.map