alert.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use client";
  2. import { type ComponentRenderProps } from "@json-render/react";
  3. import { useData } from "@json-render/react";
  4. import { getByPath } from "@json-render/core";
  5. function useResolvedValue<T>(
  6. value: T | { path: string } | null | undefined,
  7. ): T | undefined {
  8. const { data } = useData();
  9. if (value === null || value === undefined) return undefined;
  10. if (typeof value === "object" && "path" in value) {
  11. return getByPath(data, value.path) as T | undefined;
  12. }
  13. return value as T;
  14. }
  15. export function Alert({ element }: ComponentRenderProps) {
  16. const { message, variant } = element.props as {
  17. message: string | { path: string };
  18. variant?: string | null;
  19. };
  20. const resolvedMessage = useResolvedValue(message);
  21. const colors: Record<string, string> = {
  22. info: "var(--muted)",
  23. success: "#22c55e",
  24. warning: "#eab308",
  25. error: "#ef4444",
  26. };
  27. return (
  28. <div
  29. style={{
  30. padding: "12px 16px",
  31. borderRadius: "var(--radius)",
  32. background: "var(--card)",
  33. border: "1px solid var(--border)",
  34. fontSize: 14,
  35. color: colors[variant || "info"],
  36. }}
  37. >
  38. {resolvedMessage}
  39. </div>
  40. );
  41. }