Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

WV-4037 Modernize Pick LineText OpacitySelect ErrorBoundary (#6719)

vicgonzalez2531d416d09bed
diff --git a/web/js/components/compare/pick.js b/web/js/components/compare/pick.js
index 1d14b88f3..04d6ecc03 100644
--- a/web/js/components/compare/pick.js
+++ b/web/js/components/compare/pick.js
@@ -1,77 +1,41 @@
-import React from 'react';
import PropTypes from 'prop-types';
import lodashRound from 'lodash/round';
-class Pick extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- position: props.position,
- max: props.max,
- };
- }
+const Pick = ({
+ color, height, max, path, position, text, width, yOffset,
+}) => {
+ const visibility = (position < 0 || position > max) ? 'hidden' : 'visible';
+ const translate = `translate(${position},${yOffset})`;
- /**
- * Return visibility style string
- */
- getVisibility() {
- const { position, max } = this.state;
- let visibility = 'visible';
- if (position < 0 || position > max) {
- visibility = 'hidden';
- }
- return visibility;
- }
-
- /**
- * Return text el to show over dragger
- * @param {number} x | x offset
- * @param {number} y | y offset
- */
- getText(x, y, visibility) {
- const { text } = this.props;
- const { textColor } = this.state;
- if (text) {
- return (
- <text
- x={x}
- y={y}
- alignmentBaseline="middle"
- textAnchor="middle"
- style={{
- fill: textColor || null,
- visibility: visibility || null,
- }}
- >
- {text}
- </text>
- );
- }
- return false;
- }
-
- render() {
- const visibility = this.getVisibility();
- const {
- yOffset, path, width, height, color,
- } = this.props;
- const { position } = this.state;
- const translate = `translate(${position},${yOffset})`;
+ const getText = (x, y) => {
+ if (!text) return false;
return (
- <g transform={translate}>
- <path
- style={{
- fill: color || null,
- visibility,
- }}
- d={path}
- transform={`translate(${-width / 2}, ${-height / 4})`}
- />
- {this.getText(-5, lodashRound(height / 6, 4), visibility)}
- </g>
+ <text
+ x={x}
+ y={y}
+ alignmentBaseline="middle"
+ textAnchor="middle"
+ style={{ visibility }}
+ >
+ {text}
+ </text>
);
- }
-}
+ };
+
+ return (
+ <g transform={translate}>
+ <path
+ style={{
+ fill: color || null,
+ visibility,
+ }}
+ d={path}
+ transform={`translate(${-width / 2}, ${-height / 4})`}
+ />
+ {getText(-5, lodashRound(height / 6, 4))}
+ </g>
+ );
+};
Pick.propTypes = {
color: PropTypes.string,
diff --git a/web/js/components/dateline/text.js b/web/js/components/dateline/text.js
index e39565c23..ddd890622 100644
--- a/web/js/components/dateline/text.js
+++ b/web/js/components/dateline/text.js
@@ -1,4 +1,4 @@
-import React from 'react';
+import { useRef, useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import OlOverlay from 'ol/Overlay';
import util from '../../util/util';
@@ -16,42 +16,33 @@ const textStyles = {
recRadius: 3,
};
-class LineText extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- overlay: '',
- };
- this.nodeRef = React.createRef();
- }
+const LineText = ({
+ active, date, x, isCompareActive, isLeft, map, textCoords,
+}) => {
+ const nodeRef = useRef(null);
+ const [overlay, setOverlay] = useState(null);
- componentDidMount() {
- const { x, map } = this.props;
- const overlay = new OlOverlay({
- element: this.nodeRef.current,
+ useEffect(() => {
+ const newOverlay = new OlOverlay({
+ element: nodeRef.current,
stopEvent: false,
});
- overlay.setPosition([x, 90]);
- map.addOverlay(overlay);
- this.setState({ overlay });
- }
+ newOverlay.setPosition([x, 90]);
+ map.addOverlay(newOverlay);
+ setOverlay(newOverlay);
+ }, []);
- componentDidUpdate() {
- const { overlay } = this.state;
- const { x, textCoords } = this.props;
- overlay.setPosition([x, textCoords[1]]);
- }
+ useEffect(() => {
+ if (overlay) {
+ overlay.setPosition([x, textCoords[1]]);
+ }
+ }, [overlay, x, textCoords]);
- getDateText() {
- const { date, isCompareActive, isLeft } = this.props;
+ const getDateText = () => {
if (isCompareActive) {
return {
- dateLeft: isLeft
- ? '+ 1 day'
- : '',
- dateRight: isLeft
- ? ''
- : '- 1 day',
+ dateLeft: isLeft ? '+ 1 day' : '',
+ dateRight: isLeft ? '' : '- 1 day',
};
}
const dateState = {
@@ -63,69 +54,66 @@ class LineText extends React.Component {
dateLeft: dateA,
dateRight: dateB,
};
- }
+ };
- render() {
- const { active, isCompareActive } = this.props;
- const {
- textWidth, recRadius, textY, width, textOpacity, textHeight, rectOpacity,
- } = textStyles;
- const { dateLeft, dateRight } = this.getDateText();
- const leftTextWidth = isCompareActive ? 60 : textWidth;
- const rightTextWidth = isCompareActive ? 60 : textWidth;
- const svgStyle = {
- position: 'absolute',
- transform: `translateX(${-(leftTextWidth + 25)}px)`,
- overflow: 'visible',
- width: '100px',
- userSelect: 'none',
- pointerEvents: 'none',
- left: '0',
- };
+ const {
+ textWidth, recRadius, textY, width, textOpacity, textHeight, rectOpacity,
+ } = textStyles;
+ const { dateLeft, dateRight } = getDateText();
+ const leftTextWidth = isCompareActive ? 60 : textWidth;
+ const rightTextWidth = isCompareActive ? 60 : textWidth;
+ const svgStyle = {
+ position: 'absolute',
+ transform: `translateX(${-(leftTextWidth + 25)}px)`,
+ overflow: 'visible',
+ width: '100px',
+ userSelect: 'none',
+ pointerEvents: 'none',
+ left: '0',
+ };
- return (
- <svg
- ref={this.nodeRef}
- className="dateline-text"
- style={svgStyle}
- >
- {active && (
- <>
- <rect
- width={leftTextWidth + 10}
- height={textHeight}
- x={0}
- rx={recRadius}
- opacity={dateLeft ? rectOpacity : '0'}
- />
- <text
- y={textY}
- x={6}
- width={width}
- opacity={dateLeft ? textOpacity : '0'}
- >
- {dateLeft}
- </text>
- <rect
- width={rightTextWidth + 10}
- height={textHeight}
- x={leftTextWidth + 40}
- rx={recRadius}
- opacity={dateRight ? rectOpacity : '0'}
- />
- <text
- y={textY}
- x={leftTextWidth + 46}
- opacity={dateRight ? textOpacity : '0'}
- >
- {dateRight}
- </text>
- </>
- )}
- </svg>
- );
- }
-}
+ return (
+ <svg
+ ref={nodeRef}
+ className="dateline-text"
+ style={svgStyle}
+ >
+ {active && (
+ <>
+ <rect
+ width={leftTextWidth + 10}
+ height={textHeight}
+ x={0}
+ rx={recRadius}
+ opacity={dateLeft ? rectOpacity : '0'}
+ />
+ <text
+ y={textY}
+ x={6}
+ width={width}
+ opacity={dateLeft ? textOpacity : '0'}
+ >
+ {dateLeft}
+ </text>
+ <rect
+ width={rightTextWidth + 10}
+ height={textHeight}
+ x={leftTextWidth + 40}
+ rx={recRadius}
+ opacity={dateRight ? rectOpacity : '0'}
+ />
+ <text
+ y={textY}
+ x={leftTextWidth + 46}
+ opacity={dateRight ? textOpacity : '0'}
+ >
+ {dateRight}
+ </text>
+ </>
+ )}
+ </svg>
+ );
+};
LineText.propTypes = {
active: PropTypes.bool,
diff --git a/web/js/components/dateline/text.test.js b/web/js/components/dateline/text.test.js
index 160c184bb..66a21b44d 100644
--- a/web/js/components/dateline/text.test.js
+++ b/web/js/components/dateline/text.test.js
@@ -34,44 +34,36 @@ describe('LineText Component', () => {
cleanup();
});
- describe('Constructor & State', () => {
- it('should initialize with correct state', () => {
- const instance = new LineText(defaultProps);
-
- expect(instance.state.overlay).toBe('');
- expect(instance.nodeRef).toBeDefined();
- expect(instance.nodeRef.current).toBeNull();
- });
- });
-
describe('getDateText', () => {
- it('should return comparison text when compare is active (left)', () => {
- const props = { ...defaultProps, isCompareActive: true, isLeft: true };
- const instance = new LineText(props);
- const result = instance.getDateText();
-
- expect(result).toEqual({
- dateLeft: '+ 1 day',
- dateRight: '',
- });
+ it('should display comparison text when compare is active (left)', () => {
+ const props = {
+ ...defaultProps, isCompareActive: true, isLeft: true, active: true,
+ };
+ const { container } = render(<LineText {...props} />);
+ const texts = container.querySelectorAll('text');
+
+ expect(texts[0].textContent).toBe('+ 1 day');
+ expect(texts[1].textContent).toBe('');
});
- it('should return comparison text when compare is active (right)', () => {
- const props = { ...defaultProps, isCompareActive: true, isLeft: false };
- const instance = new LineText(props);
- const result = instance.getDateText();
+ it('should display comparison text when compare is active (right)', () => {
+ const props = {
+ ...defaultProps, isCompareActive: true, isLeft: false, active: true,
+ };
+ const { container } = render(<LineText {...props} />);
+ const texts = container.querySelectorAll('text');
- expect(result).toEqual({
- dateLeft: '',
- dateRight: '- 1 day',
- });
+ expect(texts[0].textContent).toBe('');
+ expect(texts[1].textContent).toBe('- 1 day');
});
- it('should return dates from selector when compare is not active', () => {
+ it('should display dates from selector when compare is not active', () => {
const mockDate = new Date('2023-01-15');
const mockDateA = '2023-01-16';
const mockDateB = '2023-01-15';
- const props = { ...defaultProps, date: mockDate, isCompareActive: false };
+ const props = {
+ ...defaultProps, date: mockDate, isCompareActive: false, active: true,
+ };
util.dateAdd.mockReturnValue(new Date('2023-01-16'));
compareSelectors.getCompareDates.mockReturnValue({
@@ -79,15 +71,13 @@ describe('LineText Component', () => {
dateB: mockDateB,
});
- const instance = new LineText(props);
- const result = instance.getDateText();
+ const { container } = render(<LineText {...props} />);
+ const texts = container.querySelectorAll('text');
expect(util.dateAdd).toHaveBeenCalledWith(mockDate, 'day', 1);
expect(compareSelectors.getCompareDates).toHaveBeenCalled();
- expect(result).toEqual({
- dateLeft: mockDateA,
- dateRight: mockDateB,
- });
+ expect(texts[0].textContent).toBe(mockDateA);
+ expect(texts[1].textContent).toBe(mockDateB);
});
});
diff --git a/web/js/components/layer/settings/opacity.js b/web/js/components/layer/settings/opacity.js
index 01c8eb974..149ae5758 100644
--- a/web/js/components/layer/settings/opacity.js
+++ b/web/js/components/layer/settings/opacity.js
@@ -1,47 +1,37 @@
-import React from 'react';
+import { useState, useRef } from 'react';
import PropTypes from 'prop-types';
import { debounce } from 'lodash';
-class OpacitySelect extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- value: props.start,
- };
+const OpacitySelect = ({ layer, setOpacity, start = 100 }) => {
+ const [value, setValue] = useState(start);
- this.debouncedSetOpacity = debounce((layerId, opacity) => {
- const { setOpacity } = this.props;
+ const debouncedSetOpacity = useRef(
+ debounce((layerId, opacity) => {
setOpacity(layerId, opacity);
- }, 100);
- }
+ }, 100),
+ ).current;
- render() {
- const { layer, start } = this.props;
- const { value } = this.state;
- return (
- <div className="layer-opacity-select settings-component">
- <h2 className="wv-header">Opacity</h2>
- <input
- type="range"
- className="form-range"
- defaultValue={start}
- onChange={(e) => {
- const val = parseFloat(e.target.value);
- this.debouncedSetOpacity(layer.id, (val / 100).toFixed(2));
- this.setState({ value: val });
- }}
- style={{ '--value-percent': `${value}%` }}
- />
- <div className="wv-label wv-label-opacity mt-1">
- {`${value}%`}
- </div>
+ return (
+ <div className="layer-opacity-select settings-component">
+ <h2 className="wv-header">Opacity</h2>
+ <input
+ type="range"
+ className="form-range"
+ defaultValue={start}
+ onChange={(e) => {
+ const val = parseFloat(e.target.value);
+ debouncedSetOpacity(layer.id, (val / 100).toFixed(2));
+ setValue(val);
+ }}
+ style={{ '--value-percent': `${value}%` }}
+ />
+ <div className="wv-label wv-label-opacity mt-1">
+ {`${value}%`}
</div>
- );
- }
-}
-OpacitySelect.defaultProps = {
- start: 100,
+ </div>
+ );
};
+
OpacitySelect.propTypes = {
layer: PropTypes.oneOfType([PropTypes.object, PropTypes.oneOf(['null'])]),
setOpacity: PropTypes.func,