|
| 1 | +import {useEffect, useRef, useState} from 'react'; |
| 2 | +import {keyframes} from '@emotion/react'; |
| 3 | +import styled from '@emotion/styled'; |
| 4 | +import {AnimatePresence, motion} from 'framer-motion'; |
| 5 | + |
| 6 | +import {IconArrow} from 'sentry/icons'; |
| 7 | +import {space} from 'sentry/styles/space'; |
| 8 | +import testableTransition from 'sentry/utils/testableTransition'; |
| 9 | + |
| 10 | +interface Props { |
| 11 | + stream: string; |
| 12 | +} |
| 13 | + |
| 14 | +const shimmer = keyframes` |
| 15 | + 0% { |
| 16 | + background-position: -1000px 0; |
| 17 | + } |
| 18 | + 100% { |
| 19 | + background-position: 1000px 0; |
| 20 | + } |
| 21 | +`; |
| 22 | + |
| 23 | +export function AutofixOutputStream({stream}: Props) { |
| 24 | + const [displayedText, setDisplayedText] = useState(''); |
| 25 | + const previousText = useRef(''); |
| 26 | + const currentIndexRef = useRef(0); |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + const newText = stream; |
| 30 | + |
| 31 | + // Reset animation if the new text is completely different |
| 32 | + if (!newText.startsWith(displayedText)) { |
| 33 | + previousText.current = newText; |
| 34 | + currentIndexRef.current = 0; |
| 35 | + setDisplayedText(''); |
| 36 | + } |
| 37 | + |
| 38 | + const interval = window.setInterval(() => { |
| 39 | + if (currentIndexRef.current < newText.length) { |
| 40 | + setDisplayedText(newText.slice(0, currentIndexRef.current + 1)); |
| 41 | + currentIndexRef.current++; |
| 42 | + } else { |
| 43 | + window.clearInterval(interval); |
| 44 | + } |
| 45 | + }, 15); |
| 46 | + |
| 47 | + return () => { |
| 48 | + window.clearInterval(interval); |
| 49 | + }; |
| 50 | + }, [displayedText, stream]); |
| 51 | + |
| 52 | + return ( |
| 53 | + <AnimatePresence mode="wait"> |
| 54 | + <Wrapper |
| 55 | + key="output-stream" |
| 56 | + initial={{opacity: 0, height: 0, scale: 0.8}} |
| 57 | + animate={{opacity: 1, height: 'auto', scale: 1}} |
| 58 | + exit={{opacity: 0, height: 0, scale: 0.8, y: -20}} |
| 59 | + transition={testableTransition({ |
| 60 | + duration: 1.0, |
| 61 | + height: { |
| 62 | + type: 'spring', |
| 63 | + bounce: 0.2, |
| 64 | + }, |
| 65 | + scale: { |
| 66 | + type: 'spring', |
| 67 | + bounce: 0.2, |
| 68 | + }, |
| 69 | + y: { |
| 70 | + type: 'tween', |
| 71 | + ease: 'easeOut', |
| 72 | + }, |
| 73 | + })} |
| 74 | + style={{ |
| 75 | + transformOrigin: 'top center', |
| 76 | + }} |
| 77 | + > |
| 78 | + <StyledArrow direction="down" size="sm" /> |
| 79 | + <StreamContainer layout> |
| 80 | + <StreamContent>{displayedText}</StreamContent> |
| 81 | + </StreamContainer> |
| 82 | + </Wrapper> |
| 83 | + </AnimatePresence> |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +const Wrapper = styled(motion.div)` |
| 88 | + display: flex; |
| 89 | + flex-direction: column; |
| 90 | + align-items: center; |
| 91 | + margin: ${space(1)} ${space(4)}; |
| 92 | + gap: ${space(1)}; |
| 93 | + overflow: hidden; |
| 94 | +`; |
| 95 | + |
| 96 | +const StreamContainer = styled(motion.div)` |
| 97 | + position: relative; |
| 98 | + width: 100%; |
| 99 | + border-radius: ${p => p.theme.borderRadius}; |
| 100 | + background: ${p => p.theme.background}; |
| 101 | + border: 1px dashed ${p => p.theme.border}; |
| 102 | + height: 5rem; |
| 103 | + overflow: hidden; |
| 104 | +
|
| 105 | + &:before { |
| 106 | + content: ''; |
| 107 | + position: absolute; |
| 108 | + inset: 0; |
| 109 | + background: linear-gradient( |
| 110 | + 90deg, |
| 111 | + transparent, |
| 112 | + ${p => p.theme.active}20, |
| 113 | + transparent |
| 114 | + ); |
| 115 | + background-size: 2000px 100%; |
| 116 | + animation: ${shimmer} 2s infinite linear; |
| 117 | + pointer-events: none; |
| 118 | + } |
| 119 | +`; |
| 120 | + |
| 121 | +const StreamContent = styled('div')` |
| 122 | + margin: 0; |
| 123 | + padding: ${space(2)}; |
| 124 | + white-space: pre-wrap; |
| 125 | + word-break: break-word; |
| 126 | + font-size: ${p => p.theme.fontSizeSmall}; |
| 127 | + color: ${p => p.theme.subText}; |
| 128 | + height: 5rem; |
| 129 | + overflow-y: auto; |
| 130 | + display: flex; |
| 131 | + flex-direction: column-reverse; |
| 132 | +`; |
| 133 | + |
| 134 | +const StyledArrow = styled(IconArrow)` |
| 135 | + color: ${p => p.theme.subText}; |
| 136 | + opacity: 0.5; |
| 137 | +`; |
0 commit comments