terminal-streaming.tsx 1007 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use client";
  2. import { Terminal } from "@/components/ai-elements/terminal";
  3. import { useEffect, useState } from "react";
  4. const lines = [
  5. "\u001B[36m$\u001B[0m npm install",
  6. "Installing dependencies...",
  7. "\u001B[32m✓\u001B[0m react@19.0.0",
  8. "\u001B[32m✓\u001B[0m typescript@5.0.0",
  9. "\u001B[32m✓\u001B[0m vite@5.0.0",
  10. "",
  11. "\u001B[32mDone!\u001B[0m Installed 3 packages in 1.2s",
  12. ];
  13. const Example = () => {
  14. const [output, setOutput] = useState("");
  15. const [isStreaming, setIsStreaming] = useState(true);
  16. useEffect(() => {
  17. let lineIndex = 0;
  18. const interval = setInterval(() => {
  19. if (lineIndex < lines.length) {
  20. setOutput((prev) => prev + (prev ? "\n" : "") + lines[lineIndex]);
  21. lineIndex += 1;
  22. } else {
  23. setIsStreaming(false);
  24. clearInterval(interval);
  25. }
  26. }, 500);
  27. return () => clearInterval(interval);
  28. }, []);
  29. return <Terminal autoScroll isStreaming={isStreaming} output={output} />;
  30. };
  31. export default Example;