Skip to content

Commit 8cf38e1

Browse files
sonic16xrocketraccoon
andauthored
feat(new-ui): show screenshots from retry with errors
* feat(new-ui): show screenshots from retry with errors --------- Co-authored-by: rocketraccoon <rocketraccoon@yandex-team.ru>
1 parent 1103aba commit 8cf38e1

6 files changed

Lines changed: 196 additions & 65 deletions

File tree

Lines changed: 101 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import {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';
44
import {useDispatch, useSelector} from 'react-redux';
55

66
import styles from './index.module.css';
77
import {getCurrentNamedImage} from '@/static/new-ui/features/visual-checks/selectors';
8-
import {getIconByStatus} from '@/static/new-ui/utils';
98
import {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

1112
export 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
}

‎lib/static/new-ui/features/suites/components/SuitesPage/index.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export function SuitesPage(): ReactNode {
4040
const {visibleTreeNodeIds, tree} = treeData;
4141
const params = useParams();
4242
const attempt = useSelector(getAttempt);
43+
const currentBrowser = useSelector(state => state.app[Page.suitesPage].currentBrowserId);
4344

4445
const currentTreeNodeId = useSelector(state => state.app[Page.suitesPage].currentTreeNodeId);
4546
const currentIndex = visibleTreeNodeIds.indexOf(currentTreeNodeId as string);
@@ -69,6 +70,10 @@ export function SuitesPage(): ReactNode {
6970
}, [currentResult, attempt]);
7071

7172
useEffect(() => {
73+
if (currentBrowser === params.suiteId) {
74+
return;
75+
}
76+
7277
dispatch(setStrictMatchFilter(false));
7378

7479
if (isInitialized && params.suiteId) {

‎lib/static/new-ui/features/visual-checks/components/VisualChecksPage/VisualChecksStickyHeader.tsx‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {useDispatch, useSelector} from 'react-redux';
55

66
import {
77
getAttempt,
8+
getCurrentBrowser,
89
getCurrentImage,
910
getImagesByNamedImageIds,
1011
NamedImageEntity
@@ -15,8 +16,8 @@ import {CompactAttemptPicker} from '@/static/new-ui/components/CompactAttemptPic
1516
import {DiffModeId, DiffModes, EditScreensFeature} from '@/constants';
1617
import {
1718
setDiffMode,
18-
staticAccepterStageScreenshot, staticAccepterUnstageScreenshot,
19-
visualChecksPageSetCurrentNamedImage
19+
staticAccepterStageScreenshot,
20+
staticAccepterUnstageScreenshot
2021
} from '@/static/modules/actions';
2122
import {isAcceptable, isScreenRevertable} from '@/static/modules/utils';
2223
import {AssertViewStatus} from '@/static/new-ui/components/AssertViewStatus';
@@ -58,16 +59,16 @@ const usePreloadImages = (
5859
}, []);
5960
};
6061

61-
export function VisualChecksStickyHeader({currentNamedImage, visibleNamedImageIds}: VisualChecksStickyHeaderProps): ReactNode {
62+
export function VisualChecksStickyHeader({currentNamedImage, visibleNamedImageIds, onImageChange}: VisualChecksStickyHeaderProps): ReactNode {
6263
const dispatch = useDispatch();
6364
const analytics = useAnalytics();
6465
const currentImage = useSelector(getCurrentImage);
6566
const attempt = useSelector(getAttempt);
6667
const navigate = useNavigate();
6768

6869
const currentNamedImageIndex = visibleNamedImageIds.indexOf(currentNamedImage?.id as string);
69-
const onPreviousImageHandler = (): void => void dispatch(visualChecksPageSetCurrentNamedImage(visibleNamedImageIds[currentNamedImageIndex - 1]));
70-
const onNextImageHandler = (): void => void dispatch(visualChecksPageSetCurrentNamedImage(visibleNamedImageIds[currentNamedImageIndex + 1]));
70+
const onPreviousImageHandler = (): void => onImageChange(visibleNamedImageIds[currentNamedImageIndex - 1]);
71+
const onNextImageHandler = (): void => onImageChange(visibleNamedImageIds[currentNamedImageIndex + 1]);
7172

7273
usePreloadImages(currentNamedImageIndex, visibleNamedImageIds);
7374

@@ -107,15 +108,7 @@ export function VisualChecksStickyHeader({currentNamedImage, visibleNamedImageId
107108
}
108109
};
109110

110-
const currentBrowser = useSelector(state => {
111-
const currentBrowserId = state.tree.results.byId[currentImage?.parentId ?? '']?.parentId;
112-
113-
if (currentBrowserId && state.tree.browsers.byId[currentBrowserId]) {
114-
return state.tree.browsers.byId[currentBrowserId];
115-
}
116-
117-
return null;
118-
});
111+
const currentBrowser = useSelector(getCurrentBrowser);
119112

120113
const currentResultId = currentImage?.parentId;
121114
const isLastResult = Boolean(currentResultId && currentBrowser && currentResultId === currentBrowser.resultIds[currentBrowser.resultIds.length - 1]);

‎lib/static/new-ui/features/visual-checks/components/VisualChecksPage/index.tsx‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import classNames from 'classnames';
2-
import React, {ReactNode, useCallback, useEffect, useMemo, useRef} from 'react';
2+
import React, {ReactNode, useCallback, useEffect, useMemo, useRef, useState} from 'react';
33
import {useDispatch, useSelector} from 'react-redux';
44

55
import {SplitViewLayout} from '@/static/new-ui/components/SplitViewLayout';
66
import {UiCard} from '@/static/new-ui/components/Card/UiCard';
7-
import {getAttempt, getCurrentImage, getCurrentNamedImage} from '@/static/new-ui/features/visual-checks/selectors';
7+
import {
8+
getAttempt,
9+
getLastAttempt,
10+
getCurrentNamedImage,
11+
getCurrentBrowser, getCurrentImage
12+
} from '@/static/new-ui/features/visual-checks/selectors';
813
import {AssertViewResult} from '@/static/new-ui/components/AssertViewResult';
914
import styles from './index.module.css';
1015
import {
@@ -32,8 +37,12 @@ export function VisualChecksPage(): ReactNode {
3237
const page = usePage();
3338

3439
const currentNamedImage = useSelector(getCurrentNamedImage);
35-
const currentImage = useSelector(getCurrentImage);
3640
const attempt = useSelector(getAttempt);
41+
const lastAttempt = useSelector(getLastAttempt);
42+
const currentImage = useSelector(getCurrentImage);
43+
const currentBrowser = useSelector(getCurrentBrowser);
44+
const [imageChanged, setImageChanged] = useState<boolean>(false);
45+
3746
const navigate = useNavigate();
3847
const params = useParams();
3948
const inited = useRef(false);
@@ -51,10 +60,20 @@ export function VisualChecksPage(): ReactNode {
5160
const isInitialized = useSelector(state => state.app.isInitialized);
5261
const onImageChange = useCallback((imageId: string) => {
5362
dispatch(visualChecksPageSetCurrentNamedImage(imageId));
54-
}, []);
63+
setImageChanged(true);
64+
}, [currentBrowser]);
65+
5566
const onTreeItemClick = useCallback((item: TreeViewItemData) => {
5667
dispatch(visualChecksPageSetCurrentNamedImage(item.id));
57-
}, []);
68+
setImageChanged(true);
69+
}, [currentBrowser, attempt]);
70+
71+
useEffect(() => {
72+
if (imageChanged && currentBrowser) {
73+
dispatch(changeTestRetry({browserId: currentBrowser?.id, retryIndex: lastAttempt}));
74+
setImageChanged(false);
75+
}
76+
}, [imageChanged, currentBrowser]);
5877

5978
useEffect(() => {
6079
if (currentTreeNodeId && attempt !== null) {

‎lib/static/new-ui/features/visual-checks/selectors.ts‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {createSelector} from 'reselect';
22

33
import {getBrowsers, getImages, getResults} from '@/static/new-ui/store/selectors';
44
import {TestStatus} from '@/constants';
5-
import {ImageEntity, State} from '@/static/new-ui/types/store';
5+
import {BrowserEntity, ImageEntity, State} from '@/static/new-ui/types/store';
66

77
/** @note NamedImageEntity describes visual assertion, not bound to specific attempt */
88
export interface NamedImageEntity {
@@ -60,7 +60,13 @@ export const getNamedImages = createSelector(
6060
const lastResultId = browser.resultIds[browser.resultIds.length - 1];
6161
const lastResult = results[lastResultId];
6262

63-
if (lastResult.status !== TestStatus.RUNNING && !lastResult.imageIds.find(imageId => images[imageId].stateName === group.stateName)) {
63+
// if last result 'ok' (not error and running) and doesn't have images we skip it
64+
// but if last result error (or running) we show it, because there are can be image
65+
if (
66+
lastResult.status !== TestStatus.RUNNING &&
67+
lastResult.status !== TestStatus.ERROR &&
68+
!lastResult.imageIds.find(imageId => images[imageId].stateName === group.stateName)
69+
) {
6470
continue;
6571
}
6672

@@ -140,3 +146,39 @@ export const getAttempt = (state: State): number | null => {
140146

141147
return null;
142148
};
149+
150+
export const getLastAttempt = (state: State): number => {
151+
const currentNamedImage = getCurrentNamedImage(state);
152+
const images = getImages(state);
153+
const currentBrowserId = currentNamedImage?.browserId;
154+
const currentBrowser = currentBrowserId && state.tree.browsers.byId[currentBrowserId];
155+
156+
if (currentBrowser) {
157+
for (let i = currentBrowser?.resultIds.length - 1; i >= 0; i--) {
158+
const imageId = `${currentBrowser?.resultIds[i]} ${currentNamedImage?.stateName}`;
159+
160+
if (images[imageId]) {
161+
return i;
162+
}
163+
}
164+
}
165+
166+
return 0;
167+
};
168+
169+
export const getCurrentBrowser = (state: State): BrowserEntity | null => {
170+
const currentNamedImageId = state.app.visualChecksPage.currentNamedImageId;
171+
const namedImages = getNamedImages(state);
172+
173+
if (currentNamedImageId) {
174+
const namedImage = namedImages[currentNamedImageId];
175+
176+
if (!namedImage) {
177+
return null;
178+
}
179+
180+
return state.tree.browsers.byId[namedImage.browserId];
181+
}
182+
183+
return null;
184+
};

0 commit comments

Comments
 (0)