Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

1.6 KB
Raw Blame
import { toast } from "sonner";
import { ErrorNotification } from "@/src/features/notifications/ErrorNotification";
import { type ToastErrorAnalytics } from "@/src/features/notifications/toastAnalytics";

const toastErrorStyleProps = {
  border: "1px solid hsl(var(--destructive))",
  backgroundColor: "hsl(var(--destructive))",
};

// `--light-yellow` is a tint with baked-in alpha, meant to sit on top of a
// solid surface. A toast floats over arbitrary page content, so the tint is
// layered over an opaque background here rather than used as the background
// itself — otherwise the page shows through the toast.
const toastWarningStyleProps = {
  border: "1px solid var(--dark-yellow)",
  backgroundColor: "hsl(var(--background))",
  backgroundImage: "linear-gradient(var(--light-yellow), var(--light-yellow))",
};

export const showErrorToast = (
  error: string,
  description: string,
  analytics: ToastErrorAnalytics,
  type: "WARNING" | "ERROR" = "ERROR",
  path?: string,
  traceId?: string,
  source: "application" | "trpc" = "application",
) => {
  return toast.custom(
    (t) => (
      <ErrorNotification
        error={error}
        description={description}
        type={type}
        source={source}
        path={path}
        traceId={traceId ?? analytics.errorId}
        analytics={analytics}
        dismissToast={toast.dismiss}
        toast={t}
      />
    ),
    {
      duration: Infinity,
      style: {
        padding: "1rem",
        borderRadius: "0.5rem",
        ...(type === "ERROR" ? toastErrorStyleProps : toastWarningStyleProps),
      },
    },
  );
};