11'use strict' ;
22
3- import React , { Component , Fragment } from 'react' ;
3+ import React , { Fragment , useEffect , useState } from 'react' ;
44import { connect } from 'react-redux' ;
55import PropTypes from 'prop-types' ;
66import classNames from 'classnames' ;
@@ -9,68 +9,102 @@ import SwipeDiff from './swipe-diff';
99import SwitchDiff from './switch-diff' ;
1010import OnionSkinDiff from './onion-skin-diff' ;
1111import diffModes from '../../../../constants/diff-modes' ;
12+ import { types } from '../../modals' ;
13+ import useFitImages from './useFitImages' ;
1214
1315import './index.styl' ;
1416
15- class StateFail extends Component {
16- static propTypes = {
17- image : PropTypes . shape ( {
18- expectedImg : PropTypes . object . isRequired ,
19- actualImg : PropTypes . object . isRequired ,
20- diffImg : PropTypes . object . isRequired ,
21- diffClusters : PropTypes . array
22- } ) . isRequired ,
23- // from store
24- diffMode : PropTypes . string
25- }
26-
27- constructor ( props ) {
28- super ( props ) ;
29-
30- this . state = { diffMode : this . props . diffMode } ;
31- }
17+ const StateFail = ( { image, diffMode : diffModeProp , isScreenshotAccepterOpened} ) => {
18+ const [ diffMode , setDiffMode ] = useState ( diffModeProp ) ;
19+ const [ fitWidths , { expectedRef, actualRef} ] = useFitImages ( image , isScreenshotAccepterOpened ) ;
3220
33- componentWillReceiveProps ( nextProps ) {
34- if ( this . state . diffMode !== nextProps . diffMode ) {
35- this . setState ( { diffMode : nextProps . diffMode } ) ;
36- }
37- }
21+ useEffect ( ( ) => {
22+ setDiffMode ( diffModeProp ) ;
23+ } , [ diffModeProp , setDiffMode ] ) ;
3824
39- _handleDiffModeClick = ( diffMode ) => {
40- if ( this . state . diffMode === diffMode ) {
41- return ;
42- }
25+ const renderDiffModeItem = ( mode ) => {
26+ const className = classNames (
27+ 'diff-modes__item' ,
28+ { 'diff-modes__active' : diffMode === mode . id }
29+ ) ;
4330
44- this . setState ( { diffMode} ) ;
45- }
31+ return (
32+ < li
33+ key = { mode . id }
34+ title = { mode . description }
35+ className = { className }
36+ onClick = { ( ) => setDiffMode ( mode . id ) }
37+ >
38+ { mode . title }
39+ </ li >
40+ ) ;
41+ } ;
4642
47- _renderDiffModeItems ( ) {
48- const diffModeItems = Object . values ( diffModes ) . map ( ( diffMode ) => {
49- return this . _renderDiffModeItem ( diffMode ) ;
50- } ) ;
43+ const renderDiffModeItems = ( ) => {
44+ const diffModeItems = Object . values ( diffModes ) . map ( renderDiffModeItem ) ;
5145
5246 return (
5347 < ul className = "diff-modes" >
5448 { diffModeItems }
5549 </ ul >
5650 ) ;
57- }
51+ } ;
5852
59- _renderDiffModeItem ( diffMode ) {
60- const className = classNames (
61- 'diff-modes__item' ,
62- { 'diff-modes__active' : this . state . diffMode === diffMode . id }
53+ const getLabelKey = ( ) => {
54+ const images = [ image . expectedImg , image . actualImg ] ;
55+ const sizes = images . map ( image => `${ image . size . width } ${ image . size . height } ` ) ;
56+ const key = sizes . join ( '' ) ;
57+
58+ return key ;
59+ } ;
60+
61+ const drawImageBox = ( image , { label, diffClusters, width, ref} = { } ) => {
62+ const titleText = `${ label } (${ image . size . width } x${ image . size . height } )` ;
63+ const titleKey = getLabelKey ( ) ;
64+
65+ return (
66+ < div className = "image-box__image" style = { { flex : image . size . width } } >
67+ { label && < div key = { titleKey } ref = { ref } className = "image-box__title" > { titleText } </ div > }
68+ < ResizedScreenshot
69+ image = { image }
70+ diffClusters = { diffClusters }
71+ overrideWidth = { width }
72+ />
73+ </ div >
6374 ) ;
75+ } ;
6476
65- return < li key = { diffMode . id } title = { diffMode . description } className = { className } onClick = { ( ) => this . _handleDiffModeClick ( diffMode . id ) } > { diffMode . title } </ li > ;
66- }
77+ const renderOnlyDiff = ( ) => {
78+ const { diffImg, diffClusters} = image ;
79+
80+ return drawImageBox ( diffImg , { diffClusters} ) ;
81+ } ;
82+
83+ const drawExpectedAndActual = ( { expectedImg, expectedWidth} , { actualImg, actualWidth} ) => {
84+ return (
85+ < Fragment >
86+ { drawImageBox ( expectedImg , { label : 'Expected' , width : expectedWidth , ref : expectedRef } ) }
87+ { drawImageBox ( actualImg , { label : 'Actual' , width : actualWidth , ref : actualRef } ) }
88+ </ Fragment >
89+ ) ;
90+ } ;
91+
92+ const renderThreeImages = ( fitWidths = [ ] ) => {
93+ const { expectedImg, actualImg, diffImg, diffClusters} = image ;
94+ const [ expectedWidth , actualWidth , diffWidth ] = fitWidths ;
95+
96+ return < Fragment >
97+ { drawExpectedAndActual ( { expectedImg, expectedWidth} , { actualImg, actualWidth} ) }
98+ { drawImageBox ( diffImg , { label : 'Diff' , diffClusters, width : diffWidth } ) }
99+ </ Fragment > ;
100+ } ;
67101
68- _renderImages ( ) {
69- const { image : { expectedImg, actualImg} } = this . props ;
102+ const renderImages = ( ) => {
103+ const { expectedImg, actualImg} = image ;
70104
71- switch ( this . state . diffMode ) {
105+ switch ( diffMode ) {
72106 case diffModes . ONLY_DIFF . id :
73- return this . _renderOnlyDiff ( ) ;
107+ return renderOnlyDiff ( ) ;
74108
75109 case diffModes . SWITCH . id :
76110 return < SwitchDiff image1 = { expectedImg } image2 = { actualImg } /> ;
@@ -83,60 +117,45 @@ class StateFail extends Component {
83117
84118 case diffModes . THREE_UP_SCALED . id :
85119 return < div className = "image-box__scaled" >
86- { this . _renderThreeImages ( ) }
120+ { renderThreeImages ( ) }
121+ </ div > ;
122+
123+ case diffModes . THREE_UP_SCALED_TO_FIT . id :
124+ return < div className = "image-box__scaled" >
125+ { renderThreeImages ( fitWidths ) }
87126 </ div > ;
88127
89128 case diffModes . THREE_UP . id :
90129 default :
91- return this . _renderThreeImages ( ) ;
130+ return renderThreeImages ( ) ;
92131 }
93- }
94-
95- _renderOnlyDiff ( ) {
96- const { image : { diffImg, diffClusters} } = this . props ;
97-
98- return this . _drawImageBox ( diffImg , { diffClusters} ) ;
99- }
100-
101- _renderThreeImages ( ) {
102- const { image : { expectedImg, actualImg, diffImg, diffClusters} } = this . props ;
103-
104- return < Fragment >
105- { this . _drawExpectedAndActual ( expectedImg , actualImg ) }
106- { this . _drawImageBox ( diffImg , { label : 'Diff' , diffClusters} ) }
107- </ Fragment > ;
108- }
109-
110- render ( ) {
111- return (
112- < Fragment >
113- { this . _renderDiffModeItems ( ) }
114- { this . _renderImages ( ) }
115- </ Fragment >
116- ) ;
117- }
118-
119- _drawExpectedAndActual ( expectedImg , actualImg ) {
120- return (
121- < Fragment >
122- { this . _drawImageBox ( expectedImg , { label : 'Expected' } ) }
123- { this . _drawImageBox ( actualImg , { label : 'Actual' } ) }
124- </ Fragment >
125- ) ;
126- }
132+ } ;
133+
134+ return (
135+ < Fragment >
136+ { renderDiffModeItems ( ) }
137+ { renderImages ( ) }
138+ </ Fragment >
139+ ) ;
140+ } ;
141+
142+ StateFail . propTypes = {
143+ image : PropTypes . shape ( {
144+ expectedImg : PropTypes . object . isRequired ,
145+ actualImg : PropTypes . object . isRequired ,
146+ diffImg : PropTypes . object . isRequired ,
147+ diffClusters : PropTypes . array
148+ } ) . isRequired ,
149+ // from store
150+ diffMode : PropTypes . string ,
151+ isScreenshotAccepterOpened : PropTypes . bool . isRequired
152+ } ;
127153
128- _drawImageBox ( image , { label, diffClusters} = { } ) {
129- const text = `${ label } (${ image . size . width } x${ image . size . height } )` ;
154+ export default connect (
155+ ( { modals, view} ) => {
156+ const diffMode = view . diffMode ;
157+ const isScreenshotAccepterOpened = modals . some ( modal => modal . id === types . SCREENSHOT_ACCEPTER ) ;
130158
131- return (
132- < div className = "image-box__image" style = { { flex : image . size . width } } >
133- { label && < div className = "image-box__title" > { text } </ div > }
134- < ResizedScreenshot image = { image } diffClusters = { diffClusters } />
135- </ div >
136- ) ;
159+ return { diffMode, isScreenshotAccepterOpened} ;
137160 }
138- }
139-
140- export default connect (
141- ( { view : { diffMode} } ) => ( { diffMode} )
142161) ( StateFail ) ;
0 commit comments