alert.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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>(value: T | { path: string } | null | undefined): T | undefined {
  6. const { data } = useData();
  7. if (value === null || value === undefined) return undefined;
  8. if (typeof value === 'object' && 'path' in value) {
  9. return getByPath(data, value.path) as T | undefined;
  10. }
  11. return value as T;
  12. }
  13. export function Alert({ element }: ComponentRenderProps) {
  14. const { message, variant } = element.props as { message: string | { path: string }; variant?: string | null };
  15. const resolvedMessage = useResolvedValue(message);
  16. const colors: Record<string, string> = {
  17. info: 'var(--muted)',
  18. success: '#22c55e',
  19. warning: '#eab308',
  20. error: '#ef4444',
  21. };
  22. return (
  23. <div
  24. style={{
  25. padding: '12px 16px',
  26. borderRadius: 'var(--radius)',
  27. background: 'var(--card)',
  28. border: '1px solid var(--border)',
  29. fontSize: 14,
  30. color: colors[variant || 'info'],
  31. }}
  32. >
  33. {resolvedMessage}
  34. </div>
  35. );
  36. }