jsx-preview.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Derived from vercel/ai-elements (skills/ai-elements/scripts/jsx-preview.tsx).
  3. * Copyright 2023 Vercel, Inc. Licensed under Apache-2.0.
  4. * Modified by ZCode: local integration, formatting and adaptations.
  5. * See THIRD-PARTY-NOTICES.md in the repository root for license and provenance.
  6. */
  7. "use client";
  8. import {
  9. JSXPreview,
  10. JSXPreviewContent,
  11. JSXPreviewError,
  12. } from "@/components/ai-elements/jsx-preview";
  13. import { Button } from "@/components/ui/button";
  14. import { useCallback, useEffect, useRef, useState } from "react";
  15. const handleError = (error: Error) => {
  16. console.log("JSX Parse Error:", error);
  17. };
  18. const fullJsx = `<div className="rounded-lg border bg-card p-6 shadow-sm">
  19. <div className="flex items-center gap-4 mb-4">
  20. <div className="h-12 w-12 rounded-full bg-primary/10 flex items-center justify-center">
  21. <span className="text-primary text-xl font-bold">AI</span>
  22. </div>
  23. <div>
  24. <h2 className="text-lg font-semibold">AI-Generated Component</h2>
  25. <p className="text-sm text-muted-foreground">Rendered from JSX string</p>
  26. </div>
  27. </div>
  28. <div className="space-y-3">
  29. <p className="text-sm">This component was dynamically rendered from a JSX string. The JSXPreview component supports streaming mode, automatically closing unclosed tags as content arrives.</p>
  30. <div className="flex gap-2">
  31. <span className="inline-flex items-center rounded-full bg-blue-100 px-2.5 py-0.5 text-xs font-medium text-blue-800">React</span>
  32. <span className="inline-flex items-center rounded-full bg-green-100 px-2.5 py-0.5 text-xs font-medium text-green-800">Streaming</span>
  33. <span className="inline-flex items-center rounded-full bg-purple-100 px-2.5 py-0.5 text-xs font-medium text-purple-800">Dynamic</span>
  34. </div>
  35. </div>
  36. <div className="mt-4 pt-4 border-t">
  37. <div className="flex justify-between items-center">
  38. <span className="text-xs text-muted-foreground">Generated just now</span>
  39. <button className="px-3 py-1.5 text-sm bg-primary text-primary-foreground rounded-md hover:bg-primary/90 transition-colors">
  40. Learn more
  41. </button>
  42. </div>
  43. </div>
  44. </div>`;
  45. const Example = () => {
  46. const [streamedJsx, setStreamedJsx] = useState(fullJsx);
  47. const [isStreaming, setIsStreaming] = useState(false);
  48. const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
  49. const simulateStreaming = useCallback(() => {
  50. setIsStreaming(true);
  51. setStreamedJsx("");
  52. let index = 0;
  53. if (intervalRef.current) {
  54. clearInterval(intervalRef.current);
  55. }
  56. intervalRef.current = setInterval(() => {
  57. if (index < fullJsx.length) {
  58. setStreamedJsx(fullJsx.slice(0, index + 15));
  59. index += 15;
  60. } else {
  61. setIsStreaming(false);
  62. if (intervalRef.current) {
  63. clearInterval(intervalRef.current);
  64. intervalRef.current = null;
  65. }
  66. }
  67. }, 30);
  68. }, []);
  69. useEffect(
  70. () => () => {
  71. if (intervalRef.current) {
  72. clearInterval(intervalRef.current);
  73. }
  74. },
  75. [],
  76. );
  77. return (
  78. <div className="space-y-4">
  79. <Button disabled={isStreaming} onClick={simulateStreaming} size="sm" variant="outline">
  80. {isStreaming ? "Streaming..." : "Simulate Streaming"}
  81. </Button>
  82. <JSXPreview
  83. className="min-h-[200px]"
  84. isStreaming={isStreaming}
  85. jsx={streamedJsx}
  86. onError={handleError}
  87. >
  88. <JSXPreviewContent />
  89. <JSXPreviewError className="mt-2" />
  90. </JSXPreview>
  91. </div>
  92. );
  93. };
  94. export default Example;