@royaltics/tracker-react
Version:
React hooks for Royaltics Error Tracker
111 lines (110 loc) • 2.83 kB
JavaScript
import { jsx as a, Fragment as k, jsxs as w } from "react/jsx-runtime";
import { createContext as x, useContext as v, useRef as y, useEffect as C, Component as g, useCallback as s } from "react";
import { Tracker as u } from "@royaltics/tracker";
const l = x(null), p = () => {
const r = v(l);
if (!r)
throw new Error("useErrorTrackerContext must be used within ErrorTrackerProvider");
return r;
}, F = ({
config: r,
children: t
}) => {
const e = y(null);
return C(() => (e.current || (e.current = u.create({
...r,
platform: "react",
app: "react-app"
})), () => {
e.current && (u.shutdown(), e.current = null);
}), [r]), e.current ? /* @__PURE__ */ a(l.Provider, { value: e.current, children: t }) : /* @__PURE__ */ a(k, { children: t });
};
class R extends g {
constructor(t) {
super(t), this.state = { hasError: !1, error: null };
}
static getDerivedStateFromError(t) {
return { hasError: !0, error: t instanceof Error ? t : new Error(String(t ?? "Unknown error")) };
}
componentDidCatch(t, e) {
try {
const c = t instanceof Error ? t : new Error(String(t ?? "Unknown error"));
this.props.client.error(c, "ERROR", {
componentStack: e.componentStack,
source: "ErrorBoundary"
});
} catch (c) {
console.error("Failed to track error:", c);
}
}
render() {
return this.state.hasError && this.state.error ? this.props.fallback ? typeof this.props.fallback == "function" ? this.props.fallback(this.state.error) : this.props.fallback : /* @__PURE__ */ w("div", { style: { padding: "20px", textAlign: "center" }, children: [
/* @__PURE__ */ a("h1", { children: "Something went wrong" }),
/* @__PURE__ */ a("p", { children: this.state.error.message })
] }) : this.props.children;
}
}
const S = () => {
const r = p(), t = s(
(o, n) => {
r.error(o, "ERROR", n);
},
[r]
), e = s(
(o, n) => {
r.error(o, "FATAL", n);
},
[r]
), c = s(
(o, n) => {
r.error(o, "DEBUG", n);
},
[r]
), E = s(
(o, n, i) => {
r.error(o, n, i);
},
[r]
), d = s(
(o, n) => {
r.event(o, "INFO", n);
},
[r]
), f = s(
(o, n) => {
r.event(o, "WARNING", n);
},
[r]
), h = s(
(o, n, i) => {
r.event(o, n, i);
},
[r]
), m = s(async () => {
await r.forceFlush();
}, [r]);
return {
error: t,
fatal: e,
debug: c,
capture: E,
info: d,
warn: f,
event: h,
flush: m
};
}, A = () => {
const r = p();
return {
ErrorBoundary: ({
children: e,
fallback: c
}) => /* @__PURE__ */ a(R, { client: r, fallback: c, children: e })
};
};
export {
R as ErrorBoundary,
F as ErrorTrackerProvider,
A as useErrorBoundary,
S as useErrorTracker
};