Interactive interface for browsing global, full-resolution satellite imagery
import { useRef, useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import OlOverlay from 'ol/Overlay';
import util from '../../util/util';
import { getCompareDates } from '../../modules/compare/selectors';
const textStyles = {
textOpacity: 1,
rectOpacity: 0.85,
width: '300',
color: 'white',
textY: 14,
fill: 'rgba(40,40,40,0.75)',
textWidth: 94,
textHeight: 20,
recRadius: 3,
};
const LineText = ({
active, date, x, isCompareActive, isLeft, map, textCoords,
}) => {
const nodeRef = useRef(null);
const [overlay, setOverlay] = useState(null);
useEffect(() => {
const newOverlay = new OlOverlay({
element: nodeRef.current,
stopEvent: false,
});
newOverlay.setPosition([x, 90]);
map.addOverlay(newOverlay);
setOverlay(newOverlay);
}, []);
useEffect(() => {
if (overlay) {
overlay.setPosition([x, textCoords[1]]);
}
}, [overlay, x, textCoords]);
const getDateText = () => {
if (isCompareActive) {
return {
dateLeft: isLeft ? '+ 1 day' : '',
dateRight: isLeft ? '' : '- 1 day',
};
}
const dateState = {
selected: util.dateAdd(date, 'day', 1),
selectedB: date,
};
const { dateA, dateB } = getCompareDates({ date: dateState });
return {
dateLeft: dateA,
dateRight: dateB,
};
};
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={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,
date: PropTypes.oneOfType([PropTypes.object, PropTypes.oneOf(['null'])]),
x: PropTypes.number,
isCompareActive: PropTypes.bool,
isLeft: PropTypes.bool,
map: PropTypes.oneOfType([PropTypes.object, PropTypes.oneOf(['null'])]),
textCoords: PropTypes.oneOfType([PropTypes.array, PropTypes.oneOf(['null'])]),
};
export default LineText;