@@ -3,12 +3,11 @@ import React from 'react';
33import { Button } from '@gravity-ui/uikit' ;
44
55import type { MultipartChunk } from '../../store/reducers/multipart/multipart' ;
6- import { useStreamMultipartQuery } from '../../store/reducers/multipart/multipart' ;
6+ import { useLazyStreamMultipartQuery } from '../../store/reducers/multipart/multipart' ;
77
88const ANIMATION_DURATION = 300 ;
99
1010export function MultipartTest ( ) {
11- const [ startStream , setStartStream ] = React . useState ( false ) ;
1211 const [ receivedChunks , setReceivedChunks ] = React . useState < MultipartChunk [ ] > ( [ ] ) ;
1312
1413 const handleChunk = React . useCallback ( ( chunk : MultipartChunk ) => {
@@ -22,29 +21,32 @@ export function MultipartTest() {
2221 } ) ;
2322 } , [ ] ) ;
2423
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- ) ;
24+ const [ trigger , { error, isLoading} ] = useLazyStreamMultipartQuery ( ) ;
25+ const requestRef = React . useRef < ReturnType < typeof trigger > | null > ( null ) ;
3626
3727 const handleStart = React . useCallback ( ( ) => {
3828 console . log ( 'Starting stream...' ) ;
3929 setReceivedChunks ( [ ] ) ;
40- setStartStream ( true ) ;
41- } , [ ] ) ;
30+ requestRef . current = trigger ( {
31+ url : '/viewer/json/query' ,
32+ onChunk : handleChunk ,
33+ } ) ;
34+ } , [ trigger , handleChunk ] ) ;
4235
4336 const handleStop = React . useCallback ( ( ) => {
4437 console . log ( 'Stopping stream...' ) ;
45- setStartStream ( false ) ;
38+ requestRef . current ?. abort ( ) ;
39+ requestRef . current = null ;
4640 } , [ ] ) ;
4741
42+ const handleButtonClick = React . useCallback ( ( ) => {
43+ if ( isLoading ) {
44+ handleStop ( ) ;
45+ } else {
46+ handleStart ( ) ;
47+ }
48+ } , [ isLoading , handleStart , handleStop ] ) ;
49+
4850 const getErrorMessage = ( err : unknown ) : string => {
4951 if ( err instanceof Error ) {
5052 return err . message ;
@@ -72,13 +74,8 @@ export function MultipartTest() {
7274 </ h2 >
7375
7476 < 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' }
77+ < Button view = "action" size = "l" onClick = { handleButtonClick } loading = { isLoading } >
78+ { isLoading ? 'Stop Streaming' : 'Start Streaming' }
8279 </ Button >
8380 { receivedChunks . length > 0 && (
8481 < span
0 commit comments