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