import React, { useState, useEffect } from 'react'; import StreamingFadeInText from '../src/components/AnimatedText'; interface RandomTextSenderProps { initialText: string; windowSize: number; // Propagate this to SmoothText for consistency animation?: string; // Animation name sep?: string; // Token separator } const RandomTextSender: React.FC = ({ initialText, windowSize, animation, sep }) => { const [currentText, setCurrentText] = useState(''); const [remainingTokens, setRemainingTokens] = useState([]); const [baseLatency, setBaseLatency] = useState(100); const [tokenCount, setTokenCount] = useState(0); // Initialize the tokens useEffect(() => { setRemainingTokens(initialText.split(' ')); // Assuming space-separated tokens }, [initialText]); // Update base latency every 10 tokens useEffect(() => { if (tokenCount > 0 && tokenCount % 10 === 0) { const newBaseLatency = baseLatency + (Math.random() > 0.5 ? 20 : 0); // Randomly choose between 200ms and 800ms setBaseLatency(newBaseLatency); console.log(`Base latency updated to: ${newBaseLatency}ms`); } }, [tokenCount]); // Function to send a token at random intervals useEffect(() => { if (remainingTokens.length > 0) { // Jitter is up to 100ms more based on windowSize (unused) const jitter = Math.random() * 10; const networkDelay = baseLatency + jitter; const timeout = setTimeout(() => { const nextToken = remainingTokens[0]; setCurrentText(prev => prev ? `${prev} ${nextToken}` : nextToken); setRemainingTokens(prev => prev.slice(1)); setTokenCount(prev => prev + 1); // Increment token count }, networkDelay); return () => clearTimeout(timeout); } }, [remainingTokens, baseLatency, windowSize]); return (
{/* */} {/*
{currentText}
*/}
); }; // This is the default export that defines the component title and other configuration export default { title: 'Components/FadeIn', component: RandomTextSender, }; const text = `To be, or not to be, that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune, Or to take arms against a sea of troubles And by opposing end them. To die—to sleep, No more; and by a sleep to say we end The heart-ache and the thousand natural shocks That flesh is heir to: 'tis a consummation Devoutly to be wish'd. To die, to sleep; To sleep, perchance to dream—ay, there's the rub: For in that sleep of death what dreams may come, When we have shuffled off this mortal coil, Must give us pause—there's the respect That makes calamity of so long life. For who would bear the whips and scorns of time, Th'oppressor's wrong, the proud man's contumely, The pangs of dispriz'd love, the law's delay, The insolence of office, and the spurns That patient merit of th'unworthy takes, When he himself might his quietus make With a bare bodkin? Who would fardels bear, To grunt and sweat under a weary life, But that the dread of something after death, The undiscovere'd country, from whose bourn No traveller returns, puzzles the will, And makes us rather bear those ills we have Than fly to others that we know not of? Thus conscience doth make cowards of us all, And thus the native hue of resolution Is sicklied o'er with the pale cast of thought, And enterprises of great pith and moment With this regard their currents turn awry And lose the name of action. ` // Here we define a "story" for the default view of SmoothText export const Default = () => ; export const DefaultChar = () => ; // You can add more stories to showcase different props or states export const fadeIn = () => ; export const AllAtOnceFadeIn = () => ; export const blurIn = () => ; export const blurInChar = () => ; // export const typewriter = () => ; export const slideInFromLeft = () => ; export const slideInFromLeftChar = () => ; export const fadeAndScale = () => ; export const fadeAndScaleChar = () => ; export const colorTransition = () => ; export const rotateIn = () => ; export const rotateInChar = () => ; export const bounceIn = () => ; export const bounceInChar = () => ; export const elastic = () => ; export const elasticChar = () => ; export const highlight = () => ; export const highlightChar = () => ; export const blurAndSharpen = () => ; export const wave = () => ; export const waveChar = () => ; export const dropIn = () => ; export const dropInChar = () => ;