|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import {Button} from '@gravity-ui/uikit'; |
| 4 | + |
| 5 | +import type {MultipartChunk} from '../../store/reducers/multipart/multipart'; |
| 6 | +import {useStreamMultipartQuery} from '../../store/reducers/multipart/multipart'; |
| 7 | + |
| 8 | +const ANIMATION_DURATION = 300; |
| 9 | + |
| 10 | +export function MultipartTest() { |
| 11 | + const [startStream, setStartStream] = React.useState(false); |
| 12 | + const [receivedChunks, setReceivedChunks] = React.useState<MultipartChunk[]>([]); |
| 13 | + |
| 14 | + const handleChunk = React.useCallback((chunk: MultipartChunk) => { |
| 15 | + console.log('handleChunk called in component:', chunk); |
| 16 | + console.log(chunk); |
| 17 | + setReceivedChunks((prevChunks: MultipartChunk[]) => { |
| 18 | + // Create a new array with the new chunk |
| 19 | + const newChunks = [chunk, ...prevChunks]; |
| 20 | + // Sort by part_number in descending order to show newest first |
| 21 | + return newChunks; |
| 22 | + }); |
| 23 | + }, []); |
| 24 | + |
| 25 | + console.log('Component rendered, startStream:', startStream); |
| 26 | + |
| 27 | + const {error, isLoading} = useStreamMultipartQuery( |
| 28 | + { |
| 29 | + url: '/viewer/json/query', |
| 30 | + onChunk: handleChunk, |
| 31 | + }, |
| 32 | + { |
| 33 | + skip: !startStream, |
| 34 | + }, |
| 35 | + ); |
| 36 | + |
| 37 | + const handleStart = React.useCallback(() => { |
| 38 | + console.log('Starting stream...'); |
| 39 | + setReceivedChunks([]); |
| 40 | + setStartStream(true); |
| 41 | + }, []); |
| 42 | + |
| 43 | + const handleStop = React.useCallback(() => { |
| 44 | + console.log('Stopping stream...'); |
| 45 | + setStartStream(false); |
| 46 | + }, []); |
| 47 | + |
| 48 | + const getErrorMessage = (err: unknown): string => { |
| 49 | + if (err instanceof Error) { |
| 50 | + return err.message; |
| 51 | + } |
| 52 | + if (typeof err === 'object') { |
| 53 | + return JSON.stringify(err); |
| 54 | + } |
| 55 | + return String(err); |
| 56 | + }; |
| 57 | + |
| 58 | + return ( |
| 59 | + <div |
| 60 | + className="multipart-test" |
| 61 | + style={{padding: '32px', maxWidth: '800px', margin: '0 auto'}} |
| 62 | + > |
| 63 | + <h2 |
| 64 | + style={{ |
| 65 | + fontSize: '24px', |
| 66 | + fontWeight: 600, |
| 67 | + marginBottom: '24px', |
| 68 | + color: 'var(--g-color-text-primary)', |
| 69 | + }} |
| 70 | + > |
| 71 | + Multipart Streaming Test |
| 72 | + </h2> |
| 73 | + |
| 74 | + <div style={{marginBottom: '24px', display: 'flex', gap: '12px', alignItems: 'center'}}> |
| 75 | + <Button |
| 76 | + view="action" |
| 77 | + size="l" |
| 78 | + onClick={startStream ? handleStop : handleStart} |
| 79 | + loading={isLoading} |
| 80 | + > |
| 81 | + {startStream ? 'Stop Streaming' : 'Start Streaming'} |
| 82 | + </Button> |
| 83 | + {receivedChunks.length > 0 && ( |
| 84 | + <span |
| 85 | + style={{ |
| 86 | + color: 'var(--g-color-text-secondary)', |
| 87 | + fontSize: '14px', |
| 88 | + }} |
| 89 | + > |
| 90 | + Received: {receivedChunks.length} chunks |
| 91 | + </span> |
| 92 | + )} |
| 93 | + </div> |
| 94 | + |
| 95 | + {Boolean(error) && ( |
| 96 | + <div |
| 97 | + style={{ |
| 98 | + color: 'var(--g-color-text-danger)', |
| 99 | + marginBottom: '24px', |
| 100 | + padding: '12px', |
| 101 | + backgroundColor: 'var(--g-color-danger-light)', |
| 102 | + borderRadius: '6px', |
| 103 | + fontSize: '14px', |
| 104 | + }} |
| 105 | + > |
| 106 | + {`Error: ${getErrorMessage(error)}`} |
| 107 | + </div> |
| 108 | + )} |
| 109 | + |
| 110 | + <div |
| 111 | + style={{ |
| 112 | + display: 'flex', |
| 113 | + flexDirection: 'column', |
| 114 | + gap: '12px', |
| 115 | + }} |
| 116 | + > |
| 117 | + {receivedChunks.map((chunk: MultipartChunk) => { |
| 118 | + const content = chunk.content || {}; |
| 119 | + const {color = '#e5e5e5', message = '', id} = content; |
| 120 | + |
| 121 | + return ( |
| 122 | + <div |
| 123 | + key={chunk.part_number} |
| 124 | + style={{ |
| 125 | + padding: '16px', |
| 126 | + borderRadius: '8px', |
| 127 | + backgroundColor: color, |
| 128 | + color: isLightColor(color) ? '#000' : '#fff', |
| 129 | + boxShadow: '0 2px 4px rgba(0,0,0,0.1)', |
| 130 | + animation: `fadeIn ${ANIMATION_DURATION}ms ease-out`, |
| 131 | + position: 'relative', |
| 132 | + overflow: 'hidden', |
| 133 | + }} |
| 134 | + > |
| 135 | + <div |
| 136 | + style={{ |
| 137 | + display: 'flex', |
| 138 | + justifyContent: 'space-between', |
| 139 | + marginBottom: '8px', |
| 140 | + fontSize: '12px', |
| 141 | + opacity: 0.8, |
| 142 | + }} |
| 143 | + > |
| 144 | + <span>ID: {id}</span> |
| 145 | + <span> |
| 146 | + Part {chunk.part_number} of {chunk.total_parts} |
| 147 | + </span> |
| 148 | + </div> |
| 149 | + <div |
| 150 | + style={{ |
| 151 | + fontSize: '16px', |
| 152 | + lineHeight: '1.5', |
| 153 | + wordBreak: 'break-word', |
| 154 | + }} |
| 155 | + > |
| 156 | + {message} |
| 157 | + </div> |
| 158 | + {chunk.result && ( |
| 159 | + <div |
| 160 | + style={{ |
| 161 | + marginTop: '8px', |
| 162 | + fontSize: '14px', |
| 163 | + opacity: 0.8, |
| 164 | + }} |
| 165 | + > |
| 166 | + Result: {chunk.result} |
| 167 | + </div> |
| 168 | + )} |
| 169 | + </div> |
| 170 | + ); |
| 171 | + })} |
| 172 | + </div> |
| 173 | + |
| 174 | + <style>{` |
| 175 | + @keyframes fadeIn { |
| 176 | + from { |
| 177 | + opacity: 0; |
| 178 | + transform: translateY(10px); |
| 179 | + } |
| 180 | + to { |
| 181 | + opacity: 1; |
| 182 | + transform: translateY(0); |
| 183 | + } |
| 184 | + } |
| 185 | + `}</style> |
| 186 | + </div> |
| 187 | + ); |
| 188 | +} |
| 189 | + |
| 190 | +// Helper function to determine if a color is light or dark |
| 191 | +function isLightColor(color: string) { |
| 192 | + const hex = color.replace('#', ''); |
| 193 | + const r = parseInt(hex.substr(0, 2), 16); |
| 194 | + const g = parseInt(hex.substr(2, 2), 16); |
| 195 | + const b = parseInt(hex.substr(4, 2), 16); |
| 196 | + const brightness = (r * 299 + g * 587 + b * 114) / 1000; |
| 197 | + return brightness > 128; |
| 198 | +} |
0 commit comments