@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
422 lines (420 loc) • 12.1 kB
JavaScript
import { ACCESS_DENIED_MESSAGES, isAccessError } from "./route/route.js";
import { useEffect, useRef, useState, useSyncExternalStore } from "react";
import { useRouter } from "@tanstack/react-router";
import { jsx, jsxs } from "react/jsx-runtime";
//#region src/default-error-overlay.tsx
/** Set by Studio's canvas engine, which ships apart from the SDK each app pins, so renaming it breaks older apps. */
const AGENT_TURN_ATTRIBUTE = "data-agent-turn";
function subscribeToAgentTurn(listener) {
const observer = new MutationObserver(listener);
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: [AGENT_TURN_ATTRIBUTE]
});
return () => observer.disconnect();
}
function isAgentTurnRunning() {
return document.documentElement.getAttribute(AGENT_TURN_ATTRIBUTE) === "running";
}
function DefaultErrorOverlay({ error, reset }) {
if (process.env.NODE_ENV === "production") return /* @__PURE__ */ jsx("div", {
style: s.overlay,
"data-softr-error-overlay": "error",
children: /* @__PURE__ */ jsxs("div", {
style: s.card,
children: [
/* @__PURE__ */ jsx(AlertIcon, {}),
/* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("h1", {
style: s.heading,
children: "Something went wrong"
}), /* @__PURE__ */ jsx("p", {
style: s.body,
children: "This page couldn't be shown. Try again in a moment."
})] }),
/* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(RetryButton, { onClick: reset }) })
]
})
});
return /* @__PURE__ */ jsx(DevView, {
error,
reset
});
}
function DevView({ error, reset }) {
const running = useSyncExternalStore(subscribeToAgentTurn, isAgentTurnRunning, () => false);
const [retryPending, setRetryPending] = useState(running);
const router = useRouter();
useEffect(() => {
if (running) {
setRetryPending(true);
return;
}
if (!retryPending) return;
const showError = () => setRetryPending(false);
const timer = setTimeout(() => {
router.invalidate().then(() => {
reset?.();
showError();
}, showError);
}, 1e3);
return () => clearTimeout(timer);
}, [
running,
retryPending,
reset,
router
]);
const shown = !running && !retryPending;
useEffect(() => {
if (shown) reportError(error instanceof Error ? error : new Error(messageOf(error)));
}, [shown, error]);
if (!shown) return /* @__PURE__ */ jsx(UpdatingView, {});
return /* @__PURE__ */ jsx("div", {
style: s.overlay,
"data-softr-error-overlay": "error",
children: /* @__PURE__ */ jsxs("div", {
style: s.card,
children: [
/* @__PURE__ */ jsx(AlertIcon, {}),
/* @__PURE__ */ jsxs("div", { children: [
/* @__PURE__ */ jsx("h1", {
style: s.heading,
children: "Something went wrong"
}),
/* @__PURE__ */ jsx("p", {
style: s.body,
children: "A render error was caught. Copy the details to the agent to help fix the issue."
}),
/* @__PURE__ */ jsx("p", {
style: s.note,
children: "Only shown in development mode."
})
] }),
/* @__PURE__ */ jsx("pre", {
style: s.message,
children: messageOf(error)
}),
error instanceof Error && error.stack && /* @__PURE__ */ jsx(StackTrace, { stack: error.stack }),
/* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(RetryButton, { onClick: reset }) })
]
})
});
}
function messageOf(error) {
if (error instanceof Error) return error.message;
if (isAccessError(error)) return ACCESS_DENIED_MESSAGES[error.reason];
try {
return JSON.stringify(error) ?? String(error);
} catch {
return String(error);
}
}
function UpdatingView() {
return /* @__PURE__ */ jsx("div", {
style: s.overlay,
"data-softr-error-overlay": "updating",
children: /* @__PURE__ */ jsxs("div", {
style: s.card,
children: [/* @__PURE__ */ jsx("div", {
style: s.pending,
children: /* @__PURE__ */ jsxs("svg", {
style: icon.md,
viewBox: "0 0 24 24",
fill: "none",
stroke: "currentColor",
strokeWidth: "2",
strokeLinecap: "round",
strokeLinejoin: "round",
children: [
/* @__PURE__ */ jsx("path", { d: "M3 12a9 9 0 0 1 9-9 9.75 9.75 0 0 1 6.74 2.74L21 8" }),
/* @__PURE__ */ jsx("path", { d: "M21 3v5h-5" }),
/* @__PURE__ */ jsx("path", { d: "M21 12a9 9 0 0 1-9 9 9.75 9.75 0 0 1-6.74-2.74L3 16" }),
/* @__PURE__ */ jsx("path", { d: "M8 16H3v5" })
]
})
}), /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("h1", {
style: s.heading,
children: "Updating the app…"
}), /* @__PURE__ */ jsx("p", {
style: s.body,
children: "The agent is still working on this change. The preview will recover once it is complete."
})] })]
})
});
}
function cleanStack(stack) {
return stack.split("\n").slice(1).filter((line) => line.trim().startsWith("at ") && !line.includes("/node_modules/")).map((line) => line.replace(/https?:\/\/[^/]+\//, "").replace(/\?[^):]+/g, "")).join("\n");
}
function StackTrace({ stack }) {
const cleaned = cleanStack(stack);
const [open, setOpen] = useState(false);
const [copied, setCopied] = useState(false);
const ref = useRef(null);
if (!cleaned) return null;
async function copyStack() {
try {
await navigator.clipboard.writeText(cleaned);
return true;
} catch {
if (ref.current) getSelection()?.selectAllChildren(ref.current);
return document.execCommand("copy");
}
}
async function copy() {
if (!await copyStack()) return;
setCopied(true);
setTimeout(() => setCopied(false), 1500);
}
return /* @__PURE__ */ jsxs("div", {
style: {
display: "flex",
flexDirection: "column",
gap: "0.5rem"
},
children: [/* @__PURE__ */ jsxs("button", {
type: "button",
style: s.toggle,
"aria-expanded": open,
onClick: () => setOpen((v) => !v),
children: [/* @__PURE__ */ jsx("svg", {
style: {
width: "0.875rem",
height: "0.875rem",
flexShrink: 0,
transition: "transform 150ms",
transform: open ? "rotate(90deg)" : "none"
},
viewBox: "0 0 24 24",
fill: "none",
stroke: "currentColor",
strokeWidth: "2",
strokeLinecap: "round",
strokeLinejoin: "round",
children: /* @__PURE__ */ jsx("path", { d: "m9 18 6-6-6-6" })
}), "Stack trace"]
}), open && /* @__PURE__ */ jsxs("div", {
style: { position: "relative" },
children: [/* @__PURE__ */ jsx("pre", {
ref,
style: s.stack,
children: cleaned
}), /* @__PURE__ */ jsx("button", {
type: "button",
style: s.copy,
"aria-label": "Copy stack trace",
onClick: copy,
children: copied ? /* @__PURE__ */ jsx("svg", {
style: icon.sm,
viewBox: "0 0 24 24",
fill: "none",
stroke: "currentColor",
strokeWidth: "2",
strokeLinecap: "round",
strokeLinejoin: "round",
children: /* @__PURE__ */ jsx("path", { d: "M20 6 9 17l-5-5" })
}) : /* @__PURE__ */ jsxs("svg", {
style: icon.sm,
viewBox: "0 0 24 24",
fill: "none",
stroke: "currentColor",
strokeWidth: "2",
strokeLinecap: "round",
strokeLinejoin: "round",
children: [/* @__PURE__ */ jsx("rect", {
width: "14",
height: "14",
x: "8",
y: "8",
rx: "2",
ry: "2"
}), /* @__PURE__ */ jsx("path", { d: "M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" })]
})
})]
})]
});
}
function AlertIcon() {
return /* @__PURE__ */ jsx("div", {
style: s.alert,
children: /* @__PURE__ */ jsxs("svg", {
style: icon.md,
viewBox: "0 0 24 24",
fill: "none",
stroke: "currentColor",
strokeWidth: "2",
strokeLinecap: "round",
strokeLinejoin: "round",
children: [
/* @__PURE__ */ jsx("path", { d: "m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3" }),
/* @__PURE__ */ jsx("path", { d: "M12 9v4" }),
/* @__PURE__ */ jsx("path", { d: "M12 17h.01" })
]
})
});
}
function RetryButton({ onClick }) {
return /* @__PURE__ */ jsxs("button", {
type: "button",
style: s.retry,
onClick,
children: [/* @__PURE__ */ jsxs("svg", {
style: icon.sm,
viewBox: "0 0 24 24",
fill: "none",
stroke: "currentColor",
strokeWidth: "2",
strokeLinecap: "round",
strokeLinejoin: "round",
children: [
/* @__PURE__ */ jsx("path", { d: "M3 12a9 9 0 0 1 9-9 9.75 9.75 0 0 1 6.74 2.74L21 8" }),
/* @__PURE__ */ jsx("path", { d: "M21 3v5h-5" }),
/* @__PURE__ */ jsx("path", { d: "M21 12a9 9 0 0 1-9 9 9.75 9.75 0 0 1-6.74-2.74L3 16" }),
/* @__PURE__ */ jsx("path", { d: "M8 16H3v5" })
]
}), "Try again"]
});
}
const icon = {
sm: {
width: "0.875rem",
height: "0.875rem"
},
md: {
width: "1.25rem",
height: "1.25rem"
}
};
const s = {
overlay: {
minHeight: "100vh",
display: "grid",
placeItems: "center",
gridTemplateColumns: "minmax(0, 1fr)",
padding: "1.5rem"
},
card: {
width: "100%",
maxWidth: "36rem",
display: "flex",
flexDirection: "column",
gap: "1.25rem",
padding: "1.5rem",
borderRadius: "calc(var(--radius, 0.625rem) + 4px)",
background: "var(--card, oklch(1 0 0))",
boxShadow: "0 0 0 1px color-mix(in oklch, var(--foreground, oklch(0.145 0 0)) 10%, transparent)"
},
alert: {
display: "flex",
alignItems: "center",
justifyContent: "center",
width: "2.5rem",
height: "2.5rem",
borderRadius: "var(--radius, 0.625rem)",
background: "color-mix(in oklch, var(--destructive, oklch(0.577 0.245 27.325)) 12%, transparent)",
color: "var(--destructive, oklch(0.577 0.245 27.325))",
flexShrink: 0
},
pending: {
display: "flex",
alignItems: "center",
justifyContent: "center",
width: "2.5rem",
height: "2.5rem",
borderRadius: "var(--radius, 0.625rem)",
background: "var(--muted, oklch(0.97 0 0))",
color: "var(--muted-foreground, oklch(0.556 0 0))",
flexShrink: 0
},
heading: {
margin: 0,
fontSize: "1rem",
fontWeight: 500,
lineHeight: 1.375
},
body: {
margin: "0.375rem 0 0",
fontSize: "0.875rem",
color: "var(--muted-foreground, oklch(0.556 0 0))"
},
note: {
margin: "0.75rem 0 0",
fontSize: "0.875rem",
color: "var(--muted-foreground, oklch(0.556 0 0))",
opacity: .65
},
message: {
margin: 0,
padding: "0.75rem",
borderRadius: "var(--radius, 0.625rem)",
background: "var(--muted, oklch(0.97 0 0))",
fontFamily: "ui-monospace, SFMono-Regular, Menlo, monospace",
fontSize: "0.8125rem",
lineHeight: 1.6,
whiteSpace: "pre-wrap",
overflowWrap: "anywhere",
maxHeight: "12rem",
overflow: "auto"
},
toggle: {
display: "inline-flex",
alignItems: "center",
gap: "0.375rem",
height: "2rem",
padding: "0 0.75rem 0 0.5rem",
border: "1px solid var(--input, oklch(0.922 0 0))",
borderRadius: "var(--radius, 0.625rem)",
background: "transparent",
color: "var(--foreground, oklch(0.145 0 0))",
fontFamily: "inherit",
fontSize: "0.875rem",
fontWeight: 500,
cursor: "pointer",
userSelect: "none"
},
stack: {
margin: 0,
padding: "0.75rem",
paddingRight: "2.75rem",
borderRadius: "var(--radius, 0.625rem)",
background: "var(--muted, oklch(0.97 0 0))",
fontFamily: "ui-monospace, SFMono-Regular, Menlo, monospace",
fontSize: "0.75rem",
lineHeight: 1.6,
maxHeight: "16rem",
overflow: "auto"
},
copy: {
position: "absolute",
top: "0.375rem",
right: "0.375rem",
display: "inline-flex",
alignItems: "center",
justifyContent: "center",
width: "1.75rem",
height: "1.75rem",
padding: 0,
border: "1px solid var(--input, oklch(0.922 0 0))",
borderRadius: "calc(var(--radius, 0.625rem) - 2px)",
background: "var(--card, oklch(1 0 0))",
color: "var(--muted-foreground, oklch(0.556 0 0))",
cursor: "pointer"
},
retry: {
display: "inline-flex",
alignItems: "center",
gap: "0.375rem",
height: "2rem",
padding: "0 0.75rem",
border: "1px solid transparent",
borderRadius: "var(--radius, 0.625rem)",
background: "var(--primary, oklch(0.205 0 0))",
color: "var(--primary-foreground, oklch(0.985 0 0))",
fontFamily: "inherit",
fontSize: "0.875rem",
fontWeight: 500,
cursor: "pointer",
userSelect: "none"
}
};
//#endregion
export { DefaultErrorOverlay };