Skip to content

Commit

Permalink
Set width and height on whats-new-popup image, instead of setting sta…
Browse files Browse the repository at this point in the history
…te on img load
  • Loading branch information
danjm committed Apr 28, 2021
1 parent 233b70e commit e805cf2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
6 changes: 5 additions & 1 deletion shared/notifications/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ export const UI_NOTIFICATIONS = {
1: {
id: 1,
date: '2020-03-17',
image: 'images/mobile-link-qr.svg',
image: {
src: 'images/mobile-link-qr.svg',
height: '270px',
width: '270px',
},
},
2: {
id: 2,
Expand Down
63 changes: 30 additions & 33 deletions ui/app/components/app/whats-new-popup/whats-new-popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export default function WhatsNewPopup({ onClose }) {
const notifications = useSelector(getSortedNotificationsToShow);

const [seenNotifications, setSeenNotifications] = useState({});
const [imageLoaded, setImageLoaded] = useState(!notifications[0].image);

const popoverRef = useRef();

Expand All @@ -59,40 +58,37 @@ export default function WhatsNewPopup({ onClose }) {
);

useEffect(() => {
let observer;
if (imageLoaded) {
observer = new window.IntersectionObserver(
(entries, _observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const [id, ref] = Object.entries(idRefMap).find(([_, _ref]) =>
_ref.current.isSameNode(entry.target),
);

setSeenNotifications((_seenNotifications) => ({
..._seenNotifications,
[id]: true,
}));

_observer.unobserve(ref.current);
}
});
},
{
root: popoverRef.current,
threshold: 1.0,
},
);

Object.values(idRefMap).forEach((ref) => {
observer.observe(ref.current);
});
}
const observer = new window.IntersectionObserver(
(entries, _observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const [id, ref] = Object.entries(idRefMap).find(([_, _ref]) =>
_ref.current.isSameNode(entry.target),
);

setSeenNotifications((_seenNotifications) => ({
..._seenNotifications,
[id]: true,
}));

_observer.unobserve(ref.current);
}
});
},
{
root: popoverRef.current,
threshold: 1.0,
},
);

Object.values(idRefMap).forEach((ref) => {
observer.observe(ref.current);
});

return () => {
observer?.disconnect();
};
}, [idRefMap, setSeenNotifications, imageLoaded]);
}, [idRefMap, setSeenNotifications]);

const renderFirstNotification = (notification, id, date) => {
return (
Expand All @@ -106,8 +102,9 @@ export default function WhatsNewPopup({ onClose }) {
{notification.image && (
<img
className="whats-new-popup__notification-image"
src={notification.image}
onLoad={() => setImageLoaded(true)}
src={notification.image.src}
height={notification.image.height}
width={notification.image.width}
/>
)}
<div className="whats-new-popup__notification-title">
Expand Down

0 comments on commit e805cf2

Please sign in to comment.