11import { ChevronLeft , ChevronRight } from '@gravity-ui/icons' ;
2- import { Button , Icon , Select } from '@gravity-ui/uikit' ;
3- import React , { ReactNode , Ref } from 'react' ;
2+ import { Button , Icon , Select , Tooltip } from '@gravity-ui/uikit' ;
3+ import React , { ReactNode , Ref , useMemo } from 'react' ;
44import { useDispatch , useSelector } from 'react-redux' ;
55
66import styles from './index.module.css' ;
77import { getCurrentNamedImage } from '@/static/new-ui/features/visual-checks/selectors' ;
8- import { getIconByStatus } from '@/static/new-ui/utils' ;
98import { changeTestRetry } from '@/static/modules/actions' ;
9+ import { getAssertViewStatusIcon } from '@/static/new-ui/utils/assert-view-status' ;
10+ import { getImages } from '@/static/new-ui/store/selectors' ;
1011
1112export function CompactAttemptPicker ( ) : ReactNode {
1213 const dispatch = useDispatch ( ) ;
1314 const currentImage = useSelector ( getCurrentNamedImage ) ;
15+ const images = useSelector ( getImages ) ;
1416 const currentBrowserId = currentImage ?. browserId ;
1517 const currentBrowser = useSelector ( state => currentBrowserId && state . tree . browsers . byId [ currentBrowserId ] ) ;
16- const resultsById = useSelector ( state => state . tree . results . byId ) ;
1718
1819 const totalAttemptsCount = currentBrowser ? currentBrowser . resultIds . length : null ;
1920 const currentAttemptIndex = useSelector ( state => currentBrowser ? state . tree . browsers . stateById [ currentBrowser . id ] . retryIndex : null ) ;
@@ -24,15 +25,47 @@ export function CompactAttemptPicker(): ReactNode {
2425 }
2526 } ;
2627
27- const onPreviousClick = ( ) : void => {
28- if ( currentBrowserId && currentAttemptIndex !== null && currentAttemptIndex > 0 ) {
29- dispatch ( changeTestRetry ( { browserId : currentBrowserId , retryIndex : currentAttemptIndex - 1 } ) ) ;
28+ const isDisabled = ( next : boolean ) : boolean => {
29+ if ( currentBrowser && currentAttemptIndex !== null ) {
30+ let i = next ? currentAttemptIndex + 1 : currentAttemptIndex - 1 ;
31+
32+ while ( next ? ( i < currentBrowser ?. resultIds . length ) : ( i >= 0 ) ) {
33+ const imageId = `${ currentBrowser ?. resultIds [ i ] } ${ currentImage ?. stateName } ` ;
34+
35+ if ( images [ imageId ] ) {
36+ return false ;
37+ }
38+
39+ i += next ? 1 : - 1 ;
40+ }
3041 }
42+
43+ return true ;
3144 } ;
3245
33- const onNextClick = ( ) : void => {
34- if ( currentBrowserId && currentAttemptIndex !== null && totalAttemptsCount !== null && currentAttemptIndex < totalAttemptsCount - 1 ) {
35- dispatch ( changeTestRetry ( { browserId : currentBrowserId , retryIndex : currentAttemptIndex + 1 } ) ) ;
46+ const nextDisabled = useMemo ( ( ) => isDisabled ( true ) , [ currentAttemptIndex , currentBrowser ] ) ;
47+ const prevDisabled = useMemo ( ( ) => isDisabled ( false ) , [ currentAttemptIndex , currentBrowser ] ) ;
48+
49+ const onNextPrev = ( next : boolean ) : void => {
50+ if ( currentBrowser && currentAttemptIndex !== null ) {
51+ let nextIndex = null ;
52+
53+ let i = next ? currentAttemptIndex + 1 : currentAttemptIndex - 1 ;
54+
55+ while ( next ? ( i < currentBrowser ?. resultIds . length ) : ( i >= 0 ) ) {
56+ const imageId = `${ currentBrowser ?. resultIds [ i ] } ${ currentImage ?. stateName } ` ;
57+
58+ if ( images [ imageId ] ) {
59+ nextIndex = i ;
60+ break ;
61+ }
62+
63+ i += next ? 1 : - 1 ;
64+ }
65+
66+ if ( currentBrowserId && nextIndex !== null ) {
67+ dispatch ( changeTestRetry ( { browserId : currentBrowserId , retryIndex : nextIndex } ) ) ;
68+ }
3669 }
3770 } ;
3871
@@ -42,30 +75,65 @@ export function CompactAttemptPicker(): ReactNode {
4275
4376 return (
4477 < div className = { styles . container } >
45- < Button view = { 'outlined' } onClick = { onPreviousClick } disabled = { currentAttemptIndex === 0 } > < Icon data = { ChevronLeft } /> </ Button >
46- < Select
47- renderControl = { ( { triggerProps : { onClick, onKeyDown} , ref} ) : React . JSX . Element => (
48- < Button className = { styles . attemptSelect } onClick = { onClick } onKeyDown = { onKeyDown } ref = { ref as Ref < HTMLButtonElement > } view = { 'flat' } >
49- Attempt < span className = { styles . attemptNumber } >
50- { currentAttemptIndex !== null ? currentAttemptIndex + 1 : '–' }
51- </ span > of < span className = { styles . attemptNumber } > { totalAttemptsCount ?? '–' } </ span >
52- </ Button >
53- ) }
54- renderOption = { ( option ) : React . JSX . Element => (
55- < div className = { styles . attemptOption } >
56- { getIconByStatus ( resultsById [ option . data . resultId ] . status ) }
57- < span > { option . content } </ span >
58- </ div >
59- ) } popupClassName = { styles . attemptSelectPopup }
60- onUpdate = { onUpdate }
78+ < Tooltip
79+ content = "Prev screenshot"
80+ openDelay = { 0 }
81+ placement = "top"
82+ >
83+ < Button
84+ aria-label = "Prev screenshot"
85+ view = "outlined"
86+ onClick = { ( ) : void => onNextPrev ( false ) }
87+ disabled = { prevDisabled }
88+ >
89+ < Icon data = { ChevronLeft } />
90+ </ Button >
91+ </ Tooltip >
92+ < Tooltip
93+ content = "Show all attempts"
94+ openDelay = { 0 }
95+ placement = "top"
96+ >
97+ < Select
98+ renderControl = { ( { triggerProps : { onClick, onKeyDown} , ref} ) : React . JSX . Element => (
99+ < Button className = { styles . attemptSelect } onClick = { onClick } onKeyDown = { onKeyDown } ref = { ref as Ref < HTMLButtonElement > } view = { 'flat' } >
100+ Attempt < span className = { styles . attemptNumber } >
101+ { currentAttemptIndex !== null ? currentAttemptIndex + 1 : '–' }
102+ </ span > of < span className = { styles . attemptNumber } > { totalAttemptsCount ?? '–' } </ span >
103+ </ Button >
104+ ) }
105+ renderOption = { ( option ) : React . JSX . Element => {
106+ const imageId = `${ option . data . resultId } ${ currentImage ?. stateName } ` ;
107+
108+ return (
109+ < div className = { styles . attemptOption } >
110+ { getAssertViewStatusIcon ( images [ imageId ] || null , true ) }
111+ < span > { option . content } </ span >
112+ </ div >
113+ ) ;
114+ } }
115+ popupClassName = { styles . attemptSelectPopup }
116+ onUpdate = { onUpdate }
117+ value = { currentAttemptIndex ? [ currentAttemptIndex . toString ( ) ] : [ ] }
118+ >
119+ { currentBrowser . resultIds . map ( ( resultId , index ) => (
120+ < Select . Option key = { index } value = { index . toString ( ) } content = { `Attempt #${ index + 1 } ` } data = { { resultId} } > </ Select . Option >
121+ ) ) }
122+ </ Select >
123+ </ Tooltip >
124+ < Tooltip
125+ content = "Next screenshot"
126+ openDelay = { 0 }
127+ placement = "top"
61128 >
62- { currentBrowser . resultIds . map ( ( resultId , index ) => (
63- < Select . Option key = { index } value = { index . toString ( ) } content = { `Attempt #${ index + 1 } ` } data = { { resultId} } > </ Select . Option >
64- ) ) }
65- </ Select >
66- < Button view = { 'outlined' } onClick = { onNextClick } disabled = { totalAttemptsCount === null || currentAttemptIndex === totalAttemptsCount - 1 } >
67- < Icon data = { ChevronRight } />
68- </ Button >
129+ < Button
130+ view = "outlined"
131+ onClick = { ( ) : void => onNextPrev ( true ) }
132+ disabled = { nextDisabled }
133+ >
134+ < Icon data = { ChevronRight } />
135+ </ Button >
136+ </ Tooltip >
69137 </ div >
70138 ) ;
71139}
0 commit comments